提交 1992a1e5 编写于 作者: K ksrini

7192068: (javac) provide a way for IDEs to produce Enclosing Method attributes.

Reviewed-by: jjg
Contributed-by: jan.lahoda@oracle.com
上级 cb2fc295
...@@ -188,7 +188,7 @@ public class ClassReader implements Completer { ...@@ -188,7 +188,7 @@ public class ClassReader implements Completer {
/** The current input pointer. /** The current input pointer.
*/ */
int bp; protected int bp;
/** The objects of the constant pool. /** The objects of the constant pool.
*/ */
...@@ -890,13 +890,13 @@ public class ClassReader implements Completer { ...@@ -890,13 +890,13 @@ public class ClassReader implements Completer {
protected enum AttributeKind { CLASS, MEMBER }; protected enum AttributeKind { CLASS, MEMBER };
protected abstract class AttributeReader { protected abstract class AttributeReader {
AttributeReader(Name name, ClassFile.Version version, Set<AttributeKind> kinds) { protected AttributeReader(Name name, ClassFile.Version version, Set<AttributeKind> kinds) {
this.name = name; this.name = name;
this.version = version; this.version = version;
this.kinds = kinds; this.kinds = kinds;
} }
boolean accepts(AttributeKind kind) { protected boolean accepts(AttributeKind kind) {
if (kinds.contains(kind)) { if (kinds.contains(kind)) {
if (majorVersion > version.major || (majorVersion == version.major && minorVersion >= version.minor)) if (majorVersion > version.major || (majorVersion == version.major && minorVersion >= version.minor))
return true; return true;
...@@ -915,11 +915,11 @@ public class ClassReader implements Completer { ...@@ -915,11 +915,11 @@ public class ClassReader implements Completer {
return false; return false;
} }
abstract void read(Symbol sym, int attrLen); protected abstract void read(Symbol sym, int attrLen);
final Name name; protected final Name name;
final ClassFile.Version version; protected final ClassFile.Version version;
final Set<AttributeKind> kinds; protected final Set<AttributeKind> kinds;
} }
protected Set<AttributeKind> CLASS_ATTRIBUTE = protected Set<AttributeKind> CLASS_ATTRIBUTE =
...@@ -936,7 +936,7 @@ public class ClassReader implements Completer { ...@@ -936,7 +936,7 @@ public class ClassReader implements Completer {
// v45.3 attributes // v45.3 attributes
new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) { new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
if (readAllOfClassFile || saveParameterNames) if (readAllOfClassFile || saveParameterNames)
((MethodSymbol)sym).code = readCode(sym); ((MethodSymbol)sym).code = readCode(sym);
else else
...@@ -945,7 +945,7 @@ public class ClassReader implements Completer { ...@@ -945,7 +945,7 @@ public class ClassReader implements Completer {
}, },
new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) { new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
Object v = readPool(nextChar()); Object v = readPool(nextChar());
// Ignore ConstantValue attribute if field not final. // Ignore ConstantValue attribute if field not final.
if ((sym.flags() & FINAL) != 0) if ((sym.flags() & FINAL) != 0)
...@@ -954,13 +954,13 @@ public class ClassReader implements Completer { ...@@ -954,13 +954,13 @@ public class ClassReader implements Completer {
}, },
new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
sym.flags_field |= DEPRECATED; sym.flags_field |= DEPRECATED;
} }
}, },
new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
int nexceptions = nextChar(); int nexceptions = nextChar();
List<Type> thrown = List.nil(); List<Type> thrown = List.nil();
for (int j = 0; j < nexceptions; j++) for (int j = 0; j < nexceptions; j++)
...@@ -971,14 +971,14 @@ public class ClassReader implements Completer { ...@@ -971,14 +971,14 @@ public class ClassReader implements Completer {
}, },
new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) { new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
ClassSymbol c = (ClassSymbol) sym; ClassSymbol c = (ClassSymbol) sym;
readInnerClasses(c); readInnerClasses(c);
} }
}, },
new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
int newbp = bp + attrLen; int newbp = bp + attrLen;
if (saveParameterNames) { if (saveParameterNames) {
// Pick up parameter names from the variable table. // Pick up parameter names from the variable table.
...@@ -1014,7 +1014,7 @@ public class ClassReader implements Completer { ...@@ -1014,7 +1014,7 @@ public class ClassReader implements Completer {
}, },
new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) { new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
ClassSymbol c = (ClassSymbol) sym; ClassSymbol c = (ClassSymbol) sym;
Name n = readName(nextChar()); Name n = readName(nextChar());
c.sourcefile = new SourceFileObject(n, c.flatname); c.sourcefile = new SourceFileObject(n, c.flatname);
...@@ -1022,7 +1022,7 @@ public class ClassReader implements Completer { ...@@ -1022,7 +1022,7 @@ public class ClassReader implements Completer {
}, },
new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
// bridge methods are visible when generics not enabled // bridge methods are visible when generics not enabled
if (allowGenerics || (sym.flags_field & BRIDGE) == 0) if (allowGenerics || (sym.flags_field & BRIDGE) == 0)
sym.flags_field |= SYNTHETIC; sym.flags_field |= SYNTHETIC;
...@@ -1032,7 +1032,7 @@ public class ClassReader implements Completer { ...@@ -1032,7 +1032,7 @@ public class ClassReader implements Completer {
// standard v49 attributes // standard v49 attributes
new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) { new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
int newbp = bp + attrLen; int newbp = bp + attrLen;
readEnclosingMethodAttr(sym); readEnclosingMethodAttr(sym);
bp = newbp; bp = newbp;
...@@ -1041,11 +1041,11 @@ public class ClassReader implements Completer { ...@@ -1041,11 +1041,11 @@ public class ClassReader implements Completer {
new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
@Override @Override
boolean accepts(AttributeKind kind) { protected boolean accepts(AttributeKind kind) {
return super.accepts(kind) && allowGenerics; return super.accepts(kind) && allowGenerics;
} }
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
if (sym.kind == TYP) { if (sym.kind == TYP) {
ClassSymbol c = (ClassSymbol) sym; ClassSymbol c = (ClassSymbol) sym;
readingClassAttr = true; readingClassAttr = true;
...@@ -1074,31 +1074,31 @@ public class ClassReader implements Completer { ...@@ -1074,31 +1074,31 @@ public class ClassReader implements Completer {
// v49 annotation attributes // v49 annotation attributes
new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
attachAnnotationDefault(sym); attachAnnotationDefault(sym);
} }
}, },
new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
attachAnnotations(sym); attachAnnotations(sym);
} }
}, },
new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
attachParameterAnnotations(sym); attachParameterAnnotations(sym);
} }
}, },
new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
attachAnnotations(sym); attachAnnotations(sym);
} }
}, },
new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
attachParameterAnnotations(sym); attachParameterAnnotations(sym);
} }
}, },
...@@ -1106,14 +1106,14 @@ public class ClassReader implements Completer { ...@@ -1106,14 +1106,14 @@ public class ClassReader implements Completer {
// additional "legacy" v49 attributes, superceded by flags // additional "legacy" v49 attributes, superceded by flags
new AttributeReader(names.Annotation, V49, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.Annotation, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
if (allowAnnotations) if (allowAnnotations)
sym.flags_field |= ANNOTATION; sym.flags_field |= ANNOTATION;
} }
}, },
new AttributeReader(names.Bridge, V49, MEMBER_ATTRIBUTE) { new AttributeReader(names.Bridge, V49, MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
sym.flags_field |= BRIDGE; sym.flags_field |= BRIDGE;
if (!allowGenerics) if (!allowGenerics)
sym.flags_field &= ~SYNTHETIC; sym.flags_field &= ~SYNTHETIC;
...@@ -1121,13 +1121,13 @@ public class ClassReader implements Completer { ...@@ -1121,13 +1121,13 @@ public class ClassReader implements Completer {
}, },
new AttributeReader(names.Enum, V49, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.Enum, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
sym.flags_field |= ENUM; sym.flags_field |= ENUM;
} }
}, },
new AttributeReader(names.Varargs, V49, CLASS_OR_MEMBER_ATTRIBUTE) { new AttributeReader(names.Varargs, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
void read(Symbol sym, int attrLen) { protected void read(Symbol sym, int attrLen) {
if (allowVarargs) if (allowVarargs)
sym.flags_field |= VARARGS; sym.flags_field |= VARARGS;
} }
...@@ -1153,7 +1153,7 @@ public class ClassReader implements Completer { ...@@ -1153,7 +1153,7 @@ public class ClassReader implements Completer {
void readEnclosingMethodAttr(Symbol sym) { protected void readEnclosingMethodAttr(Symbol sym) {
// sym is a nested class with an "Enclosing Method" attribute // sym is a nested class with an "Enclosing Method" attribute
// remove sym from it's current owners scope and place it in // remove sym from it's current owners scope and place it in
// the scope specified by the attribute // the scope specified by the attribute
......
...@@ -164,7 +164,7 @@ public class ClassWriter extends ClassFile { ...@@ -164,7 +164,7 @@ public class ClassWriter extends ClassFile {
/** Construct a class writer, given an options table. /** Construct a class writer, given an options table.
*/ */
private ClassWriter(Context context) { protected ClassWriter(Context context) {
context.put(classWriterKey, this); context.put(classWriterKey, this);
log = Log.instance(context); log = Log.instance(context);
...@@ -601,12 +601,20 @@ public class ClassWriter extends ClassFile { ...@@ -601,12 +601,20 @@ public class ClassWriter extends ClassFile {
* Returns the number of attributes written (0 or 1). * Returns the number of attributes written (0 or 1).
*/ */
int writeEnclosingMethodAttribute(ClassSymbol c) { int writeEnclosingMethodAttribute(ClassSymbol c) {
if (!target.hasEnclosingMethodAttribute() || if (!target.hasEnclosingMethodAttribute())
c.owner.kind != MTH && // neither a local class return 0;
return writeEnclosingMethodAttribute(names.EnclosingMethod, c);
}
/** Write the EnclosingMethod attribute with a specified name.
* Returns the number of attributes written (0 or 1).
*/
protected int writeEnclosingMethodAttribute(Name attributeName, ClassSymbol c) {
if (c.owner.kind != MTH && // neither a local class
c.name != names.empty) // nor anonymous c.name != names.empty) // nor anonymous
return 0; return 0;
int alenIdx = writeAttr(names.EnclosingMethod); int alenIdx = writeAttr(attributeName);
ClassSymbol enclClass = c.owner.enclClass(); ClassSymbol enclClass = c.owner.enclClass();
MethodSymbol enclMethod = MethodSymbol enclMethod =
(c.owner.type == null // local to init block (c.owner.type == null // local to init block
...@@ -1569,6 +1577,7 @@ public class ClassWriter extends ClassFile { ...@@ -1569,6 +1577,7 @@ public class ClassWriter extends ClassFile {
acount += writeFlagAttrs(c.flags()); acount += writeFlagAttrs(c.flags());
acount += writeJavaAnnotations(c.getAnnotationMirrors()); acount += writeJavaAnnotations(c.getAnnotationMirrors());
acount += writeEnclosingMethodAttribute(c); acount += writeEnclosingMethodAttribute(c);
acount += writeExtraClassAttributes(c);
poolbuf.appendInt(JAVA_MAGIC); poolbuf.appendInt(JAVA_MAGIC);
poolbuf.appendChar(target.minorVersion); poolbuf.appendChar(target.minorVersion);
...@@ -1588,6 +1597,14 @@ public class ClassWriter extends ClassFile { ...@@ -1588,6 +1597,14 @@ public class ClassWriter extends ClassFile {
pool = c.pool = null; // to conserve space pool = c.pool = null; // to conserve space
} }
/**Allows subclasses to write additional class attributes
*
* @return the number of attributes written
*/
protected int writeExtraClassAttributes(ClassSymbol c) {
return 0;
}
int adjustFlags(final long flags) { int adjustFlags(final long flags) {
int result = (int)flags; int result = (int)flags;
if ((flags & SYNTHETIC) != 0 && !target.useSyntheticFlag()) if ((flags & SYNTHETIC) != 0 && !target.useSyntheticFlag())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册