Skip to content

Commit

Permalink
do not throw ClassFormatException to SpotBugs, report missing class i…
Browse files Browse the repository at this point in the history
…nstead
  • Loading branch information
KengoTODA committed Jun 14, 2018
1 parent 6318980 commit 042a74c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
import static org.apache.bcel.Const.LDC_W;
import static org.apache.bcel.Repository.lookupClass;

import java.util.Deque;
import java.util.LinkedList;

import org.apache.bcel.classfile.ClassFormatException;
import org.apache.bcel.classfile.Constant;
import org.apache.bcel.generic.Type;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.OpcodeStack.CustomUserValue;
import edu.umd.cs.findbugs.OpcodeStack.Item;
import edu.umd.cs.findbugs.ba.AnalysisContext;
import edu.umd.cs.findbugs.bcel.OpcodeStackDetector;
import java.util.Deque;
import java.util.LinkedList;
import org.apache.bcel.classfile.Constant;
import org.apache.bcel.generic.Type;
import edu.umd.cs.findbugs.classfile.DescriptorFactory;
import edu.umd.cs.findbugs.internalAnnotations.SlashedClassName;

@CustomUserValue
public class IllegalPassedClassDetector extends OpcodeStackDetector {
Expand Down Expand Up @@ -47,14 +55,20 @@ private void memorizeResultOfClassLiteral(int code) {
}

JavaType storedClass;
@SlashedClassName
String storedClassName = getClassConstantOperand();
try {
String storedClassName = getClassConstantOperand();
storedClass = findClass(storedClassName);
} catch (ClassFormatException e) {
AnalysisContext.reportMissingClass(DescriptorFactory.createClassDescriptor(storedClassName));
storedClass = null;
} finally {
super.afterOpcode(code);
}
Item returnedClass = getStack().getStackItem(0);
returnedClass.setUserValue(storedClass);
if (storedClass != null) {
Item returnedClass = getStack().getStackItem(0);
returnedClass.setUserValue(storedClass);
}
}

private void memorizeResultOfGetClassMethod(int code) {
Expand All @@ -71,7 +85,8 @@ private void memorizeResultOfGetClassMethod(int code) {
returnedClass.setUserValue(classOfCaller);
}

private JavaType findClass(String storedClassName) {
@VisibleForTesting
JavaType findClass(@SlashedClassName String storedClassName) throws ClassFormatException {
try {
return JavaType.from(lookupClass(storedClassName));
} catch (ClassNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jp.skypencil.findbugs.slf4j;

import static org.junit.jupiter.api.Assertions.assertThrows;

import org.apache.bcel.classfile.ClassFormatException;
import org.junit.jupiter.api.Test;

public class IllegalPassedClassDetectorTest {
@Test
public void testFindClass() {
IllegalPassedClassDetector detector = new IllegalPassedClassDetector(null);
assertThrows(ClassFormatException.class, () -> {
detector.findClass("pkg/ClassThatDoesNotExist");
});
}
}

0 comments on commit 042a74c

Please sign in to comment.