test/tools/javac/processing/model/element/TestAnonClassNames.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1521
71f35e4b93a5
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial