6854796: update JSR308 impl with latest code from type-annotations repo

Co-authored-by: Mahmood Ali <mali@csail.mit.edu>
Co-authored-by: Matt Papi <mpapi@csail.mit.edu>
Reviewed-by: jjg, mcimadamore, darcy
This commit is contained in:
Michael Ernst 2009-06-26 19:47:12 -07:00 committed by Jonathan Gibbons
parent e03ee9130a
commit d6b7dc0812
10 changed files with 70 additions and 34 deletions

View File

@ -53,25 +53,25 @@ public enum TargetType {
// //
/** For annotations on typecasts. */ /** For annotations on typecasts. */
TYPECAST(0x00), TYPECAST(0x00, IsLocal),
/** For annotations on a type argument or nested array of a typecast. */ /** For annotations on a type argument or nested array of a typecast. */
TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation), TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation, IsLocal),
/** For annotations on type tests. */ /** For annotations on type tests. */
INSTANCEOF(0x02), INSTANCEOF(0x02, IsLocal),
/** For annotations on a type argument or nested array of a type test. */ /** For annotations on a type argument or nested array of a type test. */
INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation), INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation, IsLocal),
/** For annotations on object creation expressions. */ /** For annotations on object creation expressions. */
NEW(0x04), NEW(0x04, IsLocal),
/** /**
* For annotations on a type argument or nested array of an object creation * For annotations on a type argument or nested array of an object creation
* expression. * expression.
*/ */
NEW_GENERIC_OR_ARRAY(0x05, HasLocation), NEW_GENERIC_OR_ARRAY(0x05, HasLocation, IsLocal),
/** For annotations on the method receiver. */ /** For annotations on the method receiver. */
@ -81,10 +81,10 @@ public enum TargetType {
//@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation), //@Deprecated METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
/** For annotations on local variables. */ /** For annotations on local variables. */
LOCAL_VARIABLE(0x08), LOCAL_VARIABLE(0x08, IsLocal),
/** For annotations on a type argument or nested array of a local. */ /** For annotations on a type argument or nested array of a local. */
LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation), LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation, IsLocal),
// handled by regular annotations // handled by regular annotations
//@Deprecated METHOD_RETURN(0x0A), //@Deprecated METHOD_RETURN(0x0A),
@ -138,17 +138,17 @@ public enum TargetType {
//@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation), //@Deprecated THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
/** For annotations in type arguments of object creation expressions. */ /** For annotations in type arguments of object creation expressions. */
NEW_TYPE_ARGUMENT(0x18), NEW_TYPE_ARGUMENT(0x18, IsLocal),
NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation), NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation, IsLocal),
METHOD_TYPE_ARGUMENT(0x1A), METHOD_TYPE_ARGUMENT(0x1A, IsLocal),
METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation), METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation, IsLocal),
WILDCARD_BOUND(0x1C, HasBound), WILDCARD_BOUND(0x1C, HasBound),
WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation), WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
CLASS_LITERAL(0x1E), CLASS_LITERAL(0x1E, IsLocal),
CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation), CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation, IsLocal),
METHOD_TYPE_PARAMETER(0x20, HasParameter), METHOD_TYPE_PARAMETER(0x20, HasParameter),
@ -218,6 +218,17 @@ public enum TargetType {
return flags.contains(HasBound); return flags.contains(HasBound);
} }
/**
* Returns whether or not this TargetType represents an annotation whose
* target is exclusively a tree in a method body
*
* Note: wildcard bound targets could target a local tree and a class
* member declaration signature tree
*/
public boolean isLocal() {
return flags.contains(IsLocal);
}
public int targetTypeValue() { public int targetTypeValue() {
return this.targetTypeValue; return this.targetTypeValue;
} }
@ -261,6 +272,6 @@ public enum TargetType {
} }
static enum TargetAttribute { static enum TargetAttribute {
HasLocation, HasParameter, HasBound; HasLocation, HasParameter, HasBound, IsLocal;
} }
} }

View File

@ -45,6 +45,7 @@ public class TypeAnnotationPosition {
public int pos = -1; public int pos = -1;
// For typecasts, type tests, new (and locals, as start_pc). // For typecasts, type tests, new (and locals, as start_pc).
public boolean isValidOffset = false;
public int offset = -1; public int offset = -1;
// For locals. arrays same length // For locals. arrays same length
@ -177,4 +178,17 @@ public class TypeAnnotationPosition {
sb.append(']'); sb.append(']');
return sb.toString(); return sb.toString();
} }
/**
* Indicates whether the target tree of the annotation has been optimized
* away from classfile or not.
* @return true if the target has not been optimized away
*/
public boolean emitToClassfile() {
if (type == TargetType.WILDCARD_BOUND
|| type == TargetType.WILDCARD_BOUND_GENERIC_OR_ARRAY)
return wildcard_position.isValidOffset;
else
return !type.isLocal() || isValidOffset;
}
} }

View File

@ -1979,7 +1979,6 @@ public class Lower extends TreeTranslator {
c.members_field = new Scope(c); c.members_field = new Scope(c);
c.flags_field = flags; c.flags_field = flags;
c.attributes_field = tree.packge.attributes_field; c.attributes_field = tree.packge.attributes_field;
tree.packge.attributes_field = List.nil();
ClassType ctype = (ClassType) c.type; ClassType ctype = (ClassType) c.type;
ctype.supertype_field = syms.objectType; ctype.supertype_field = syms.objectType;
ctype.interfaces_field = List.nil(); ctype.interfaces_field = List.nil();

View File

@ -774,11 +774,14 @@ public class ClassWriter extends ClassFile {
ListBuffer<Attribute.TypeCompound> invisibles = ListBuffer.lb(); ListBuffer<Attribute.TypeCompound> invisibles = ListBuffer.lb();
for (Attribute.TypeCompound tc : typeAnnos) { for (Attribute.TypeCompound tc : typeAnnos) {
switch (getRetention(tc.type.tsym)) { if (tc.position.type == TargetType.UNKNOWN
case SOURCE: break; || !tc.position.emitToClassfile())
case CLASS: invisibles.append(tc); break; continue;
case RUNTIME: visibles.append(tc); break; switch (getRetention(tc.type.tsym)) {
default: ;// /* fail soft */ throw new AssertionError(vis); case SOURCE: break;
case CLASS: invisibles.append(tc); break;
case RUNTIME: visibles.append(tc); break;
default: ;// /* fail soft */ throw new AssertionError(vis);
} }
} }
@ -905,12 +908,11 @@ public class ClassWriter extends ClassFile {
} }
void writeTypeAnnotation(Attribute.TypeCompound c) { void writeTypeAnnotation(Attribute.TypeCompound c) {
// ignore UNKNOWN attributes - improve testing if (debugJSR308)
if (debugJSR308) System.out.println("TA: writing " + c + " at " + c.position
System.out.println("TA: writing " + c + " at " + c.position + " in " + log.currentSourceFile());
+ " in " + log.currentSourceFile()); writeCompoundAttribute(c);
writeCompoundAttribute(c); writePosition(c.position);
writePosition(c.position);
} }
void writePosition(TypeAnnotationPosition p) { void writePosition(TypeAnnotationPosition p) {

View File

@ -1912,8 +1912,8 @@ public class Code {
if (length < Character.MAX_VALUE) { if (length < Character.MAX_VALUE) {
v.length = length; v.length = length;
putVar(v); putVar(v);
fillLocalVarPosition(v);
} }
fillLocalVarPosition(v);
} }
} }
state.defined.excl(adr); state.defined.excl(adr);
@ -1929,6 +1929,7 @@ public class Code {
p.lvarOffset[0] = (int)lv.start_pc; p.lvarOffset[0] = (int)lv.start_pc;
p.lvarLength[0] = (int)lv.length; p.lvarLength[0] = (int)lv.length;
p.lvarIndex[0] = (int)lv.reg; p.lvarIndex[0] = (int)lv.reg;
p.isValidOffset = true;
p = p.wildcard_position; p = p.wildcard_position;
} }
} }

View File

@ -1715,6 +1715,7 @@ public class Gen extends JCTree.Visitor {
if (ta.position.pos == treePos) { if (ta.position.pos == treePos) {
ta.position.offset = code.cp; ta.position.offset = code.cp;
ta.position.lvarOffset[0] = code.cp; ta.position.lvarOffset[0] = code.cp;
ta.position.isValidOffset = true;
} }
} }
@ -1726,6 +1727,7 @@ public class Gen extends JCTree.Visitor {
if (ta.position.pos == treePos) { if (ta.position.pos == treePos) {
ta.position.offset = code.cp; ta.position.offset = code.cp;
ta.position.lvarOffset[0] = code.cp; ta.position.lvarOffset[0] = code.cp;
ta.position.isValidOffset = true;
} }
} }
@ -1737,6 +1739,7 @@ public class Gen extends JCTree.Visitor {
if (ta.position.pos == treePos) { if (ta.position.pos == treePos) {
ta.position.offset = code.cp; ta.position.offset = code.cp;
ta.position.lvarOffset[0] = code.cp; ta.position.lvarOffset[0] = code.cp;
ta.position.isValidOffset = true;
} }
} }
} }

View File

