Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added partial support for CONSTANT_Dynamic #2512

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.junit.After;
Expand Down Expand Up @@ -113,12 +114,16 @@ private void assertHasCodeBase(IScannableCodeBase codeBase) throws Exception {

@Test
public void acceptConstantDynamic() throws Exception {
String fileName = "../spotbugsTestCases/src/classSamples/recordCompileWithBazel6.2/Foo.clazz";
ArrayList<String> fileNames = new ArrayList<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment about the class files (how to recreate them), and refer the issue here?

fileNames.add("../spotbugsTestCases/src/classSamples/recordCompileWithJaCoCo0.8.8/Foo.clazz");
fileNames.add("../spotbugsTestCases/src/classSamples/recordCompileWithJaCoCo0.8.8/FooWithMember.clazz");

try (SingleFileCodeBase codeBase = new SingleFileCodeBase(null, fileName)) {
ClassDescriptor classDescriptor = codeBase.getClassDescriptor();
for (String fileName : fileNames) {
try (SingleFileCodeBase codeBase = new SingleFileCodeBase(null, fileName)) {
ClassDescriptor classDescriptor = codeBase.getClassDescriptor();

assertNotNull(classDescriptor);
assertNotNull(classDescriptor);
}
}
}
}
8 changes: 8 additions & 0 deletions spotbugs/src/main/java/edu/umd/cs/findbugs/OpcodeStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@
import org.apache.bcel.classfile.Constant;
import org.apache.bcel.classfile.ConstantClass;
import org.apache.bcel.classfile.ConstantDouble;
import org.apache.bcel.classfile.ConstantDynamic;
import org.apache.bcel.classfile.ConstantFloat;
import org.apache.bcel.classfile.ConstantInteger;
import org.apache.bcel.classfile.ConstantInvokeDynamic;
import org.apache.bcel.classfile.ConstantLong;
import org.apache.bcel.classfile.ConstantNameAndType;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.classfile.ConstantString;
import org.apache.bcel.classfile.ConstantUtf8;
Expand Down Expand Up @@ -3341,6 +3343,12 @@ private void pushByConstant(DismantleBytecode dbc, Constant c) {
push(new Item("D", Double.valueOf(((ConstantDouble) c).getBytes())));
} else if (c instanceof ConstantLong) {
push(new Item("J", Long.valueOf(((ConstantLong) c).getBytes())));
} else if (c instanceof ConstantDynamic) {
ConstantPool cp = dbc.getConstantPool();
ConstantNameAndType sig = cp.getConstant(((ConstantDynamic) c).getNameAndTypeIndex());
String nameConstantOperand = ((ConstantUtf8) cp.getConstant(sig.getNameIndex())).getBytes();
String sigConstantOperand = ((ConstantUtf8) cp.getConstant(sig.getSignatureIndex())).getBytes();
push(new Item(sigConstantOperand, nameConstantOperand));
} else {
throw new UnsupportedOperationException("StaticConstant type not expected");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.bcel.classfile.ConstantCP;
import org.apache.bcel.classfile.ConstantClass;
import org.apache.bcel.classfile.ConstantDouble;
import org.apache.bcel.classfile.ConstantDynamic;
import org.apache.bcel.classfile.ConstantFieldref;
import org.apache.bcel.classfile.ConstantFloat;
import org.apache.bcel.classfile.ConstantInteger;
Expand Down Expand Up @@ -713,6 +714,12 @@ public void visit(Code obj) {
id.getNameAndTypeIndex());
nameConstantOperand = getStringFromIndex(sig.getNameIndex());
sigConstantOperand = getStringFromIndex(sig.getSignatureIndex());
} else if (constantRefOperand instanceof ConstantDynamic) {
ConstantDynamic id = (ConstantDynamic) constantRefOperand;
ConstantNameAndType sig = (ConstantNameAndType) getConstantPool().getConstant(
id.getNameAndTypeIndex());
nameConstantOperand = getStringFromIndex(sig.getNameIndex());
sigConstantOperand = getStringFromIndex(sig.getSignatureIndex());
} else if (constantRefOperand instanceof ConstantCP) {
ConstantCP cp = (ConstantCP) constantRefOperand;
ConstantClass clazz = (ConstantClass) getConstantPool().getConstant(cp.getClassIndex());
Expand Down
Binary file not shown.