8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable

Tue, 22 Oct 2013 03:36:44 +0200

author
jfranck
date
Tue, 22 Oct 2013 03:36:44 +0200
changeset 2154
ac839d6f4953
parent 2153
b82982ac3ca2
child 2155
87c950ea88be

8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable
Summary: In AnnoConstruct.getAnnotationByType() check that the annotation sought after is inherited before looking on supertypes.
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/code/AnnoConstruct.java file | annotate | diff | comparison | revisions
test/tools/javac/processing/model/element/TestNonInherited.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/AnnoConstruct.java	Mon Oct 21 15:37:11 2013 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/AnnoConstruct.java	Tue Oct 22 03:36:44 2013 +0200
     1.3 @@ -25,6 +25,7 @@
     1.4  package com.sun.tools.javac.code;
     1.5  
     1.6  import java.lang.annotation.Annotation;
     1.7 +import java.lang.annotation.Inherited;
     1.8  import java.lang.reflect.InvocationTargetException;
     1.9  import java.lang.reflect.Method;
    1.10  
    1.11 @@ -112,7 +113,8 @@
    1.12          }
    1.13  
    1.14          // Deal with inherited annotations
    1.15 -        if (direct == null && container == null)
    1.16 +        if (direct == null && container == null &&
    1.17 +                annoType.isAnnotationPresent(Inherited.class))
    1.18              return getInheritedAnnotations(annoType);
    1.19  
    1.20          // Pack them in an array
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/processing/model/element/TestNonInherited.java	Tue Oct 22 03:36:44 2013 +0200
     2.3 @@ -0,0 +1,78 @@
     2.4 +/*
     2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 8026855
    2.30 + * @summary Javac should only look on supertypes for repeatable annotations if
    2.31 + *          both container and containee are inherited.
    2.32 + * @library /tools/javac/lib
    2.33 + * @build   JavacTestingAbstractProcessor TestNonInherited
    2.34 + * @compile -processor TestNonInherited -proc:only TestNonInherited.java
    2.35 + */
    2.36 +
    2.37 +import com.sun.tools.javac.util.Assert;
    2.38 +
    2.39 +import java.lang.annotation.*;
    2.40 +import java.util.Arrays;
    2.41 +import java.util.Set;
    2.42 +import javax.annotation.processing.*;
    2.43 +import javax.lang.model.element.*;
    2.44 +import javax.lang.model.util.*;
    2.45 +
    2.46 +import static javax.lang.model.util.ElementFilter.*;
    2.47 +
    2.48 +@TestNonInherited.Foo(1)
    2.49 +public class TestNonInherited extends JavacTestingAbstractProcessor {
    2.50 +    public boolean process(Set<? extends TypeElement> annotations,
    2.51 +                           RoundEnvironment roundEnv) {
    2.52 +        if (!roundEnv.processingOver()) {
    2.53 +            boolean hasRun = false;
    2.54 +            for (Element element : roundEnv.getRootElements())
    2.55 +                for (TypeElement te : typesIn(element.getEnclosedElements()))
    2.56 +                    if (te.getQualifiedName().contentEquals("TestNonInherited.T2")) {
    2.57 +                        hasRun = true;
    2.58 +                        Foo[] foos = te.getAnnotationsByType(Foo.class);
    2.59 +                        System.out.println("  " + te);
    2.60 +                        System.out.println("  " + Arrays.asList(foos));
    2.61 +                        Assert.check(foos.length == 0, "Should not find any instance of @Foo");
    2.62 +                    }
    2.63 +            if (!hasRun)
    2.64 +                throw new RuntimeException("The annotation processor could not find the declaration of T2, test broken!");
    2.65 +        }
    2.66 +        return true;
    2.67 +    }
    2.68 +
    2.69 +    public static class T2 extends TestNonInherited {
    2.70 +    }
    2.71 +
    2.72 +    @Repeatable(FooContainer.class)
    2.73 +    public static @interface Foo {
    2.74 +        int value();
    2.75 +    }
    2.76 +
    2.77 +    @Inherited
    2.78 +    public static @interface FooContainer {
    2.79 +        Foo[] value();
    2.80 +    }
    2.81 +}

mercurial