use of org.jetbrains.org.objectweb.asm.ClassWriter in project kotlin by JetBrains.
the class CompilingEvaluatorUtils method changeSuperToMagicAccessor.
public static byte[] changeSuperToMagicAccessor(byte[] bytes) {
ClassWriter classWriter = new ClassWriter(0);
ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM5, 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.ClassWriter in project kotlin by JetBrains.
the class CompileKotlinAgainstCustomBinariesTest method testInlineFunWithoutDebugInfo.
/*test source mapping generation when source info is absent*/
public void testInlineFunWithoutDebugInfo() throws Exception {
compileKotlin("sourceInline.kt", tmpdir);
File inlineFunClass = new File(tmpdir.getAbsolutePath(), "test/A.class");
ClassWriter cw = new ClassWriter(Opcodes.ASM5);
new ClassReader(FilesKt.readBytes(inlineFunClass)).accept(new ClassVisitor(Opcodes.ASM5, cw) {
@Override
public void visitSource(String source, String debug) {
//skip debug info
}
}, 0);
assert inlineFunClass.delete();
assert !inlineFunClass.exists();
FilesKt.writeBytes(inlineFunClass, cw.toByteArray());
compileKotlin("source.kt", tmpdir, tmpdir);
final Ref<String> debugInfo = new Ref<String>();
File resultFile = new File(tmpdir.getAbsolutePath(), "test/B.class");
new ClassReader(FilesKt.readBytes(resultFile)).accept(new ClassVisitor(Opcodes.ASM5) {
@Override
public void visitSource(String source, String debug) {
//skip debug info
debugInfo.set(debug);
}
}, 0);
String expected = "SMAP\n" + "source.kt\n" + "Kotlin\n" + "*S Kotlin\n" + "*F\n" + "+ 1 source.kt\n" + "test/B\n" + "*L\n" + "1#1,13:1\n" + "*E\n";
if (InlineCodegenUtil.GENERATE_SMAP) {
assertEquals(expected, debugInfo.get());
} else {
assertEquals(null, debugInfo.get());
}
}
use of org.jetbrains.org.objectweb.asm.ClassWriter 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.ClassWriter in project android by JetBrains.
the class ResourceClassGenerator method generate.
/**
* @param fqcn Fully qualified class name (as accepted by ClassLoader, or as returned by Class.getName())
*/
@Nullable
public byte[] generate(String fqcn) {
String className = fqcn.replace('.', '/');
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("generate(%s)", anonymizeClassName(className)));
}
// Don't compute MAXS and FRAMES.
ClassWriter cw = new ClassWriter(0);
cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, className, null, Type.getInternalName(Object.class), null);
int index = className.lastIndexOf('$');
if (index != -1) {
String typeName = className.substring(index + 1);
ResourceType type = ResourceType.getEnum(typeName);
if (type == null) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format(" type '%s' doesn't exist", typeName));
}
return null;
}
cw.visitInnerClass(className, className.substring(0, index), typeName, ACC_PUBLIC + ACC_FINAL + ACC_STATIC);
if (myCache == null) {
myCache = Maps.newHashMap();
}
if (type == ResourceType.STYLEABLE) {
if (myStyleableCache == null) {
TObjectIntHashMap<String> styleableIntCache = new TObjectIntHashMap<String>();
myCache.put(type, styleableIntCache);
myStyleableCache = Maps.newHashMap();
generateStyleable(cw, styleableIntCache, className);
} else {
TObjectIntHashMap<String> styleableIntCache = myCache.get(type);
assert styleableIntCache != null;
generateFields(cw, styleableIntCache);
generateIntArrayFromCache(cw, className, myStyleableCache);
}
} else {
TObjectIntHashMap<String> typeCache = myCache.get(type);
if (typeCache == null) {
typeCache = new TObjectIntHashMap<String>();
myCache.put(type, typeCache);
generateValuesForType(cw, type, typeCache);
} else {
generateFields(cw, typeCache);
}
}
} else {
// Default R class.
boolean styleableAdded = false;
for (ResourceType t : myAppResources.getAvailableResourceTypes()) {
// getAvailableResourceTypes() sometimes returns both styleable and declare styleable. Make sure that we only create one subclass.
if (t == ResourceType.DECLARE_STYLEABLE) {
t = ResourceType.STYLEABLE;
}
if (t == ResourceType.STYLEABLE) {
if (styleableAdded) {
continue;
} else {
styleableAdded = true;
}
}
cw.visitInnerClass(className + "$" + t.getName(), className, t.getName(), ACC_PUBLIC + ACC_FINAL + ACC_STATIC);
}
}
generateConstructor(cw);
cw.visitEnd();
return cw.toByteArray();
}
use of org.jetbrains.org.objectweb.asm.ClassWriter in project intellij-community by JetBrains.
the class BaseInstrumentingBuilder method performBuild.
@Override
protected final ExitCode performBuild(CompileContext context, ModuleChunk chunk, InstrumentationClassFinder finder, OutputConsumer outputConsumer) {
ExitCode exitCode = ExitCode.NOTHING_DONE;
for (CompiledClass compiledClass : outputConsumer.getCompiledClasses().values()) {
if (Utils.IS_TEST_MODE || LOG.isDebugEnabled()) {
LOG.info("checking " + compiledClass + " by " + getClass());
}
final BinaryContent originalContent = compiledClass.getContent();
final ClassReader reader = new FailSafeClassReader(originalContent.getBuffer(), originalContent.getOffset(), originalContent.getLength());
final int version = getClassFileVersion(reader);
if (IS_INSTRUMENTED_KEY.get(compiledClass, Boolean.FALSE) || !canInstrument(compiledClass, version)) {
// do not instrument the same content twice
continue;
}
final ClassWriter writer = new InstrumenterClassWriter(reader, getAsmClassWriterFlags(version), finder);
try {
if (Utils.IS_TEST_MODE || LOG.isDebugEnabled()) {
LOG.info("instrumenting " + compiledClass + " by " + getClass());
}
final BinaryContent instrumented = instrument(context, compiledClass, reader, writer, finder);
if (instrumented != null) {
compiledClass.setContent(instrumented);
finder.cleanCachedData(compiledClass.getClassName());
IS_INSTRUMENTED_KEY.set(compiledClass, Boolean.TRUE);
exitCode = ExitCode.OK;
}
} catch (Throwable e) {
LOG.info(e);
final String message = e.getMessage();
if (message != null) {
context.processMessage(new CompilerMessage(getPresentableName(), BuildMessage.Kind.ERROR, message, ContainerUtil.getFirstItem(compiledClass.getSourceFilesPaths())));
} else {
context.processMessage(new CompilerMessage(getPresentableName(), e));
}
}
}
return exitCode;
}
Aggregations