langtools/test/tools/javac/processing/model/element/TestAnonClassNames.java
author darcy
Wed, 29 Sep 2010 23:27:57 -0700
changeset 6720 f16f91662ad8
parent 6590 f745e683da2c
child 7072 4863847e93a5
permissions -rw-r--r--
6983738: Use a JavacTestingAbstractProcessor Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5012
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5320
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5012
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
     4
 *
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
     8
 *
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    13
 * accompanied this code).
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    14
 *
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5320
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5320
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5320
diff changeset
    21
 * questions.
5012
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    22
 */
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    23
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    24
/*
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    25
 * @test
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    26
 * @bug 6449781
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    27
 * @summary Test that reported names of anonymous classes are non-null.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    28
 * @author  Joseph D. Darcy
6720
f16f91662ad8 6983738: Use a JavacTestingAbstractProcessor
darcy
parents: 6590
diff changeset
    29
 * @library ../../../lib
f16f91662ad8 6983738: Use a JavacTestingAbstractProcessor
darcy
parents: 6590
diff changeset
    30
 * @build   JavacTestingAbstractProcessor TestAnonSourceNames
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 5520
diff changeset
    31
 * @compile -processor TestAnonSourceNames TestAnonClassNames.java
5012
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    32
 * @run main TestAnonClassNames
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    33
 */
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    34
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    35
/*
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    36
 * This test operates in phases to test retrieving the qualified name
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    37
 * of anonymous classes from type elements modeling the anonymous
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    38
 * class.  The type elements are generated using both source files and
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    39
 * class files as the basis of constructing the elements.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    40
 *
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    41
 * Source files will be tested by the @compile line which runs
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    42
 * TestAnonSourceNames as an annotation processor over this file.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    43
 *
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    44
 * Class files are tested by the @run command on this type.  This
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    45
 * class gets the names of classes with different nesting kinds,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    46
 * including anonymous classes, and then invokes the compiler with an
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    47
 * annotation processor having the class files names as inputs.  The
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    48
 * compiler is invoked via the javax.tools mechanism.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    49
 */
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    50
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    51
import java.lang.annotation.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    52
import javax.lang.model.element.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    53
import javax.annotation.processing.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    54
import javax.lang.model.SourceVersion;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    55
import javax.lang.model.element.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    56
import javax.lang.model.util.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    57
import javax.tools.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    58
import java.util.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    59
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    60
import static java.lang.annotation.RetentionPolicy.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    61
import static javax.lang.model.element.NestingKind.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    62
import static javax.lang.model.util.ElementFilter.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    63
import static javax.tools.Diagnostic.Kind.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    64
import static javax.tools.StandardLocation.*;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    65
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    66
@Nesting(TOP_LEVEL)
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    67
public class TestAnonClassNames {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    68
    @Nesting(MEMBER)
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    69
    static class MemberClass1{}
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    70
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    71
    @Nesting(MEMBER)
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    72
    class MemberClass2{}
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    73
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    74
    @Nesting(MEMBER)
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    75
    class Win$$AtVegas { } // Class with funny name.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    76
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    77
    public static void main(String... argv) {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    78
        @Nesting(LOCAL)
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    79
        class LocalClass{};
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    80
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    81
        Object o =  new @Nesting(ANONYMOUS) Object() { // An anonymous annotated class
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    82
                public String toString() {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    83
                    return "I have no name!";
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    84
                }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    85
            };
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    86
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    87
        Class<?>[] classes = {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    88
            MemberClass1.class,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    89
            MemberClass2.class,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    90
            LocalClass.class,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    91
            Win$$AtVegas.class,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    92
            o.getClass(),
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    93
            TestAnonClassNames.class,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    94
        };
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    95
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    96
        for(Class<?> clazz : classes) {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    97
            String name = clazz.getName();
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    98
            System.out.format("%s is %s%n",
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
    99
                              clazz.getName(),
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   100
                              clazz.getAnnotation(Nesting.class).value());
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   101
            testClassName(name);
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   102
        }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   103
    }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   104
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   105
    /**
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   106
     * Perform annotation processing on the class file name and verify
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   107
     * the existence of different flavors of class names when the
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   108
     * input classes are modeled as elements.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   109
     */
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   110
    static void testClassName(String className) {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   111
        JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
5320
e2aaa958b02d 6939618: Revert 'simple' diamond implementation
mcimadamore
parents: 5012
diff changeset
   112
        List<String> classNames = new ArrayList<String>();
5012
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   113
        classNames.add(className);
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   114
5320
e2aaa958b02d 6939618: Revert 'simple' diamond implementation
mcimadamore
parents: 5012
diff changeset
   115
        List<String> options = new ArrayList<String>();
5012
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   116
        options.add("-proc:only");
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   117
        options.add("-classpath");
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   118
        options.add(System.getProperty("test.classes"));
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   119
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   120
        JavaCompiler.CompilationTask compileTask =
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   121
            javaCompiler.getTask(null, // Output
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   122
                                 null, // File manager
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   123
                                 null, // Diagnostics
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   124
                                 options,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   125
                                 classNames,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   126
                                 null); // Sources
5320
e2aaa958b02d 6939618: Revert 'simple' diamond implementation
mcimadamore
parents: 5012
diff changeset
   127
        List<Processor> processors = new ArrayList<Processor>();
5012
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   128
        processors.add(new ClassNameProber());
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   129
        compileTask.setProcessors(processors);
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   130
        Boolean goodResult = compileTask.call();
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   131
        if (!goodResult) {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   132
            throw new RuntimeException("Errors found during compile.");
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   133
        }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   134
    }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   135
}
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   136
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   137
@Retention(RUNTIME)
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   138
@interface Nesting {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   139
    NestingKind value();
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   140
}
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   141
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   142
/**
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   143
 * Probe at the various kinds of names of a type element.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   144
 */
6720
f16f91662ad8 6983738: Use a JavacTestingAbstractProcessor
darcy
parents: 6590
diff changeset
   145
class ClassNameProber extends JavacTestingAbstractProcessor {
5012
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   146
    public ClassNameProber(){super();}
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   147
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   148
    private boolean classesFound=false;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   149
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   150
    public boolean process(Set<? extends TypeElement> annotations,
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   151
                           RoundEnvironment roundEnv) {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   152
        if (!roundEnv.processingOver()) {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   153
            for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   154
                classesFound = true;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   155
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   156
                // Verify different names are non-null; an NPE will
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   157
                // result in failed compile status being reported.
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   158
                NestingKind nestingKind = typeElt.getNestingKind();
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   159
                System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n",
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   160
                                  typeElt.getSimpleName().toString(),
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   161
                                  typeElt.getQualifiedName().toString(),
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   162
                                  typeElt.getKind().toString(),
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   163
                                  nestingKind.toString());
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   164
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   165
                if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   166
                    throw new RuntimeException("Mismatch of expected and reported nesting kind.");
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   167
                }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   168
            }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   169
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   170
        }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   171
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   172
        if (!classesFound) {
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   173
            throw new RuntimeException("Error: no classes processed.");
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   174
        }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   175
        return true;
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   176
    }
405341b6e9c5 6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
darcy
parents:
diff changeset
   177
}