< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java

Print this page

        

*** 34,54 **** import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; - import javax.tools.JavaFileManager; import javax.tools.JavaFileObject; ! import com.sun.tools.javac.code.*; import com.sun.tools.javac.code.Lint.LintCategory; import com.sun.tools.javac.code.Scope.WriteableScope; import com.sun.tools.javac.code.Symbol.*; import com.sun.tools.javac.code.Symtab; import com.sun.tools.javac.code.Type.*; ! import com.sun.tools.javac.comp.Annotate; import com.sun.tools.javac.file.BaseFileObject; import com.sun.tools.javac.jvm.ClassFile.NameAndType; import com.sun.tools.javac.jvm.ClassFile.Version; import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.DefinedBy.Api; --- 34,54 ---- import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.tools.JavaFileManager; import javax.tools.JavaFileObject; ! import com.sun.tools.javac.comp.Annotate; ! import com.sun.tools.javac.comp.Annotate.AnnotationTypeCompleter; import com.sun.tools.javac.code.*; import com.sun.tools.javac.code.Lint.LintCategory; import com.sun.tools.javac.code.Scope.WriteableScope; import com.sun.tools.javac.code.Symbol.*; import com.sun.tools.javac.code.Symtab; import com.sun.tools.javac.code.Type.*; ! import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata; import com.sun.tools.javac.file.BaseFileObject; import com.sun.tools.javac.jvm.ClassFile.NameAndType; import com.sun.tools.javac.jvm.ClassFile.Version; import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.DefinedBy.Api;
*** 78,88 **** /** The context key for the class reader. */ protected static final Context.Key<ClassReader> classReaderKey = new Context.Key<>(); public static final int INITIAL_BUFFER_SIZE = 0x0fff0; ! Annotate annotate; /** Switch: verbose output. */ boolean verbose; --- 78,88 ---- /** The context key for the class reader. */ protected static final Context.Key<ClassReader> classReaderKey = new Context.Key<>(); public static final int INITIAL_BUFFER_SIZE = 0x0fff0; ! private final Annotate annotate; /** Switch: verbose output. */ boolean verbose;
*** 187,196 **** --- 187,208 ---- /** * The set of attribute names for which warnings have been generated for the current class */ Set<Name> warnedAttrs = new HashSet<>(); + /** + * The prototype @Target Attribute.Compound if this class is an annotation annotated with + * @Target + */ + CompoundAnnotationProxy target; + + /** + * The prototype @Repetable Attribute.Compound if this class is an annotation annotated with + * @Repeatable + */ + CompoundAnnotationProxy repeatable; + /** Get the ClassReader instance for this invocation. */ public static ClassReader instance(Context context) { ClassReader instance = context.get(classReaderKey); if (instance == null) instance = new ClassReader(context);
*** 198,207 **** --- 210,220 ---- } /** Construct a new class reader. */ protected ClassReader(Context context) { context.put(classReaderKey, this); + annotate = Annotate.instance(context); names = Names.instance(context); syms = Symtab.instance(context); types = Types.instance(context); fileManager = context.get(JavaFileManager.class); if (fileManager == null)
*** 209,219 **** diagFactory = JCDiagnostic.Factory.instance(context); log = Log.instance(context); Options options = Options.instance(context); - annotate = Annotate.instance(context); verbose = options.isSet(VERBOSE); checkClassFile = options.isSet("-checkclassfile"); Source source = Source.instance(context); allowSimplifiedVarargs = source.allowSimplifiedVarargs(); --- 222,231 ----
*** 1301,1310 **** --- 1313,1329 ---- int numAttributes = nextChar(); if (numAttributes != 0) { ListBuffer<CompoundAnnotationProxy> proxies = new ListBuffer<>(); for (int i = 0; i<numAttributes; i++) { CompoundAnnotationProxy proxy = readCompoundAnnotation(); + + if (proxy.type.tsym == syms.annotationTargetType.tsym) { + target = proxy; + } else if (proxy.type.tsym == syms.repeatableType.tsym) { + repeatable = proxy; + } + proxies.append(proxy); } annotate.normal(new AnnotationCompleter(sym, proxies.toList())); } }
*** 1702,1713 **** this.position = position; } } class AnnotationDeproxy implements ProxyVisitor { ! private ClassSymbol requestingOwner = currentOwner.kind == MTH ! ? currentOwner.enclClass() : (ClassSymbol)currentOwner; List<Attribute.Compound> deproxyCompoundList(List<CompoundAnnotationProxy> pl) { // also must fill in types!!!! ListBuffer<Attribute.Compound> buf = new ListBuffer<>(); for (List<CompoundAnnotationProxy> l = pl; l.nonEmpty(); l=l.tail) { --- 1721,1735 ---- this.position = position; } } class AnnotationDeproxy implements ProxyVisitor { ! private ClassSymbol requestingOwner; ! ! AnnotationDeproxy(ClassSymbol owner) { ! this.requestingOwner = owner; ! } List<Attribute.Compound> deproxyCompoundList(List<CompoundAnnotationProxy> pl) { // also must fill in types!!!! ListBuffer<Attribute.Compound> buf = new ListBuffer<>(); for (List<CompoundAnnotationProxy> l = pl; l.nonEmpty(); l=l.tail) {
*** 1852,1874 **** public void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy) { result = deproxyCompound(proxy); } } ! class AnnotationDefaultCompleter extends AnnotationDeproxy implements Annotate.Worker { final MethodSymbol sym; final Attribute value; final JavaFileObject classFile = currentClassFile; ! @Override ! public String toString() { ! return " ClassReader store default for " + sym.owner + "." + sym + " is " + value; ! } AnnotationDefaultCompleter(MethodSymbol sym, Attribute value) { this.sym = sym; this.value = value; } ! // implement Annotate.Worker.run() public void run() { JavaFileObject previousClassFile = currentClassFile; try { // Reset the interim value set earlier in // attachAnnotationDefault(). --- 1874,1896 ---- public void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy) { result = deproxyCompound(proxy); } } ! class AnnotationDefaultCompleter extends AnnotationDeproxy implements Runnable { final MethodSymbol sym; final Attribute value; final JavaFileObject classFile = currentClassFile; ! AnnotationDefaultCompleter(MethodSymbol sym, Attribute value) { + super(currentOwner.kind == MTH + ? currentOwner.enclClass() : (ClassSymbol)currentOwner); this.sym = sym; this.value = value; } ! ! @Override public void run() { JavaFileObject previousClassFile = currentClassFile; try { // Reset the interim value set earlier in // attachAnnotationDefault().
*** 1877,1902 **** sym.defaultValue = deproxy(sym.type.getReturnType(), value); } finally { currentClassFile = previousClassFile; } } } ! class AnnotationCompleter extends AnnotationDeproxy implements Annotate.Worker { final Symbol sym; final List<CompoundAnnotationProxy> l; final JavaFileObject classFile; ! @Override ! public String toString() { ! return " ClassReader annotate " + sym.owner + "." + sym + " with " + l; ! } AnnotationCompleter(Symbol sym, List<CompoundAnnotationProxy> l) { this.sym = sym; this.l = l; this.classFile = currentClassFile; } ! // implement Annotate.Worker.run() public void run() { JavaFileObject previousClassFile = currentClassFile; try { currentClassFile = classFile; List<Attribute.Compound> newList = deproxyCompoundList(l); --- 1899,1929 ---- sym.defaultValue = deproxy(sym.type.getReturnType(), value); } finally { currentClassFile = previousClassFile; } } + + @Override + public String toString() { + return " ClassReader store default for " + sym.owner + "." + sym + " is " + value; + } } ! class AnnotationCompleter extends AnnotationDeproxy implements Runnable { final Symbol sym; final List<CompoundAnnotationProxy> l; final JavaFileObject classFile; ! AnnotationCompleter(Symbol sym, List<CompoundAnnotationProxy> l) { + super(currentOwner.kind == MTH + ? currentOwner.enclClass() : (ClassSymbol)currentOwner); this.sym = sym; this.l = l; this.classFile = currentClassFile; } ! ! @Override public void run() { JavaFileObject previousClassFile = currentClassFile; try { currentClassFile = classFile; List<Attribute.Compound> newList = deproxyCompoundList(l);
*** 1907,1916 **** --- 1934,1948 ---- } } finally { currentClassFile = previousClassFile; } } + + @Override + public String toString() { + return " ClassReader annotate " + sym.owner + "." + sym + " with " + l; + } } class TypeAnnotationCompleter extends AnnotationCompleter { List<TypeAnnotationProxy> proxies;
*** 2286,2295 **** --- 2318,2329 ---- public void readClassFile(ClassSymbol c) { currentOwner = c; currentClassFile = c.classfile; warnedAttrs.clear(); filling = true; + target = null; + repeatable = null; try { bp = 0; buf = readInputStream(buf, c.classfile.openInputStream()); readClassBuffer(c); if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
*** 2306,2315 **** --- 2340,2355 ---- } else if (missingTypeVariables.isEmpty() != foundTypeVariables.isEmpty()) { Name name = missingTypeVariables.head.tsym.name; throw badClassFile("undecl.type.var", name); } + + if ((c.flags_field & Flags.ANNOTATION) != 0) { + c.setAnnotationTypeMetadata(new AnnotationTypeMetadata(c, new CompleterDeproxy(c, target, repeatable))); + } else { + c.setAnnotationTypeMetadata(AnnotationTypeMetadata.notAnAnnotationType()); + } } catch (IOException ex) { throw badClassFile("unable.to.access.file", ex.getMessage()); } catch (ArrayIndexOutOfBoundsException ex) { throw badClassFile("bad.class.file", c.flatname); } finally {
*** 2503,2508 **** --- 2543,2586 ---- @Override public int hashCode() { return name.hashCode(); } } + + private class CompleterDeproxy implements AnnotationTypeCompleter { + ClassSymbol proxyOn; + CompoundAnnotationProxy target; + CompoundAnnotationProxy repeatable; + + public CompleterDeproxy(ClassSymbol c, CompoundAnnotationProxy target, + CompoundAnnotationProxy repeatable) + { + this.proxyOn = c; + this.target = target; + this.repeatable = repeatable; + } + + @Override + public void complete(ClassSymbol sym) { + Assert.check(proxyOn == sym); + Attribute.Compound theTarget = null, theRepeatable = null; + AnnotationDeproxy deproxy; + + try { + if (target != null) { + deproxy = new AnnotationDeproxy(proxyOn); + theTarget = deproxy.deproxyCompound(target); + } + + if (repeatable != null) { + deproxy = new AnnotationDeproxy(proxyOn); + theRepeatable = deproxy.deproxyCompound(repeatable); + } + } catch (Exception e) { + throw new CompletionFailure(sym, e.getMessage()); + } + + sym.getAnnotationTypeMetadata().setTarget(theTarget); + sym.getAnnotationTypeMetadata().setRepeatable(theRepeatable); + } + } }
< prev index next >