use of org.jetbrains.org.objectweb.asm.ClassReader in project kotlin by JetBrains.
the class SourceInfoGenTest method getProducerInfo.
private String getProducerInfo(String name) {
OutputFile file = generateClassesInFile().get(name);
assertNotNull(file);
ClassReader classReader = new ClassReader(file.asByteArray());
final String[] producer = new String[1];
classReader.accept(new ClassVisitor(Opcodes.ASM5) {
@Override
public void visitSource(String source, String debug) {
producer[0] = source;
}
}, 0);
return producer[0];
}
use of org.jetbrains.org.objectweb.asm.ClassReader in project kotlin by JetBrains.
the class CodegenTestCase method verifyWithAsm.
private static boolean verifyWithAsm(@NotNull OutputFile file, ClassLoader loader) {
ClassNode classNode = new ClassNode();
new ClassReader(file.asByteArray()).accept(classNode, 0);
SimpleVerifier verifier = new SimpleVerifier();
verifier.setClassLoader(loader);
Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(verifier);
boolean noErrors = true;
for (MethodNode method : classNode.methods) {
try {
analyzer.analyze(classNode.name, method);
} catch (Throwable e) {
System.err.println(file.asText());
System.err.println(classNode.name + "::" + method.name + method.desc);
//noinspection InstanceofCatchParameter
if (e instanceof AnalyzerException) {
// Print the erroneous instruction
TraceMethodVisitor tmv = new TraceMethodVisitor(new Textifier());
((AnalyzerException) e).node.accept(tmv);
PrintWriter pw = new PrintWriter(System.err);
tmv.p.print(pw);
pw.flush();
}
e.printStackTrace();
noErrors = false;
}
}
return noErrors;
}
use of org.jetbrains.org.objectweb.asm.ClassReader in project kotlin by JetBrains.
the class ClassEntry method addSuperClasses.
/** Adds in all the super classes found for the given class entries into the given map */
private static void addSuperClasses(@NonNull LintClient client, @NonNull SuperclassVisitor visitor, @NonNull List<ClassEntry> entries) {
for (ClassEntry entry : entries) {
try {
ClassReader reader = new ClassReader(entry.bytes);
int flags = ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
reader.accept(visitor, flags);
} catch (Throwable t) {
client.log(null, "Error processing %1$s: broken class file?", entry.path());
}
}
}
use of org.jetbrains.org.objectweb.asm.ClassReader in project kotlin by JetBrains.
the class LintDriver method runClassDetectors.
private void runClassDetectors(Scope scope, List<ClassEntry> entries, Project project, Project main) {
if (mScope.contains(scope)) {
List<Detector> classDetectors = mScopeDetectors.get(scope);
if (classDetectors != null && !classDetectors.isEmpty() && !entries.isEmpty()) {
AsmVisitor visitor = new AsmVisitor(mClient, classDetectors);
String sourceContents = null;
String sourceName = "";
mOuterClasses = new ArrayDeque<ClassNode>();
ClassEntry prev = null;
for (ClassEntry entry : entries) {
if (prev != null && prev.compareTo(entry) == 0) {
// Duplicate entries for some reason: ignore
continue;
}
prev = entry;
ClassReader reader;
ClassNode classNode;
try {
reader = new ClassReader(entry.bytes);
classNode = new ClassNode();
reader.accept(classNode, 0);
} catch (Throwable t) {
mClient.log(null, "Error processing %1$s: broken class file?", entry.path());
continue;
}
ClassNode peek;
while ((peek = mOuterClasses.peek()) != null) {
if (classNode.name.startsWith(peek.name)) {
break;
} else {
mOuterClasses.pop();
}
}
mOuterClasses.push(classNode);
if (isSuppressed(null, classNode)) {
// Class was annotated with suppress all -- no need to look any further
continue;
}
if (sourceContents != null) {
// Attempt to reuse the source buffer if initialized
// This means making sure that the source files
// foo/bar/MyClass and foo/bar/MyClass$Bar
// and foo/bar/MyClass$3 and foo/bar/MyClass$3$1 have the same prefix.
String newName = classNode.name;
int newRootLength = newName.indexOf('$');
if (newRootLength == -1) {
newRootLength = newName.length();
}
int oldRootLength = sourceName.indexOf('$');
if (oldRootLength == -1) {
oldRootLength = sourceName.length();
}
if (newRootLength != oldRootLength || !sourceName.regionMatches(0, newName, 0, newRootLength)) {
sourceContents = null;
}
}
ClassContext context = new ClassContext(this, project, main, entry.file, entry.jarFile, entry.binDir, entry.bytes, classNode, scope == Scope.JAVA_LIBRARIES, /*fromLibrary*/
sourceContents);
try {
visitor.runClassDetectors(context);
} catch (Exception e) {
mClient.log(e, null);
}
if (mCanceled) {
return;
}
sourceContents = context.getSourceContents(false);
sourceName = classNode.name;
}
mOuterClasses = null;
}
}
}
use of org.jetbrains.org.objectweb.asm.ClassReader in project kotlin by JetBrains.
the class LintDriver method findClass.
/**
* Returns the {@link ClassNode} corresponding to the given type, if possible, or null
*
* @param type the fully qualified type, using JVM signatures (/ and $, not . as path
* separators)
* @param flags the ASM flags to pass to the {@link ClassReader}, normally 0 but can
* for example be {@link ClassReader#SKIP_CODE} and/oor
* {@link ClassReader#SKIP_DEBUG}
* @return the class node for the type, or null
*/
@Nullable
public ClassNode findClass(@NonNull ClassContext context, @NonNull String type, int flags) {
String relative = type.replace('/', File.separatorChar) + DOT_CLASS;
File classFile = findClassFile(context.getProject(), relative);
if (classFile != null) {
if (classFile.getPath().endsWith(DOT_JAR)) {
// TODO: Handle .jar files
return null;
}
try {
byte[] bytes = mClient.readBytes(classFile);
ClassReader reader = new ClassReader(bytes);
ClassNode classNode = new ClassNode();
reader.accept(classNode, flags);
return classNode;
} catch (Throwable t) {
mClient.log(null, "Error processing %1$s: broken class file?", classFile.getPath());
}
}
return null;
}
Aggregations