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

Wed, 29 Sep 2010 23:27:57 -0700

author
darcy
date
Wed, 29 Sep 2010 23:27:57 -0700
changeset 699
d2aaaec153e8
parent 672
ea54372637a5
child 722
4851ff2ffc10
permissions
-rw-r--r--

6983738: Use a JavacTestingAbstractProcessor
Reviewed-by: jjg

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

mercurial