duke@1: /* jfranck@1313: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.comp; duke@1: jfranck@1313: import java.util.Map; duke@1: import com.sun.tools.javac.util.*; jfranck@1313: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; duke@1: import com.sun.tools.javac.code.*; duke@1: import com.sun.tools.javac.code.Symbol.*; duke@1: import com.sun.tools.javac.tree.*; duke@1: import com.sun.tools.javac.tree.JCTree.*; duke@1: jjg@1374: import static com.sun.tools.javac.code.TypeTag.ARRAY; jjg@1374: import static com.sun.tools.javac.code.TypeTag.CLASS; jjg@1127: import static com.sun.tools.javac.tree.JCTree.Tag.*; jjg@1127: duke@1: /** Enter annotations on symbols. Annotations accumulate in a queue, duke@1: * which is processed at the top level of any set of recursive calls duke@1: * requesting it be processed. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class Annotate { duke@1: protected static final Context.Key annotateKey = duke@1: new Context.Key(); duke@1: duke@1: public static Annotate instance(Context context) { duke@1: Annotate instance = context.get(annotateKey); duke@1: if (instance == null) duke@1: instance = new Annotate(context); duke@1: return instance; duke@1: } duke@1: duke@1: final Attr attr; duke@1: final TreeMaker make; duke@1: final Log log; duke@1: final Symtab syms; jjg@113: final Names names; duke@1: final Resolve rs; duke@1: final Types types; duke@1: final ConstFold cfolder; duke@1: final Check chk; duke@1: duke@1: protected Annotate(Context context) { duke@1: context.put(annotateKey, this); duke@1: attr = Attr.instance(context); duke@1: make = TreeMaker.instance(context); duke@1: log = Log.instance(context); duke@1: syms = Symtab.instance(context); jjg@113: names = Names.instance(context); duke@1: rs = Resolve.instance(context); duke@1: types = Types.instance(context); duke@1: cfolder = ConstFold.instance(context); duke@1: chk = Check.instance(context); duke@1: } duke@1: duke@1: /* ******************************************************************** duke@1: * Queue maintenance duke@1: *********************************************************************/ duke@1: duke@1: private int enterCount = 0; duke@1: duke@1: ListBuffer q = new ListBuffer(); jfranck@1313: ListBuffer repeatedQ = new ListBuffer(); duke@1: jfranck@1313: public void normal(Annotator a) { duke@1: q.append(a); duke@1: } duke@1: duke@1: public void earlier(Annotator a) { duke@1: q.prepend(a); duke@1: } duke@1: jfranck@1313: public void repeated(Annotator a) { jfranck@1313: repeatedQ.append(a); jfranck@1313: } jfranck@1313: duke@1: /** Called when the Enter phase starts. */ duke@1: public void enterStart() { duke@1: enterCount++; duke@1: } duke@1: duke@1: /** Called after the Enter phase completes. */ duke@1: public void enterDone() { duke@1: enterCount--; duke@1: flush(); duke@1: } duke@1: duke@1: public void flush() { duke@1: if (enterCount != 0) return; duke@1: enterCount++; duke@1: try { duke@1: while (q.nonEmpty()) duke@1: q.next().enterAnnotation(); jfranck@1313: jfranck@1313: while (repeatedQ.nonEmpty()) { jfranck@1313: repeatedQ.next().enterAnnotation(); jfranck@1313: } duke@1: } finally { duke@1: enterCount--; duke@1: } duke@1: } duke@1: duke@1: /** A client that has annotations to add registers an annotator, duke@1: * the method it will use to add the annotation. There are no duke@1: * parameters; any needed data should be captured by the duke@1: * Annotator. duke@1: */ duke@1: public interface Annotator { duke@1: void enterAnnotation(); duke@1: String toString(); duke@1: } duke@1: jfranck@1313: /** jfranck@1313: * This context contains all the information needed to synthesize new jfranck@1313: * annotations trees by the completer for repeating annotations. jfranck@1313: */ jfranck@1313: public class AnnotateRepeatedContext { jfranck@1313: public final Env env; jfranck@1313: public final Map> annotated; jfranck@1313: public final Map pos; jfranck@1313: public final Log log; jfranck@1313: jfranck@1313: public AnnotateRepeatedContext(Env env, jfranck@1313: Map> annotated, jfranck@1313: Map pos, jfranck@1313: Log log) { jjg@1322: Assert.checkNonNull(env); jjg@1322: Assert.checkNonNull(annotated); jjg@1322: Assert.checkNonNull(pos); jjg@1322: Assert.checkNonNull(log); jfranck@1313: jfranck@1313: this.env = env; jfranck@1313: this.annotated = annotated; jfranck@1313: this.pos = pos; jfranck@1313: this.log = log; jfranck@1313: } jfranck@1313: jfranck@1313: /** jfranck@1313: * Process a list of repeating annotations returning a new jfranck@1313: * Attribute.Compound that is the attribute for the synthesized tree jfranck@1313: * for the container. jfranck@1313: * jfranck@1313: * @param repeatingAnnotations a List of repeating annotations jfranck@1313: * @return a new Attribute.Compound that is the container for the repeatingAnnotations jfranck@1313: */ jfranck@1445: public Attribute.Compound processRepeatedAnnotations(List repeatingAnnotations, Symbol sym) { jfranck@1445: return Annotate.this.processRepeatedAnnotations(repeatingAnnotations, this, sym); jfranck@1313: } jfranck@1313: jfranck@1313: /** jfranck@1313: * Queue the Annotator a on the repeating annotations queue of the jfranck@1313: * Annotate instance this context belongs to. jfranck@1313: * jfranck@1313: * @param a the Annotator to enqueue for repeating annotation annotating jfranck@1313: */ jfranck@1313: public void annotateRepeated(Annotator a) { jfranck@1313: Annotate.this.repeated(a); jfranck@1313: } jfranck@1313: } duke@1: duke@1: /* ******************************************************************** duke@1: * Compute an attribute from its annotation. duke@1: *********************************************************************/ duke@1: duke@1: /** Process a single compound annotation, returning its duke@1: * Attribute. Used from MemberEnter for attaching the attributes duke@1: * to the annotated symbol. duke@1: */ duke@1: Attribute.Compound enterAnnotation(JCAnnotation a, duke@1: Type expected, duke@1: Env env) { duke@1: // The annotation might have had its type attributed (but not checked) duke@1: // by attr.attribAnnotationTypes during MemberEnter, in which case we do not duke@1: // need to do it again. duke@1: Type at = (a.annotationType.type != null ? a.annotationType.type duke@1: : attr.attribType(a.annotationType, env)); duke@1: a.type = chk.checkType(a.annotationType.pos(), at, expected); duke@1: if (a.type.isErroneous()) duke@1: return new Attribute.Compound(a.type, List.>nil()); duke@1: if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) { duke@1: log.error(a.annotationType.pos(), duke@1: "not.annotation.type", a.type.toString()); duke@1: return new Attribute.Compound(a.type, List.>nil()); duke@1: } duke@1: List args = a.args; jjg@1127: if (args.length() == 1 && !args.head.hasTag(ASSIGN)) { duke@1: // special case: elided "value=" assumed duke@1: args.head = make.at(args.head.pos). duke@1: Assign(make.Ident(names.value), args.head); duke@1: } duke@1: ListBuffer> buf = duke@1: new ListBuffer>(); duke@1: for (List tl = args; tl.nonEmpty(); tl = tl.tail) { duke@1: JCExpression t = tl.head; jjg@1127: if (!t.hasTag(ASSIGN)) { duke@1: log.error(t.pos(), "annotation.value.must.be.name.value"); duke@1: continue; duke@1: } duke@1: JCAssign assign = (JCAssign)t; jjg@1127: if (!assign.lhs.hasTag(IDENT)) { duke@1: log.error(t.pos(), "annotation.value.must.be.name.value"); duke@1: continue; duke@1: } duke@1: JCIdent left = (JCIdent)assign.lhs; duke@1: Symbol method = rs.resolveQualifiedMethod(left.pos(), mcimadamore@992: env, mcimadamore@992: a.type, mcimadamore@992: left.name, mcimadamore@992: List.nil(), mcimadamore@992: null); duke@1: left.sym = method; duke@1: left.type = method.type; duke@1: if (method.owner != a.type.tsym) duke@1: log.error(left.pos(), "no.annotation.member", left.name, a.type); duke@1: Type result = method.type.getReturnType(); duke@1: Attribute value = enterAttributeValue(result, assign.rhs, env); duke@1: if (!method.type.isErroneous()) duke@1: buf.append(new Pair duke@1: ((MethodSymbol)method, value)); mcimadamore@676: t.type = result; duke@1: } duke@1: return new Attribute.Compound(a.type, buf.toList()); duke@1: } duke@1: duke@1: Attribute enterAttributeValue(Type expected, duke@1: JCExpression tree, duke@1: Env env) { mcimadamore@992: //first, try completing the attribution value sym - if a completion mcimadamore@992: //error is thrown, we should recover gracefully, and display an mcimadamore@992: //ordinary resolution diagnostic. mcimadamore@992: try { mcimadamore@992: expected.tsym.complete(); mcimadamore@992: } catch(CompletionFailure e) { mcimadamore@992: log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym); mcimadamore@992: return new Attribute.Error(expected); mcimadamore@992: } duke@1: if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) { duke@1: Type result = attr.attribExpr(tree, env, expected); duke@1: if (result.isErroneous()) duke@1: return new Attribute.Error(expected); duke@1: if (result.constValue() == null) { duke@1: log.error(tree.pos(), "attribute.value.must.be.constant"); duke@1: return new Attribute.Error(expected); duke@1: } duke@1: result = cfolder.coerce(result, expected); duke@1: return new Attribute.Constant(expected, result.constValue()); duke@1: } duke@1: if (expected.tsym == syms.classType.tsym) { duke@1: Type result = attr.attribExpr(tree, env, expected); duke@1: if (result.isErroneous()) duke@1: return new Attribute.Error(expected); duke@1: if (TreeInfo.name(tree) != names._class) { duke@1: log.error(tree.pos(), "annotation.value.must.be.class.literal"); duke@1: return new Attribute.Error(expected); duke@1: } duke@1: return new Attribute.Class(types, duke@1: (((JCFieldAccess) tree).selected).type); duke@1: } duke@1: if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) { jjg@1127: if (!tree.hasTag(ANNOTATION)) { duke@1: log.error(tree.pos(), "annotation.value.must.be.annotation"); duke@1: expected = syms.errorType; duke@1: } duke@1: return enterAnnotation((JCAnnotation)tree, expected, env); duke@1: } jjg@1374: if (expected.hasTag(ARRAY)) { // should really be isArray() jjg@1127: if (!tree.hasTag(NEWARRAY)) { duke@1: tree = make.at(tree.pos). duke@1: NewArray(null, List.nil(), List.of(tree)); duke@1: } duke@1: JCNewArray na = (JCNewArray)tree; duke@1: if (na.elemtype != null) { duke@1: log.error(na.elemtype.pos(), "new.not.allowed.in.annotation"); duke@1: return new Attribute.Error(expected); duke@1: } duke@1: ListBuffer buf = new ListBuffer(); duke@1: for (List l = na.elems; l.nonEmpty(); l=l.tail) { duke@1: buf.append(enterAttributeValue(types.elemtype(expected), duke@1: l.head, duke@1: env)); duke@1: } mcimadamore@676: na.type = expected; duke@1: return new Attribute. duke@1: Array(expected, buf.toArray(new Attribute[buf.length()])); duke@1: } jjg@1374: if (expected.hasTag(CLASS) && duke@1: (expected.tsym.flags() & Flags.ENUM) != 0) { duke@1: attr.attribExpr(tree, env, expected); duke@1: Symbol sym = TreeInfo.symbol(tree); duke@1: if (sym == null || duke@1: TreeInfo.nonstaticSelect(tree) || duke@1: sym.kind != Kinds.VAR || duke@1: (sym.flags() & Flags.ENUM) == 0) { duke@1: log.error(tree.pos(), "enum.annotation.must.be.enum.constant"); duke@1: return new Attribute.Error(expected); duke@1: } duke@1: VarSymbol enumerator = (VarSymbol) sym; duke@1: return new Attribute.Enum(expected, enumerator); duke@1: } duke@1: if (!expected.isErroneous()) duke@1: log.error(tree.pos(), "annotation.value.not.allowable.type"); duke@1: return new Attribute.Error(attr.attribExpr(tree, env, expected)); duke@1: } jfranck@1313: jfranck@1313: /* ********************************* jfranck@1313: * Support for repeating annotations jfranck@1313: ***********************************/ jfranck@1313: jfranck@1313: /* Process repeated annotations. This method returns the jfranck@1313: * synthesized container annotation or null IFF all repeating jfranck@1313: * annotation are invalid. This method reports errors/warnings. jfranck@1313: */ jfranck@1313: private Attribute.Compound processRepeatedAnnotations(List annotations, jfranck@1445: AnnotateRepeatedContext ctx, jfranck@1445: Symbol on) { jfranck@1313: Attribute.Compound firstOccurrence = annotations.head; jfranck@1313: List repeated = List.nil(); jfranck@1445: Type origAnnoType = null; jfranck@1313: Type arrayOfOrigAnnoType = null; jfranck@1313: Type targetContainerType = null; jfranck@1313: MethodSymbol containerValueSymbol = null; jfranck@1313: jfranck@1313: Assert.check(!annotations.isEmpty() && jfranck@1313: !annotations.tail.isEmpty()); // i.e. size() > 1 jfranck@1313: jfranck@1313: for (List al = annotations; jfranck@1313: !al.isEmpty(); jfranck@1313: al = al.tail) jfranck@1313: { jfranck@1313: Attribute.Compound currentAnno = al.head; jfranck@1313: jfranck@1313: origAnnoType = currentAnno.type; jfranck@1313: if (arrayOfOrigAnnoType == null) { jfranck@1313: arrayOfOrigAnnoType = types.makeArrayType(origAnnoType); duke@1: } jfranck@1313: jfranck@1313: Type currentContainerType = getContainingType(currentAnno, ctx.pos.get(currentAnno)); jfranck@1313: if (currentContainerType == null) { jfranck@1313: continue; jfranck@1313: } jfranck@1313: // Assert that the target Container is == for all repeated jfranck@1313: // annos of the same annotation type, the types should jfranck@1313: // come from the same Symbol, i.e. be '==' jfranck@1313: Assert.check(targetContainerType == null || currentContainerType == targetContainerType); jfranck@1313: targetContainerType = currentContainerType; jfranck@1313: jfranck@1313: containerValueSymbol = validateContainer(targetContainerType, origAnnoType, ctx.pos.get(currentAnno)); jfranck@1313: jfranck@1313: if (containerValueSymbol == null) { // Check of CA type failed jfranck@1313: // errors are already reported jfranck@1313: continue; jfranck@1313: } jfranck@1313: jfranck@1313: repeated = repeated.prepend(currentAnno); jfranck@1313: } jfranck@1313: jfranck@1313: if (!repeated.isEmpty()) { jfranck@1313: repeated = repeated.reverse(); jfranck@1313: JCAnnotation annoTree; jfranck@1313: TreeMaker m = make.at(ctx.pos.get(firstOccurrence)); jfranck@1313: Pair p = jfranck@1313: new Pair(containerValueSymbol, jfranck@1313: new Attribute.Array(arrayOfOrigAnnoType, repeated)); jfranck@1313: annoTree = m.Annotation(new Attribute.Compound(targetContainerType, jfranck@1313: List.of(p))); jfranck@1445: jfranck@1445: if (!chk.annotationApplicable(annoTree, on)) jfranck@1445: log.error(annoTree.pos(), "invalid.containedby.annotation.incompatible.target", targetContainerType, origAnnoType); jfranck@1445: jfranck@1445: if (!chk.validateAnnotationDeferErrors(annoTree)) jfranck@1445: log.error(annoTree.pos(), "duplicate.annotation.invalid.repeated", origAnnoType); jfranck@1445: jfranck@1313: Attribute.Compound c = enterAnnotation(annoTree, jfranck@1313: targetContainerType, jfranck@1313: ctx.env); jfranck@1464: c.setSynthesized(true); jfranck@1313: return c; jfranck@1313: } else { jfranck@1313: return null; // errors should have been reported elsewhere jfranck@1313: } jfranck@1313: } jfranck@1313: jfranck@1313: /** Fetches the actual Type that should be the containing annotation. */ jfranck@1313: private Type getContainingType(Attribute.Compound currentAnno, jfranck@1313: DiagnosticPosition pos) jfranck@1313: { jfranck@1313: Type origAnnoType = currentAnno.type; jfranck@1313: TypeSymbol origAnnoDecl = origAnnoType.tsym; jfranck@1313: jfranck@1313: // Fetch the ContainedBy annotation from the current jfranck@1313: // annotation's declaration, or null if it has none jfranck@1313: Attribute.Compound ca = origAnnoDecl.attribute(syms.containedByType.tsym); jfranck@1313: if (ca == null) { // has no ContainedBy annotation jfranck@1445: log.error(pos, "duplicate.annotation.missing.container", origAnnoType, syms.containedByType); jfranck@1313: return null; jfranck@1313: } jfranck@1313: jfranck@1313: return filterSame(extractContainingType(ca, pos, origAnnoDecl), jfranck@1313: origAnnoType); jfranck@1313: } jfranck@1313: jfranck@1313: // returns null if t is same as 's', returns 't' otherwise jfranck@1313: private Type filterSame(Type t, Type s) { jfranck@1313: if (t == null || s == null) { jfranck@1313: return t; jfranck@1313: } jfranck@1313: jfranck@1313: return types.isSameType(t, s) ? null : t; jfranck@1313: } jfranck@1313: jfranck@1313: /** Extract the actual Type to be used for a containing annotation. */ jfranck@1313: private Type extractContainingType(Attribute.Compound ca, jfranck@1313: DiagnosticPosition pos, jfranck@1313: TypeSymbol annoDecl) jfranck@1313: { jfranck@1313: // The next three checks check that the ContainedBy annotation jfranck@1313: // on the declaration of the annotation type that is repeating is jfranck@1313: // valid. jfranck@1313: jfranck@1313: // ContainedBy must have at least one element jfranck@1313: if (ca.values.isEmpty()) { jfranck@1313: log.error(pos, "invalid.containedby.annotation", annoDecl); jfranck@1313: return null; jfranck@1313: } jfranck@1313: Pair p = ca.values.head; jfranck@1313: Name name = p.fst.name; jfranck@1313: if (name != names.value) { // should contain only one element, named "value" jfranck@1313: log.error(pos, "invalid.containedby.annotation", annoDecl); jfranck@1313: return null; jfranck@1313: } jfranck@1313: if (!(p.snd instanceof Attribute.Class)) { // check that the value of "value" is an Attribute.Class jfranck@1313: log.error(pos, "invalid.containedby.annotation", annoDecl); jfranck@1313: return null; jfranck@1313: } jfranck@1313: jfranck@1313: return ((Attribute.Class)p.snd).getValue(); jfranck@1313: } jfranck@1313: jfranck@1313: /* Validate that the suggested targetContainerType Type is a valid jfranck@1313: * container type for repeated instances of originalAnnoType jfranck@1313: * annotations. Return null and report errors if this is not the jfranck@1313: * case, return the MethodSymbol of the value element in jfranck@1313: * targetContainerType if it is suitable (this is needed to jfranck@1313: * synthesize the container). */ jfranck@1313: private MethodSymbol validateContainer(Type targetContainerType, jfranck@1313: Type originalAnnoType, jfranck@1313: DiagnosticPosition pos) { jfranck@1313: MethodSymbol containerValueSymbol = null; jfranck@1313: boolean fatalError = false; jfranck@1313: jfranck@1313: // Validate that there is a (and only 1) value method jfranck@1313: Scope scope = targetContainerType.tsym.members(); jfranck@1313: int nr_value_elems = 0; jfranck@1313: boolean error = false; jfranck@1313: for(Symbol elm : scope.getElementsByName(names.value)) { jfranck@1313: nr_value_elems++; jfranck@1313: jfranck@1313: if (nr_value_elems == 1 && jfranck@1313: elm.kind == Kinds.MTH) { jfranck@1313: containerValueSymbol = (MethodSymbol)elm; jfranck@1313: } else { jfranck@1313: error = true; jfranck@1313: } jfranck@1313: } jfranck@1313: if (error) { jfranck@1313: log.error(pos, jfranck@1313: "invalid.containedby.annotation.multiple.values", jfranck@1313: targetContainerType, jfranck@1313: nr_value_elems); jfranck@1313: return null; jfranck@1313: } else if (nr_value_elems == 0) { jfranck@1313: log.error(pos, jfranck@1313: "invalid.containedby.annotation.no.value", jfranck@1313: targetContainerType); jfranck@1313: return null; jfranck@1313: } jfranck@1313: jfranck@1313: // validate that the 'value' element is a method jfranck@1313: // probably "impossible" to fail this jfranck@1313: if (containerValueSymbol.kind != Kinds.MTH) { jfranck@1313: log.error(pos, jfranck@1313: "invalid.containedby.annotation.invalid.value", jfranck@1313: targetContainerType); jfranck@1313: fatalError = true; jfranck@1313: } jfranck@1313: jfranck@1313: // validate that the 'value' element has the correct return type jfranck@1313: // i.e. array of original anno jfranck@1313: Type valueRetType = containerValueSymbol.type.getReturnType(); jfranck@1313: Type expectedType = types.makeArrayType(originalAnnoType); jfranck@1313: if (!(types.isArray(valueRetType) && jfranck@1313: types.isSameType(expectedType, valueRetType))) { jfranck@1313: log.error(pos, jfranck@1313: "invalid.containedby.annotation.value.return", jfranck@1313: targetContainerType, jfranck@1313: valueRetType, jfranck@1313: expectedType); jfranck@1313: fatalError = true; jfranck@1313: } jfranck@1313: if (error) { jfranck@1313: fatalError = true; jfranck@1313: } jfranck@1313: jfranck@1313: // Explicitly no check for/validity of @ContainerFor. That is jfranck@1313: // done on declaration of the container, and at reflect time. jfranck@1313: jfranck@1313: // The rest of the conditions for a valid containing annotation are made jfranck@1313: // in Check.validateRepeatedAnnotaton(); jfranck@1313: jfranck@1313: return fatalError ? null : containerValueSymbol; jfranck@1313: } jfranck@1313: }