/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @bug 8031744 * @summary test that the right thing happens when we compile java/lang/annotation/Target.java * @library /tools/javac/lib * @build JavacTestingAbstractProcessor TestCompileMetaAnnotations * @compile -processor TestCompileMetaAnnotations -proc:only -implicit:class ../java/lang/annotation/Target.java */ import com.sun.tools.javac.code.Attribute; import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol.ClassSymbol; import com.sun.tools.javac.code.Symtab; import com.sun.tools.javac.code.Type; import com.sun.tools.javac.processing.JavacProcessingEnvironment; import com.sun.tools.javac.util.Assert; import com.sun.tools.javac.util.Context; import java.util.*; import javax.annotation.processing.*; import javax.lang.model.element.*; @SupportedAnnotationTypes({"java.lang.annotation.Target"}) public class TestCompileMetaAnnotations extends JavacTestingAbstractProcessor { private int round = 0; @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { if (round > 0) return false; JavacProcessingEnvironment env = (JavacProcessingEnvironment)processingEnv; Context ctx = env.getContext(); Symtab tab = Symtab.instance(ctx); // game on! Set elements = roundEnv.getElementsAnnotatedWith(java.lang.annotation.Target.class); Assert.check(elements.size() == 1); Type predefTargetType = tab.annotationTargetType; ClassSymbol predefTarget = (ClassSymbol)predefTargetType.tsym; ClassSymbol currentlyCompiling = (ClassSymbol)elements.toArray()[0]; Type currentyCompilingType = currentlyCompiling.type; Attribute.Compound usingPredef = currentlyCompiling.attribute(predefTarget); Attribute.Compound usingCurrently = currentlyCompiling.attribute(currentlyCompiling); System.err.println("Predef: " + printSymbol(predefTarget)); System.err.println("Compiling: " + printSymbol(currentlyCompiling)); System.err.println("Using predef: " + usingPredef + " declared: " + printSymbol(usingPredef.type.tsym)); System.err.println("Using currently: " + usingCurrently + " declared: " + printSymbol(usingCurrently.type.tsym)); // Check that the predefined Symbol for @Target matches the Symbol we are currently compiling // and matches the tsym for the @Target... annotation on the declaration of // java.lang.annotation.Target Assert.check(predefTarget == currentlyCompiling); Assert.check(currentlyCompiling == usingPredef.type.tsym); Assert.check(usingPredef.type.tsym == usingCurrently.type.tsym); Assert.check(predefTargetType == currentyCompilingType); Assert.checkNonNull(predefTarget.sourcefile); round++; return true; } private String printSymbol(Symbol s) { return s.getClass().getName() + "@0x" + Integer.toHexString(s.hashCode()); } }