BCEL-277: Resolving the String representation of a constant throws NoSuchElementException in case of CONSTANT_NameAndType constant. Thanks to Sam Yoon. This also closes #9 from GitHub.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/bcel/trunk@1754483 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 3bd9426..a24921d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -63,6 +63,7 @@
<body>
<release version="6.1" date="tba" description="tba">
+ <action issue="BCEL-277" type="fix" dev="britter" due-to="Sam Yoon">Resolving the String representation of a constant throws NoSuchElementException in case of CONSTANT_NameAndType constant.</action>
</release>
<release version="6.0" date="2016-07-10" description="Apache Commons BCEL 6.0 is a major release supporting the new features
diff --git a/src/main/java/org/apache/bcel/classfile/ConstantPool.java b/src/main/java/org/apache/bcel/classfile/ConstantPool.java
index d9f9f8a..ea255b5 100644
--- a/src/main/java/org/apache/bcel/classfile/ConstantPool.java
+++ b/src/main/java/org/apache/bcel/classfile/ConstantPool.java
@@ -131,7 +131,7 @@
case Const.CONSTANT_NameAndType:
str = constantToString(((ConstantNameAndType) c).getNameIndex(),
Const.CONSTANT_Utf8)
- + ":" + constantToString(((ConstantNameAndType) c).getSignatureIndex(),
+ + " " + constantToString(((ConstantNameAndType) c).getSignatureIndex(),
Const.CONSTANT_Utf8);
break;
case Const.CONSTANT_InterfaceMethodref:
@@ -148,7 +148,7 @@
str = Const.getMethodHandleName(cmh.getReferenceKind())
+ " " + constantToString(cmh.getReferenceIndex(),
getConstant(cmh.getReferenceIndex()).getTag());
- break;
+ break;
case Const.CONSTANT_MethodType:
final ConstantMethodType cmt = (ConstantMethodType) c;
str = constantToString(cmt.getDescriptorIndex(), Const.CONSTANT_Utf8);
@@ -209,7 +209,7 @@
}
- /**
+ /**
* Dump constant pool to file stream in binary format.
*
* @param file Output file stream
diff --git a/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java b/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java
new file mode 100644
index 0000000..4846f86
--- /dev/null
+++ b/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.bcel.classfile;
+
+import org.apache.bcel.AbstractTestCase;
+import org.apache.bcel.generic.ConstantPoolGen;
+import org.apache.bcel.generic.InstructionHandle;
+import org.apache.bcel.generic.InstructionList;
+import org.apache.bcel.generic.MethodGen;
+import org.junit.Test;
+
+public class ConstantPoolTestCase extends AbstractTestCase {
+ @Test
+ public void testConstantToString() throws ClassNotFoundException {
+ JavaClass clazz = getTestClass(PACKAGE_BASE_NAME + ".data.SimpleClassWithDefaultConstructor");
+ ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool());
+
+ Method[] methods = clazz.getMethods();
+
+ for (Method method : methods) {
+ if (method.getName().equals("<init>")) {
+ for (InstructionHandle instructionHandle : getInstructionHandles(clazz, cp, method)) {
+ System.out.println(instructionHandle.getInstruction().toString(cp.getConstantPool()));
+ }
+ }
+ }
+ }
+
+ private InstructionHandle[] getInstructionHandles(JavaClass clazz, ConstantPoolGen cp, Method method) {
+ MethodGen methodGen = new MethodGen(method, clazz.getClassName(), cp);
+ InstructionList instructionList = methodGen.getInstructionList();
+ return instructionList.getInstructionHandles();
+ }
+}
diff --git a/src/test/java/org/apache/bcel/data/SimpleClassWithDefaultConstructor.java b/src/test/java/org/apache/bcel/data/SimpleClassWithDefaultConstructor.java
new file mode 100644
index 0000000..9bb1805
--- /dev/null
+++ b/src/test/java/org/apache/bcel/data/SimpleClassWithDefaultConstructor.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.bcel.data;
+
+public class SimpleClassWithDefaultConstructor {
+
+ public SimpleClassWithDefaultConstructor() {
+ }
+
+}