Created
April 4, 2015 21:56
-
-
Save davidmc24/17e120b67ce35831a07a to your computer and use it in GitHub Desktop.
Example class for javassist bug report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javassist.ClassPool; | |
import javassist.CtBehavior; | |
import javassist.CtClass; | |
import javassist.NotFoundException; | |
import javassist.bytecode.ClassFile; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class DescribingHandlers { | |
public static void describeTo(Class<?> clazz, StringBuilder stringBuilder) { | |
if (clazz.isAnonymousClass()) { | |
ClassPool pool = ClassPool.getDefault(); | |
CtClass ctClass; | |
try { | |
ctClass = pool.get(clazz.getName()); | |
CtBehavior[] behaviors = ctClass.getDeclaredBehaviors(); | |
List<CtBehavior> withLineNumber = | |
Arrays.asList(behaviors).stream() | |
.filter(input -> input.getMethodInfo().getLineNumber(0) > 0) | |
.sorted((o1, o2) -> Integer.valueOf(o1.getMethodInfo().getLineNumber(0)).compareTo(o2.getMethodInfo().getLineNumber(0))) | |
.collect(Collectors.toList()); | |
if (!withLineNumber.isEmpty()) { | |
CtBehavior method = withLineNumber.get(0); | |
int lineNumber = method.getMethodInfo().getLineNumber(0); | |
ClassFile classFile = ctClass.getClassFile(); | |
String sourceFile = classFile.getSourceFile(); | |
if (lineNumber != -1 && sourceFile != null) { | |
stringBuilder | |
.append("anonymous class ") | |
.append(clazz.getName()) | |
.append(" at approximately line ") | |
.append(lineNumber) | |
.append(" of ") | |
.append(sourceFile); | |
return; | |
} | |
} | |
} catch (NotFoundException ignore) { | |
// fall through | |
} | |
} | |
stringBuilder.append(clazz.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment