diff --git a/classfile/apichanges.xml b/classfile/apichanges.xml --- a/classfile/apichanges.xml +++ b/classfile/apichanges.xml @@ -107,6 +107,20 @@ + + + Support for Java 7 classfile enhancements. + + + + + + Java 7 added a several new constant pool entry types to support the + invokeDynamic instruction. These need to be modeled by the library. + + + + Support for Java 6 classfile enhancements. diff --git a/classfile/manifest.mf b/classfile/manifest.mf --- a/classfile/manifest.mf +++ b/classfile/manifest.mf @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.modules.classfile/1 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/classfile/Bundle.properties -OpenIDE-Module-Specification-Version: 1.39 +OpenIDE-Module-Specification-Version: 1.40 diff --git a/classfile/src/org/netbeans/modules/classfile/InnerClass.java b/classfile/src/org/netbeans/modules/classfile/BootstrapMethod.java copy from classfile/src/org/netbeans/modules/classfile/InnerClass.java copy to classfile/src/org/netbeans/modules/classfile/BootstrapMethod.java --- a/classfile/src/org/netbeans/modules/classfile/InnerClass.java +++ b/classfile/src/org/netbeans/modules/classfile/BootstrapMethod.java @@ -1,9 +1,7 @@ /* - * InnerClass.java - * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. + * Copyright 2013 Oracle and/or its affiliates. All rights reserved. * * Oracle and Java are registered trademarks of Oracle and/or its affiliates. * Other names may be trademarks of their respective owners. @@ -29,7 +27,7 @@ * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Software is Sun Microsystems, Inc. Portions Copyright 2013 Sun * Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL @@ -57,87 +55,50 @@ * An InnerClass attribute of a classfile. * * @author Thomas Ball + * @since 1.40 */ -public final class InnerClass { +public final class BootstrapMethod { - ClassName name; - ClassName outerClassName; - String simpleName; - int access; + int methodRef; + int[] arguments; - static InnerClass[] loadInnerClasses(DataInputStream in, ConstantPool pool) + static BootstrapMethod[] loadBootstrapMethod(DataInputStream in, ConstantPool pool) throws IOException { int n = in.readUnsignedShort(); - InnerClass[] innerClasses = new InnerClass[n]; + BootstrapMethod[] innerClasses = new BootstrapMethod[n]; for (int i = 0; i < n; i++) - innerClasses[i] = new InnerClass(in, pool); + innerClasses[i] = new BootstrapMethod(in, pool); return innerClasses; } - InnerClass(DataInputStream in, ConstantPool pool) - throws IOException { - loadInnerClass(in, pool); + BootstrapMethod(DataInputStream in, ConstantPool pool) throws IOException { + this.methodRef = in.readUnsignedShort(); + int args = in.readUnsignedShort(); + arguments = new int[args]; + for (int i = 0; i < args; i++) { + arguments[i] = in.readUnsignedShort(); + } } - private void loadInnerClass(DataInputStream in, ConstantPool pool) - throws IOException { - int index = in.readUnsignedShort(); - name = (index > 0) ? pool.getClass(index).getClassName() : null; - index = in.readUnsignedShort(); - outerClassName = (index > 0) ? pool.getClass(index).getClassName() : null; - index = in.readUnsignedShort(); - if (index > 0) { - CPUTF8Info entry = (CPUTF8Info)pool.get(index); - simpleName = entry.getName(); - } - access = in.readUnsignedShort(); + public int getMethodRef() { + return methodRef; } - /** Returns the name of this class, including its package (if any). - * If the compiler didn't define this value, the string - * "" is returned. - * @return the name of this class. - */ - public final ClassName getName() { - return name; - } - - /** Returns the name of the enclosing outer class, including - * its package (if any). - * @return the name of this class, or null if not available. - */ - public final ClassName getOuterClassName() { - return outerClassName; - } - - /** - * Returns the original simple name as given in the source code. - * If this is an anonymous class, null is returned instead. - * @return the simple name of this class, or null if anonymous. - */ - public final String getSimpleName() { - return simpleName; - } - - /** - * Returns the access flags of this class. - */ - public final int getAccess() { - return access; + public int[] getArguments() { + return arguments.clone(); } @Override public String toString() { - StringBuffer sb = new StringBuffer(); - sb.append("innerclass="); - sb.append(name); - if (simpleName != null) { - sb.append(" ("); - sb.append(simpleName); - sb.append(')'); + StringBuilder sb = new StringBuilder(); + sb.append("bootstrapmethod="); + sb.append(methodRef); + sb.append("("); + for (int i = 0; i < arguments.length; i++) { + if (i > 0) sb.append(", "); + sb.append(arguments[i]); } - sb.append(", outerclass="); - sb.append(outerClassName); + sb.append(")"); return sb.toString(); } } diff --git a/classfile/src/org/netbeans/modules/classfile/CPNameAndTypeInfo.java b/classfile/src/org/netbeans/modules/classfile/CPInvokeDynamicInfo.java copy from classfile/src/org/netbeans/modules/classfile/CPNameAndTypeInfo.java copy to classfile/src/org/netbeans/modules/classfile/CPInvokeDynamicInfo.java --- a/classfile/src/org/netbeans/modules/classfile/CPNameAndTypeInfo.java +++ b/classfile/src/org/netbeans/modules/classfile/CPInvokeDynamicInfo.java @@ -3,7 +3,7 @@ * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. + * Copyright 1997-2013 Oracle and/or its affiliates. All rights reserved. * * Oracle and Java are registered trademarks of Oracle and/or its affiliates. * Other names may be trademarks of their respective owners. @@ -29,7 +29,7 @@ * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2013 Sun * Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL @@ -52,49 +52,36 @@ /** - * A class representing the CONSTANT_NameAndType constant pool type. + * A class representing the CONSTANT_InvokeDynamic constant pool type. * * @author Thomas Ball + * @since 1.40 */ -public class CPNameAndTypeInfo extends CPEntry { - int iName; - int iDesc; +public class CPInvokeDynamicInfo extends CPEntry { + int iBootstrapMethod; + int iNameAndType; - CPNameAndTypeInfo(ConstantPool pool,int iName,int iDesc) { + CPInvokeDynamicInfo(ConstantPool pool,int iBootstrapMethod,int iNameAndType) { super(pool); - this.iName = iName; - this.iDesc = iDesc; - } - - protected CPNameAndTypeInfo(ConstantPool pool) { - super(pool); - iName = CPName.INVALID_INDEX; - iDesc = CPName.INVALID_INDEX; - } - - public final String getName() { - return ((CPName)pool.cpEntries[iName]).getName(); - } - - void setNameIndex(int index) { - iName = index; - } - - public final String getDescriptor() { - return ((CPName)pool.cpEntries[iDesc]).getName(); - } - - void setDescriptorIndex(int index) { - iDesc = index; + this.iBootstrapMethod = iBootstrapMethod; + this.iNameAndType = iNameAndType; } public int getTag() { - return ConstantPool.CONSTANT_NameAndType; + return ConstantPool.CONSTANT_InvokeDynamic; + } + + public int getBootstrapMethod() { + return iBootstrapMethod; + } + + public int getNameAndType() { + return iNameAndType; } @Override public String toString() { - return getClass().getName() + ": name=" + getName() + //NOI18N - ", descriptor=" + getDescriptor(); //NOI18N + return getClass().getName() + ": bootstrapMethod=" + iBootstrapMethod + //NOI18N + ", nameAndType=" + iNameAndType; //NOI18N } } diff --git a/classfile/src/org/netbeans/modules/classfile/CPNameAndTypeInfo.java b/classfile/src/org/netbeans/modules/classfile/CPMethodHandleInfo.java copy from classfile/src/org/netbeans/modules/classfile/CPNameAndTypeInfo.java copy to classfile/src/org/netbeans/modules/classfile/CPMethodHandleInfo.java --- a/classfile/src/org/netbeans/modules/classfile/CPNameAndTypeInfo.java +++ b/classfile/src/org/netbeans/modules/classfile/CPMethodHandleInfo.java @@ -3,7 +3,7 @@ * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. + * Copyright 1997-2013 Oracle and/or its affiliates. All rights reserved. * * Oracle and Java are registered trademarks of Oracle and/or its affiliates. * Other names may be trademarks of their respective owners. @@ -29,7 +29,7 @@ * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2013 Sun * Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL @@ -52,49 +52,62 @@ /** - * A class representing the CONSTANT_NameAndType constant pool type. + * A class representing the CONSTANT_MethodHandle constant pool type. * * @author Thomas Ball + * @since 1.40 */ -public class CPNameAndTypeInfo extends CPEntry { - int iName; - int iDesc; +public class CPMethodHandleInfo extends CPEntry { + ReferenceKind referenceKind; + int iReference; - CPNameAndTypeInfo(ConstantPool pool,int iName,int iDesc) { + CPMethodHandleInfo(ConstantPool pool, int referenceKind,int iReference) { super(pool); - this.iName = iName; - this.iDesc = iDesc; - } - - protected CPNameAndTypeInfo(ConstantPool pool) { - super(pool); - iName = CPName.INVALID_INDEX; - iDesc = CPName.INVALID_INDEX; - } - - public final String getName() { - return ((CPName)pool.cpEntries[iName]).getName(); - } - - void setNameIndex(int index) { - iName = index; - } - - public final String getDescriptor() { - return ((CPName)pool.cpEntries[iDesc]).getName(); - } - - void setDescriptorIndex(int index) { - iDesc = index; + this.referenceKind = ReferenceKind.from(referenceKind); + this.iReference = iReference; } public int getTag() { - return ConstantPool.CONSTANT_NameAndType; + return ConstantPool.CONSTANT_MethodHandle; + } + + public ReferenceKind getReferenceKind() { + return referenceKind; + } + + public int getReference() { + return iReference; } @Override public String toString() { - return getClass().getName() + ": name=" + getName() + //NOI18N - ", descriptor=" + getDescriptor(); //NOI18N + return getClass().getName() + ": kind=" + referenceKind + //NOI18N + ", index=" + iReference; //NOI18N + } + + public enum ReferenceKind { + getField(1), + getStatic(2), + putField(3), + putStatic(4), + invokeVirtual(5), + invokeStatic(6), + invokeSpecial(7), + newInvokeSpecial(8), + invokeInterface(9); + + private final int kindInt; + + private ReferenceKind(int kindInt) { + this.kindInt = kindInt; + } + + static ReferenceKind from(int referenceKind) { + for (ReferenceKind k : values()) { + if (k.kindInt == referenceKind) return k; + } + + throw new IllegalStateException("Unknown ref kind: " + referenceKind); + } } } diff --git a/classfile/src/org/netbeans/modules/classfile/CPNameAndTypeInfo.java b/classfile/src/org/netbeans/modules/classfile/CPMethodTypeInfo.java copy from classfile/src/org/netbeans/modules/classfile/CPNameAndTypeInfo.java copy to classfile/src/org/netbeans/modules/classfile/CPMethodTypeInfo.java --- a/classfile/src/org/netbeans/modules/classfile/CPNameAndTypeInfo.java +++ b/classfile/src/org/netbeans/modules/classfile/CPMethodTypeInfo.java @@ -3,7 +3,7 @@ * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. + * Copyright 1997-2013 Oracle and/or its affiliates. All rights reserved. * * Oracle and Java are registered trademarks of Oracle and/or its affiliates. * Other names may be trademarks of their respective owners. @@ -29,7 +29,7 @@ * Contributor(s): * * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2013 Sun * Microsystems, Inc. All Rights Reserved. * * If you wish your version of this file to be governed by only the CDDL @@ -52,49 +52,29 @@ /** - * A class representing the CONSTANT_NameAndType constant pool type. + * A class representing the CONSTANT_MethodType constant pool type. * * @author Thomas Ball + * @since 1.40 */ -public class CPNameAndTypeInfo extends CPEntry { - int iName; - int iDesc; +public class CPMethodTypeInfo extends CPEntry { + int iDescriptor; - CPNameAndTypeInfo(ConstantPool pool,int iName,int iDesc) { + CPMethodTypeInfo(ConstantPool pool, int iDescriptor) { super(pool); - this.iName = iName; - this.iDesc = iDesc; - } - - protected CPNameAndTypeInfo(ConstantPool pool) { - super(pool); - iName = CPName.INVALID_INDEX; - iDesc = CPName.INVALID_INDEX; - } - - public final String getName() { - return ((CPName)pool.cpEntries[iName]).getName(); - } - - void setNameIndex(int index) { - iName = index; - } - - public final String getDescriptor() { - return ((CPName)pool.cpEntries[iDesc]).getName(); - } - - void setDescriptorIndex(int index) { - iDesc = index; + this.iDescriptor = iDescriptor; } public int getTag() { - return ConstantPool.CONSTANT_NameAndType; + return ConstantPool.CONSTANT_MethodType; + } + + public int getDescriptor() { + return iDescriptor; } @Override public String toString() { - return getClass().getName() + ": name=" + getName() + //NOI18N - ", descriptor=" + getDescriptor(); //NOI18N + return getClass().getName() + ": descriptor=" + iDescriptor; //NOI18N } } diff --git a/classfile/src/org/netbeans/modules/classfile/ClassFile.java b/classfile/src/org/netbeans/modules/classfile/ClassFile.java --- a/classfile/src/org/netbeans/modules/classfile/ClassFile.java +++ b/classfile/src/org/netbeans/modules/classfile/ClassFile.java @@ -69,6 +69,7 @@ Method[] methods; String sourceFileName; InnerClass[] innerClasses; + BootstrapMethod[] bootstrapMethods; private AttributeMap attributes; private Map annotations; short majorVersion; @@ -414,6 +415,28 @@ } return Arrays.asList(innerClasses); } + + /**Return the content of the BootstrapMethods attribute. + * + * @return + * @since 1.40 + */ + public final List getBootstrapMethods(){ + if (bootstrapMethods == null) { + DataInputStream in = attributes.getStream("BootstrapMethods"); // NOI18N + if (in != null) { + try { + bootstrapMethods = + BootstrapMethod.loadBootstrapMethod(in, constantPool); + in.close(); + } catch (IOException e) { + throw new InvalidClassFileAttributeException("invalid InnerClasses attribute", e); + } + } else + bootstrapMethods = new BootstrapMethod[0]; + } + return Arrays.asList(bootstrapMethods); + } /** * Returns the major version number of this classfile. diff --git a/classfile/src/org/netbeans/modules/classfile/ConstantPool.java b/classfile/src/org/netbeans/modules/classfile/ConstantPool.java --- a/classfile/src/org/netbeans/modules/classfile/ConstantPool.java +++ b/classfile/src/org/netbeans/modules/classfile/ConstantPool.java @@ -74,6 +74,9 @@ static final int CONSTANT_MethodRef = 10; static final int CONSTANT_InterfaceMethodRef = 11; static final int CONSTANT_NameAndType = 12; + static final int CONSTANT_MethodHandle = 15; + static final int CONSTANT_MethodType = 16; + static final int CONSTANT_InvokeDynamic = 18; CPEntry[] cpEntries; @@ -265,6 +268,26 @@ newEntry = new CPNameAndTypeInfo(this, nameIndex, descIndex); break; } + + case CONSTANT_MethodHandle: { + int kind = cpr.readUnsignedByte(); + int index = cpr.readUnsignedShort(); + newEntry = new CPMethodHandleInfo(this, kind, index); + break; + } + + case CONSTANT_MethodType: { + int index = cpr.readUnsignedShort(); + newEntry = new CPMethodTypeInfo(this, index); + break; + } + + case CONSTANT_InvokeDynamic: { + int bootstrapMethod = cpr.readUnsignedShort(); + int nameAndType = cpr.readUnsignedShort(); + newEntry = new CPInvokeDynamicInfo(this, bootstrapMethod, nameAndType); + break; + } default: throw new IllegalArgumentException( diff --git a/classfile/test/unit/src/org/netbeans/modules/classfile/JDK8ClassFilesTest.java b/classfile/test/unit/src/org/netbeans/modules/classfile/JDK8ClassFilesTest.java new file mode 100644 --- /dev/null +++ b/classfile/test/unit/src/org/netbeans/modules/classfile/JDK8ClassFilesTest.java @@ -0,0 +1,80 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.netbeans.modules.classfile; + +import java.io.InputStream; +import java.util.Arrays; +import junit.framework.TestCase; +import org.netbeans.modules.classfile.CPMethodHandleInfo.ReferenceKind; + +/** + * + * @author lahvac + */ +public class JDK8ClassFilesTest extends TestCase { + + public JDK8ClassFilesTest(String testName) { + super(testName); + } + + public void testDefenderMethods() throws Exception { + InputStream classData = + getClass().getResourceAsStream("datafiles/WithLambda.classx"); + ClassFile classFile = new ClassFile(classData); + CPInvokeDynamicInfo invokeDynamic = (CPInvokeDynamicInfo) classFile.getConstantPool().get(2); + assertEquals(ConstantPool.CONSTANT_InvokeDynamic, invokeDynamic.getTag()); + BootstrapMethod bootstrapMethod = classFile.getBootstrapMethods().get(invokeDynamic.getBootstrapMethod()); + assertEquals("[23, 24, 25]", Arrays.toString(bootstrapMethod.getArguments())); + CPMethodHandleInfo bootstrapMH = (CPMethodHandleInfo) classFile.getConstantPool().get(bootstrapMethod.getMethodRef()); + assertEquals(ConstantPool.CONSTANT_MethodHandle, bootstrapMH.getTag()); + assertEquals(ReferenceKind.invokeStatic, bootstrapMH.getReferenceKind()); + CPMethodInfo bootstrapMethodInfo = (CPMethodInfo) classFile.getConstantPool().get(bootstrapMH.getReference()); + assertEquals("CallSite metaFactory(MethodHandles$Lookup,String,MethodType,MethodHandle,MethodHandle,MethodType)", bootstrapMethodInfo.getFullMethodName()); + CPNameAndTypeInfo nameAndType = (CPNameAndTypeInfo) classFile.getConstantPool().get(invokeDynamic.getNameAndType()); + assertEquals("()Ljava/lang/Runnable;", nameAndType.getDescriptor()); + assertEquals("lambda", nameAndType.getName()); + CPMethodTypeInfo methodType = (CPMethodTypeInfo) classFile.getConstantPool().get(25); + assertEquals(ConstantPool.CONSTANT_MethodType, methodType.getTag()); + CPUTF8Info descriptor = (CPUTF8Info) classFile.getConstantPool().get(methodType.getDescriptor()); + assertEquals("()V", descriptor.getName()); + } +} \ No newline at end of file diff --git a/classfile/test/unit/src/org/netbeans/modules/classfile/datafiles/WithLambda.classx b/classfile/test/unit/src/org/netbeans/modules/classfile/datafiles/WithLambda.classx new file mode 100644 index 0000000000000000000000000000000000000000..0bdc94521d7f218ded434e66079f2fc038d681ef GIT binary patch literal 1003 zc$}S5O>fgc5Pj36cIz0DhLTb~3X~L*05#BK5Qj#pKr&P!RXJ~xrE%-5BYRz`KZ^@U zJ@5lK@RJa;wxvY0MTkA@e7|`+J3oG%e+RIG9S4h8t+RgHK^b>!+_iC!V7(4hpx+Qm zt@a_I+>1t%Q1t^PKctg^j1R;hWKZ3Xh9W!^abWDiS=QqqA*}dXCffTH=&>&*gOTW( z5W5I#E&o`Y3NIAu$orToWeU5d6UBtp%YuZZFhgm+Bsl#jjfZkCFzu?d4%?>Cg$0{X z+l?Zf=vYkOOFfQ8N!7webs6_rKB&QV@erFXuHiaE{K1r~XB74a$8xA0JVLXAEj%`Z zdcq?Li_}J|eHmdJEf;M(x3TTQGyPT!t<70#piZL`>E$h%r1&g`I*QK-l?hvWxn+YN zZ23`il1>TkJEdgY3q_Jhp7GE5K^A!VbKeRz3T4vF3%he^eH{ntsC!l6!P!(^8O%}t zH=IGYFGhPJ4EupLpIpBXWtD@rp2CyQ9D+ZjqD&n!@kRz(d| qbIg(yc