Mon, 27 Aug 2012 07:21:46 -0700
7192068: (javac) provide a way for IDEs to produce Enclosing Method attributes.
Reviewed-by: jjg
Contributed-by: jan.lahoda@oracle.com
1.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java Mon Aug 20 13:50:04 2012 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java Mon Aug 27 07:21:46 2012 -0700 1.3 @@ -188,7 +188,7 @@ 1.4 1.5 /** The current input pointer. 1.6 */ 1.7 - int bp; 1.8 + protected int bp; 1.9 1.10 /** The objects of the constant pool. 1.11 */ 1.12 @@ -890,13 +890,13 @@ 1.13 1.14 protected enum AttributeKind { CLASS, MEMBER }; 1.15 protected abstract class AttributeReader { 1.16 - AttributeReader(Name name, ClassFile.Version version, Set<AttributeKind> kinds) { 1.17 + protected AttributeReader(Name name, ClassFile.Version version, Set<AttributeKind> kinds) { 1.18 this.name = name; 1.19 this.version = version; 1.20 this.kinds = kinds; 1.21 } 1.22 1.23 - boolean accepts(AttributeKind kind) { 1.24 + protected boolean accepts(AttributeKind kind) { 1.25 if (kinds.contains(kind)) { 1.26 if (majorVersion > version.major || (majorVersion == version.major && minorVersion >= version.minor)) 1.27 return true; 1.28 @@ -915,11 +915,11 @@ 1.29 return false; 1.30 } 1.31 1.32 - abstract void read(Symbol sym, int attrLen); 1.33 + protected abstract void read(Symbol sym, int attrLen); 1.34 1.35 - final Name name; 1.36 - final ClassFile.Version version; 1.37 - final Set<AttributeKind> kinds; 1.38 + protected final Name name; 1.39 + protected final ClassFile.Version version; 1.40 + protected final Set<AttributeKind> kinds; 1.41 } 1.42 1.43 protected Set<AttributeKind> CLASS_ATTRIBUTE = 1.44 @@ -936,7 +936,7 @@ 1.45 // v45.3 attributes 1.46 1.47 new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) { 1.48 - void read(Symbol sym, int attrLen) { 1.49 + protected void read(Symbol sym, int attrLen) { 1.50 if (readAllOfClassFile || saveParameterNames) 1.51 ((MethodSymbol)sym).code = readCode(sym); 1.52 else 1.53 @@ -945,7 +945,7 @@ 1.54 }, 1.55 1.56 new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) { 1.57 - void read(Symbol sym, int attrLen) { 1.58 + protected void read(Symbol sym, int attrLen) { 1.59 Object v = readPool(nextChar()); 1.60 // Ignore ConstantValue attribute if field not final. 1.61 if ((sym.flags() & FINAL) != 0) 1.62 @@ -954,13 +954,13 @@ 1.63 }, 1.64 1.65 new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { 1.66 - void read(Symbol sym, int attrLen) { 1.67 + protected void read(Symbol sym, int attrLen) { 1.68 sym.flags_field |= DEPRECATED; 1.69 } 1.70 }, 1.71 1.72 new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { 1.73 - void read(Symbol sym, int attrLen) { 1.74 + protected void read(Symbol sym, int attrLen) { 1.75 int nexceptions = nextChar(); 1.76 List<Type> thrown = List.nil(); 1.77 for (int j = 0; j < nexceptions; j++) 1.78 @@ -971,14 +971,14 @@ 1.79 }, 1.80 1.81 new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) { 1.82 - void read(Symbol sym, int attrLen) { 1.83 + protected void read(Symbol sym, int attrLen) { 1.84 ClassSymbol c = (ClassSymbol) sym; 1.85 readInnerClasses(c); 1.86 } 1.87 }, 1.88 1.89 new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { 1.90 - void read(Symbol sym, int attrLen) { 1.91 + protected void read(Symbol sym, int attrLen) { 1.92 int newbp = bp + attrLen; 1.93 if (saveParameterNames) { 1.94 // Pick up parameter names from the variable table. 1.95 @@ -1014,7 +1014,7 @@ 1.96 }, 1.97 1.98 new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) { 1.99 - void read(Symbol sym, int attrLen) { 1.100 + protected void read(Symbol sym, int attrLen) { 1.101 ClassSymbol c = (ClassSymbol) sym; 1.102 Name n = readName(nextChar()); 1.103 c.sourcefile = new SourceFileObject(n, c.flatname); 1.104 @@ -1022,7 +1022,7 @@ 1.105 }, 1.106 1.107 new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) { 1.108 - void read(Symbol sym, int attrLen) { 1.109 + protected void read(Symbol sym, int attrLen) { 1.110 // bridge methods are visible when generics not enabled 1.111 if (allowGenerics || (sym.flags_field & BRIDGE) == 0) 1.112 sym.flags_field |= SYNTHETIC; 1.113 @@ -1032,7 +1032,7 @@ 1.114 // standard v49 attributes 1.115 1.116 new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) { 1.117 - void read(Symbol sym, int attrLen) { 1.118 + protected void read(Symbol sym, int attrLen) { 1.119 int newbp = bp + attrLen; 1.120 readEnclosingMethodAttr(sym); 1.121 bp = newbp; 1.122 @@ -1041,11 +1041,11 @@ 1.123 1.124 new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) { 1.125 @Override 1.126 - boolean accepts(AttributeKind kind) { 1.127 + protected boolean accepts(AttributeKind kind) { 1.128 return super.accepts(kind) && allowGenerics; 1.129 } 1.130 1.131 - void read(Symbol sym, int attrLen) { 1.132 + protected void read(Symbol sym, int attrLen) { 1.133 if (sym.kind == TYP) { 1.134 ClassSymbol c = (ClassSymbol) sym; 1.135 readingClassAttr = true; 1.136 @@ -1074,31 +1074,31 @@ 1.137 // v49 annotation attributes 1.138 1.139 new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) { 1.140 - void read(Symbol sym, int attrLen) { 1.141 + protected void read(Symbol sym, int attrLen) { 1.142 attachAnnotationDefault(sym); 1.143 } 1.144 }, 1.145 1.146 new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { 1.147 - void read(Symbol sym, int attrLen) { 1.148 + protected void read(Symbol sym, int attrLen) { 1.149 attachAnnotations(sym); 1.150 } 1.151 }, 1.152 1.153 new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { 1.154 - void read(Symbol sym, int attrLen) { 1.155 + protected void read(Symbol sym, int attrLen) { 1.156 attachParameterAnnotations(sym); 1.157 } 1.158 }, 1.159 1.160 new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { 1.161 - void read(Symbol sym, int attrLen) { 1.162 + protected void read(Symbol sym, int attrLen) { 1.163 attachAnnotations(sym); 1.164 } 1.165 }, 1.166 1.167 new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) { 1.168 - void read(Symbol sym, int attrLen) { 1.169 + protected void read(Symbol sym, int attrLen) { 1.170 attachParameterAnnotations(sym); 1.171 } 1.172 }, 1.173 @@ -1106,14 +1106,14 @@ 1.174 // additional "legacy" v49 attributes, superceded by flags 1.175 1.176 new AttributeReader(names.Annotation, V49, CLASS_OR_MEMBER_ATTRIBUTE) { 1.177 - void read(Symbol sym, int attrLen) { 1.178 + protected void read(Symbol sym, int attrLen) { 1.179 if (allowAnnotations) 1.180 sym.flags_field |= ANNOTATION; 1.181 } 1.182 }, 1.183 1.184 new AttributeReader(names.Bridge, V49, MEMBER_ATTRIBUTE) { 1.185 - void read(Symbol sym, int attrLen) { 1.186 + protected void read(Symbol sym, int attrLen) { 1.187 sym.flags_field |= BRIDGE; 1.188 if (!allowGenerics) 1.189 sym.flags_field &= ~SYNTHETIC; 1.190 @@ -1121,13 +1121,13 @@ 1.191 }, 1.192 1.193 new AttributeReader(names.Enum, V49, CLASS_OR_MEMBER_ATTRIBUTE) { 1.194 - void read(Symbol sym, int attrLen) { 1.195 + protected void read(Symbol sym, int attrLen) { 1.196 sym.flags_field |= ENUM; 1.197 } 1.198 }, 1.199 1.200 new AttributeReader(names.Varargs, V49, CLASS_OR_MEMBER_ATTRIBUTE) { 1.201 - void read(Symbol sym, int attrLen) { 1.202 + protected void read(Symbol sym, int attrLen) { 1.203 if (allowVarargs) 1.204 sym.flags_field |= VARARGS; 1.205 } 1.206 @@ -1153,7 +1153,7 @@ 1.207 1.208 1.209 1.210 - void readEnclosingMethodAttr(Symbol sym) { 1.211 + protected void readEnclosingMethodAttr(Symbol sym) { 1.212 // sym is a nested class with an "Enclosing Method" attribute 1.213 // remove sym from it's current owners scope and place it in 1.214 // the scope specified by the attribute
2.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java Mon Aug 20 13:50:04 2012 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java Mon Aug 27 07:21:46 2012 -0700 2.3 @@ -164,7 +164,7 @@ 2.4 2.5 /** Construct a class writer, given an options table. 2.6 */ 2.7 - private ClassWriter(Context context) { 2.8 + protected ClassWriter(Context context) { 2.9 context.put(classWriterKey, this); 2.10 2.11 log = Log.instance(context); 2.12 @@ -601,12 +601,20 @@ 2.13 * Returns the number of attributes written (0 or 1). 2.14 */ 2.15 int writeEnclosingMethodAttribute(ClassSymbol c) { 2.16 - if (!target.hasEnclosingMethodAttribute() || 2.17 - c.owner.kind != MTH && // neither a local class 2.18 + if (!target.hasEnclosingMethodAttribute()) 2.19 + return 0; 2.20 + return writeEnclosingMethodAttribute(names.EnclosingMethod, c); 2.21 + } 2.22 + 2.23 + /** Write the EnclosingMethod attribute with a specified name. 2.24 + * Returns the number of attributes written (0 or 1). 2.25 + */ 2.26 + protected int writeEnclosingMethodAttribute(Name attributeName, ClassSymbol c) { 2.27 + if (c.owner.kind != MTH && // neither a local class 2.28 c.name != names.empty) // nor anonymous 2.29 return 0; 2.30 2.31 - int alenIdx = writeAttr(names.EnclosingMethod); 2.32 + int alenIdx = writeAttr(attributeName); 2.33 ClassSymbol enclClass = c.owner.enclClass(); 2.34 MethodSymbol enclMethod = 2.35 (c.owner.type == null // local to init block 2.36 @@ -1569,6 +1577,7 @@ 2.37 acount += writeFlagAttrs(c.flags()); 2.38 acount += writeJavaAnnotations(c.getAnnotationMirrors()); 2.39 acount += writeEnclosingMethodAttribute(c); 2.40 + acount += writeExtraClassAttributes(c); 2.41 2.42 poolbuf.appendInt(JAVA_MAGIC); 2.43 poolbuf.appendChar(target.minorVersion); 2.44 @@ -1588,6 +1597,14 @@ 2.45 pool = c.pool = null; // to conserve space 2.46 } 2.47 2.48 + /**Allows subclasses to write additional class attributes 2.49 + * 2.50 + * @return the number of attributes written 2.51 + */ 2.52 + protected int writeExtraClassAttributes(ClassSymbol c) { 2.53 + return 0; 2.54 + } 2.55 + 2.56 int adjustFlags(final long flags) { 2.57 int result = (int)flags; 2.58 if ((flags & SYNTHETIC) != 0 && !target.useSyntheticFlag())