@ -477,7 +477,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
public Todo todo; public Todo todo;
/** Ordered list of compiler phases for each compilation unit. */ /** Ordered list of compiler phases for each compilation unit. */
protected enum CompileState { public enum CompileState {
PARSE(1), PARSE(1),
ENTER(2), ENTER(2),
PROCESS(3), PROCESS(3),

View File

@ -1211,7 +1211,6 @@ public class JavacParser implements Parser {
} }
} else { } else {
if (!annos.isEmpty()) { if (!annos.isEmpty()) {
illegal(0);
if (permitTypeAnnotationsPushBack) if (permitTypeAnnotationsPushBack)
typeAnnotationsPushedBack = annos; typeAnnotationsPushedBack = annos;
else else

View File

@ -50,6 +50,7 @@ import javax.tools.StandardJavaFileManager;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.DiagnosticListener; import javax.tools.DiagnosticListener;
import com.sun.source.util.AbstractTypeProcessor;
import com.sun.source.util.TaskEvent; import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener; import com.sun.source.util.TaskListener;
import com.sun.tools.javac.api.JavacTaskImpl; import com.sun.tools.javac.api.JavacTaskImpl;
@ -58,6 +59,7 @@ import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.file.JavacFileManager; import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.jvm.*; import com.sun.tools.javac.jvm.*;
import com.sun.tools.javac.main.JavaCompiler; import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.main.JavaCompiler.CompileState;
import com.sun.tools.javac.model.JavacElements; import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.model.JavacTypes; import com.sun.tools.javac.model.JavacTypes;
import com.sun.tools.javac.parser.*; import com.sun.tools.javac.parser.*;
@ -93,6 +95,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
private final boolean lint; private final boolean lint;
private final boolean procOnly; private final boolean procOnly;
private final boolean fatalErrors; private final boolean fatalErrors;
private boolean foundTypeProcessors;
private final JavacFiler filer; private final JavacFiler filer;
private final JavacMessager messager; private final JavacMessager messager;
@ -153,6 +156,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
options.get("-Xprint") != null; options.get("-Xprint") != null;
fatalErrors = options.get("fatalEnterError") != null; fatalErrors = options.get("fatalEnterError") != null;
platformAnnotations = initPlatformAnnotations(); platformAnnotations = initPlatformAnnotations();
foundTypeProcessors = false;
// Initialize services before any processors are initialzied // Initialize services before any processors are initialzied
// in case processors use them. // in case processors use them.
@ -670,6 +674,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
} }
if (matchedNames.size() > 0 || ps.contributed) { if (matchedNames.size() > 0 || ps.contributed) {
foundTypeProcessors = foundTypeProcessors || (ps.processor instanceof AbstractTypeProcessor);
boolean processingResult = callProcessor(ps.processor, typeElements, renv); boolean processingResult = callProcessor(ps.processor, typeElements, renv);
ps.contributed = true; ps.contributed = true;
ps.removeSupportedOptions(unmatchedProcessorOptions); ps.removeSupportedOptions(unmatchedProcessorOptions);
@ -916,7 +921,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
compiler.log.nerrors += messager.errorCount(); compiler.log.nerrors += messager.errorCount();
if (compiler.errorCount() == 0) if (compiler.errorCount() == 0)
compiler.log.nerrors++; compiler.log.nerrors++;
} else if (procOnly) { } else if (procOnly && !foundTypeProcessors) {
compiler.todo.clear(); compiler.todo.clear();
} else { // Final compilation } else { // Final compilation
compiler.close(false); compiler.close(false);
@ -924,6 +929,8 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
this.context = currentContext; this.context = currentContext;
updateProcessingState(currentContext, true); updateProcessingState(currentContext, true);
compiler = JavaCompiler.instance(currentContext); compiler = JavaCompiler.instance(currentContext);
if (procOnly && foundTypeProcessors)
compiler.shouldStopPolicy = CompileState.FLOW;
if (true) { if (true) {
compiler.enterTrees(cleanTrees(roots)); compiler.enterTrees(cleanTrees(roots));

View File

@ -37,6 +37,9 @@ import java.util.*;
/** /**
* Object providing state about a prior round of annotation processing. * Object providing state about a prior round of annotation processing.
* *
* <p>The methods in this class do not take type annotations into account,
* as target types, not java elements.
*
* <p><b>This is NOT part of any API supported by Sun Microsystems. * <p><b>This is NOT part of any API supported by Sun Microsystems.
* If you write code that depends on this, you do so at your own risk. * If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or * This code and its internal interfaces are subject to change or
@ -105,9 +108,6 @@ public class JavacRoundEnvironment implements RoundEnvironment {
* elements are {@linkplain #getSpecifiedTypeElements specified * elements are {@linkplain #getSpecifiedTypeElements specified
* types} and any types nested within them. * types} and any types nested within them.
* *
* <p>This method will not return type annotations, which annotate
* types, not elements.
*
* @param a annotation type being requested * @param a annotation type being requested
* @return the elements annotated with the given annotation type, * @return the elements annotated with the given annotation type,
* or an empty set if there are none * or an empty set if there are none