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/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,40 +52,19 @@ /** - * 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() { @@ -94,7 +73,7 @@ @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,54 @@ /** - * 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; } @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,25 @@ /** - * 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_MethodHandle; } @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/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/regression/JDK8ClassFilesTest.java b/classfile/test/unit/src/regression/JDK8ClassFilesTest.java new file mode 100644 --- /dev/null +++ b/classfile/test/unit/src/regression/JDK8ClassFilesTest.java @@ -0,0 +1,64 @@ +/* + * 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 regression; + +import java.io.InputStream; +import junit.framework.TestCase; +import org.netbeans.modules.classfile.ClassFile; + +/** + * + * @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); + classFile.toString(); + } +} \ No newline at end of file diff --git a/classfile/test/unit/src/regression/datafiles/WithLambda.classx b/classfile/test/unit/src/regression/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