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

Tue, 19 Oct 2010 15:02:48 -0700

author
jjg
date
Tue, 19 Oct 2010 15:02:48 -0700
changeset 722
4851ff2ffc10
parent 699
d2aaaec153e8
child 937
a2399c8db703
permissions
-rw-r--r--

6987760: remove 308 support from JDK7
Reviewed-by: darcy, mcimadamore

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
jjg@722 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();
jjg@722 98 Nesting anno = clazz.getAnnotation(Nesting.class);
darcy@515 99 System.out.format("%s is %s%n",
darcy@515 100 clazz.getName(),
jjg@722 101 anno == null ? "(unset/ANONYMOUS)" : anno.value());
darcy@515 102 testClassName(name);
darcy@515 103 }
darcy@515 104 }
darcy@515 105
darcy@515 106 /**
darcy@515 107 * Perform annotation processing on the class file name and verify
darcy@515 108 * the existence of different flavors of class names when the
darcy@515 109 * input classes are modeled as elements.
darcy@515 110 */
darcy@515 111 static void testClassName(String className) {
darcy@515 112 JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
mcimadamore@536 113 List<String> classNames = new ArrayList<String>();
darcy@515 114 classNames.add(className);
darcy@515 115
mcimadamore@536 116 List<String> options = new ArrayList<String>();
darcy@515 117 options.add("-proc:only");
darcy@515 118 options.add("-classpath");
darcy@515 119 options.add(System.getProperty("test.classes"));
darcy@515 120
darcy@515 121 JavaCompiler.CompilationTask compileTask =
darcy@515 122 javaCompiler.getTask(null, // Output
darcy@515 123 null, // File manager
darcy@515 124 null, // Diagnostics
darcy@515 125 options,
darcy@515 126 classNames,
darcy@515 127 null); // Sources
mcimadamore@536 128 List<Processor> processors = new ArrayList<Processor>();
darcy@515 129 processors.add(new ClassNameProber());
darcy@515 130 compileTask.setProcessors(processors);
darcy@515 131 Boolean goodResult = compileTask.call();
darcy@515 132 if (!goodResult) {
darcy@515 133 throw new RuntimeException("Errors found during compile.");
darcy@515 134 }
darcy@515 135 }
darcy@515 136 }
darcy@515 137
darcy@515 138 @Retention(RUNTIME)
darcy@515 139 @interface Nesting {
darcy@515 140 NestingKind value();
darcy@515 141 }
darcy@515 142
darcy@515 143 /**
darcy@515 144 * Probe at the various kinds of names of a type element.
darcy@515 145 */
darcy@699 146 class ClassNameProber extends JavacTestingAbstractProcessor {
darcy@515 147 public ClassNameProber(){super();}
darcy@515 148
darcy@515 149 private boolean classesFound=false;
darcy@515 150
darcy@515 151 public boolean process(Set<? extends TypeElement> annotations,
darcy@515 152 RoundEnvironment roundEnv) {
darcy@515 153 if (!roundEnv.processingOver()) {
darcy@515 154 for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) {
darcy@515 155 classesFound = true;
darcy@515 156
darcy@515 157 // Verify different names are non-null; an NPE will
darcy@515 158 // result in failed compile status being reported.
darcy@515 159 NestingKind nestingKind = typeElt.getNestingKind();
darcy@515 160 System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n",
darcy@515 161 typeElt.getSimpleName().toString(),
darcy@515 162 typeElt.getQualifiedName().toString(),
darcy@515 163 typeElt.getKind().toString(),
darcy@515 164 nestingKind.toString());
jjg@722 165 Nesting anno = typeElt.getAnnotation(Nesting.class);
jjg@722 166 if ((anno == null ? NestingKind.ANONYMOUS : anno.value()) != nestingKind) {
darcy@515 167 throw new RuntimeException("Mismatch of expected and reported nesting kind.");
darcy@515 168 }
darcy@515 169 }
darcy@515 170
darcy@515 171 }
darcy@515 172
darcy@515 173 if (!classesFound) {
darcy@515 174 throw new RuntimeException("Error: no classes processed.");
darcy@515 175 }
darcy@515 176 return true;
darcy@515 177 }
darcy@515 178 }

mercurial