use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.
the class OutputFilesSink method save.
public void save(@NotNull final OutputFileObject fileObject) {
final BinaryContent content = fileObject.getContent();
final File srcFile = fileObject.getSourceFile();
boolean isTemp = false;
final JavaFileObject.Kind outKind = fileObject.getKind();
if (srcFile != null && content != null) {
final String sourcePath = FileUtil.toSystemIndependentName(srcFile.getPath());
final JavaSourceRootDescriptor rootDescriptor = myContext.getProjectDescriptor().getBuildRootIndex().findJavaRootDescriptor(myContext, srcFile);
try {
if (rootDescriptor != null) {
isTemp = rootDescriptor.isTemp;
if (!isTemp) {
// first, handle [src->output] mapping and register paths for files_generated event
if (outKind == JavaFileObject.Kind.CLASS) {
// todo: avoid array copying?
myOutputConsumer.registerCompiledClass(rootDescriptor.target, new CompiledClass(fileObject.getFile(), srcFile, fileObject.getClassName(), content));
} else {
myOutputConsumer.registerOutputFile(rootDescriptor.target, fileObject.getFile(), Collections.<String>singleton(sourcePath));
}
}
} else {
// was not able to determine the source root descriptor or the source root is excluded from compilation (e.g. for annotation processors)
if (outKind == JavaFileObject.Kind.CLASS) {
myOutputConsumer.registerCompiledClass(null, new CompiledClass(fileObject.getFile(), srcFile, fileObject.getClassName(), content));
}
}
} catch (IOException e) {
myContext.processMessage(new CompilerMessage(JavaBuilder.BUILDER_NAME, e));
}
if (!isTemp && outKind == JavaFileObject.Kind.CLASS) {
// register in mappings any non-temp class file
try {
final ClassReader reader = new FailSafeClassReader(content.getBuffer(), content.getOffset(), content.getLength());
myMappingsCallback.associate(FileUtil.toSystemIndependentName(fileObject.getFile().getPath()), sourcePath, reader);
} catch (Throwable e) {
// need this to make sure that unexpected errors in, for example, ASM will not ruin the compilation
final String message = "Class dependency information may be incomplete! Error parsing generated class " + fileObject.getFile().getPath();
LOG.info(message, e);
myContext.processMessage(new CompilerMessage(JavaBuilder.BUILDER_NAME, BuildMessage.Kind.WARNING, message + "\n" + CompilerMessage.getTextFromThrowable(e), sourcePath));
}
}
}
if (outKind == JavaFileObject.Kind.CLASS) {
myContext.processMessage(new ProgressMessage("Writing classes... " + myChunkName));
if (!isTemp && srcFile != null) {
mySuccessfullyCompiled.add(srcFile);
}
}
}
use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.
the class CompilingEvaluator method changeSuperToMagicAccessor.
private static byte[] changeSuperToMagicAccessor(byte[] bytes) {
ClassWriter classWriter = new ClassWriter(0);
ClassVisitor classVisitor = new ClassVisitor(Opcodes.API_VERSION, classWriter) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
if ("java/lang/Object".equals(superName)) {
superName = "sun/reflect/MagicAccessorImpl";
}
super.visit(version, access, name, signature, superName, interfaces);
}
};
new ClassReader(bytes).accept(classVisitor, 0);
return classWriter.toByteArray();
}
use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.
the class BytecodeAnalysisIndex method collectKeys.
@NotNull
private static Map<Bytes, Void> collectKeys(byte[] content) throws NoSuchAlgorithmException {
HashMap<Bytes, Void> map = new HashMap<>();
MessageDigest md = BytecodeAnalysisConverter.getMessageDigest();
new ClassReader(content).accept(new KeyedMethodVisitor() {
@Nullable
@Override
MethodVisitor visitMethod(MethodNode node, Key key) {
map.put(ClassDataIndexer.compressKey(md, key), null);
return null;
}
}, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
return map;
}
use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.
the class ClassFileViewProvider method detectInnerClass.
private static boolean detectInnerClass(VirtualFile file, @Nullable byte[] content) {
String name = file.getNameWithoutExtension();
int p = name.lastIndexOf('$', name.length() - 2);
if (p <= 0)
return false;
Boolean isInner = IS_INNER_CLASS.get(file);
if (isInner != null)
return isInner;
if (content == null) {
try {
content = file.contentsToByteArray(false);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
ClassReader reader = new ClassReader(content);
final Ref<Boolean> ref = Ref.create(Boolean.FALSE);
final String className = reader.getClassName();
reader.accept(new ClassVisitor(Opcodes.API_VERSION) {
@Override
public void visitOuterClass(String owner, String name, String desc) {
ref.set(Boolean.TRUE);
}
@Override
public void visitInnerClass(String name, String outer, String inner, int access) {
if (className.equals(name)) {
ref.set(Boolean.TRUE);
}
}
}, EMPTY_ATTRIBUTES, ClassReader.SKIP_DEBUG | ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
isInner = ref.get();
IS_INNER_CLASS.set(file, isInner);
return isInner;
}
use of org.jetbrains.org.objectweb.asm.ClassReader in project intellij-community by JetBrains.
the class ClsMirrorBuildingTest method testStrayInnersFiltering.
public void testStrayInnersFiltering() throws IOException {
String path = JavaTestUtil.getJavaTestDataPath() + "/../../mockJDK-1.8/jre/lib/rt.jar!/java/lang/Class.class";
VirtualFile file = StandardFileSystems.jar().findFileByPath(path);
assertNotNull(path, file);
InnerClassSourceStrategy<VirtualFile> strategy = new InnerClassSourceStrategy<VirtualFile>() {
@Override
public VirtualFile findInnerClass(String innerName, VirtualFile outerClass) {
String baseName = outerClass.getNameWithoutExtension();
VirtualFile child = outerClass.getParent().findChild(baseName + "$" + innerName + ".class");
// stray inner classes should be filtered out
assert child != null : innerName + " is not an inner class of " + outerClass;
return child;
}
@Override
public void accept(VirtualFile innerClass, StubBuildingVisitor<VirtualFile> visitor) {
try {
byte[] bytes = innerClass.contentsToByteArray();
new ClassReader(bytes).accept(visitor, ClassReader.SKIP_FRAMES);
} catch (IOException ignored) {
}
}
};
PsiJavaFileStubImpl stub = new PsiJavaFileStubImpl("java.lang", true);
StubBuildingVisitor<VirtualFile> visitor = new StubBuildingVisitor<>(file, strategy, stub, 0, null);
new ClassReader(file.contentsToByteArray()).accept(visitor, ClassReader.SKIP_FRAMES);
}
Aggregations