Search in sources :

Example 6 with ClassWriter

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;
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) InstrumenterClassWriter(com.intellij.compiler.instrumentation.InstrumenterClassWriter) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter) InstrumenterClassWriter(com.intellij.compiler.instrumentation.InstrumenterClassWriter)

Example 7 with ClassWriter

use of org.jetbrains.org.objectweb.asm.ClassWriter in project intellij-community by JetBrains.

the class AsmCodeGeneratorTest method initCodeGenerator.

private AsmCodeGenerator initCodeGenerator(final String formFileName, final String className, final String testDataPath) throws Exception {
    String tmpPath = FileUtil.getTempDirectory();
    String formPath = testDataPath + formFileName;
    String javaPath = testDataPath + className + ".java";
    final int rc = Main.compile(new String[] { "-d", tmpPath, javaPath });
    assertEquals(0, rc);
    final String classPath = tmpPath + "/" + className + ".class";
    final File classFile = new File(classPath);
    assertTrue(classFile.exists());
    final LwRootContainer rootContainer = loadFormData(formPath);
    final AsmCodeGenerator codeGenerator = new AsmCodeGenerator(rootContainer, myClassFinder, myNestedFormLoader, false, new ClassWriter(ClassWriter.COMPUTE_FRAMES));
    final FileInputStream classStream = new FileInputStream(classFile);
    try {
        codeGenerator.patchClass(classStream);
    } finally {
        classStream.close();
        FileUtil.delete(classFile);
        final File[] inners = new File(tmpPath).listFiles((dir, name) -> name.startsWith(className + "$") && name.endsWith(".class"));
        if (inners != null) {
            for (File file : inners) FileUtil.delete(file);
        }
    }
    return codeGenerator;
}
Also used : LwRootContainer(com.intellij.uiDesigner.lw.LwRootContainer) AsmCodeGenerator(com.intellij.uiDesigner.compiler.AsmCodeGenerator) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter)

Example 8 with ClassWriter

use of org.jetbrains.org.objectweb.asm.ClassWriter in project intellij-community by JetBrains.

the class PreviewNestedFormLoader method generateStubClass.

private void generateStubClass(final LwRootContainer rootContainer, final String generatedClassName) throws IOException, CodeGenerationException {
    @NonNls ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, generatedClassName, null, "java/lang/Object", ArrayUtil.EMPTY_STRING_ARRAY);
    cw.visitField(Opcodes.ACC_PUBLIC, PreviewFormAction.PREVIEW_BINDING_FIELD, "Ljavax/swing/JComponent;", null, null);
    @NonNls MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    cw.visitEnd();
    ByteArrayInputStream bais = new ByteArrayInputStream(cw.toByteArray());
    AsmCodeGenerator acg = new AsmCodeGenerator(rootContainer, myFinder, this, true, new PsiClassWriter(myModule));
    byte[] data = acg.patchClass(bais);
    FormErrorInfo[] errors = acg.getErrors();
    if (errors.length > 0) {
        throw new CodeGenerationException(errors[0].getComponentId(), errors[0].getErrorMessage());
    }
    FileUtil.writeToFile(new File(myTempPath, generatedClassName + ".class"), data);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CodeGenerationException(com.intellij.uiDesigner.compiler.CodeGenerationException) ByteArrayInputStream(java.io.ByteArrayInputStream) PsiClassWriter(com.intellij.compiler.PsiClassWriter) FormErrorInfo(com.intellij.uiDesigner.compiler.FormErrorInfo) AsmCodeGenerator(com.intellij.uiDesigner.compiler.AsmCodeGenerator) File(java.io.File) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter) PsiClassWriter(com.intellij.compiler.PsiClassWriter) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Aggregations

ClassWriter (org.jetbrains.org.objectweb.asm.ClassWriter)8 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)4 File (java.io.File)3 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)3 FailSafeClassReader (com.intellij.compiler.instrumentation.FailSafeClassReader)2 AsmCodeGenerator (com.intellij.uiDesigner.compiler.AsmCodeGenerator)2 ResourceType (com.android.resources.ResourceType)1 PsiClassWriter (com.intellij.compiler.PsiClassWriter)1 InstrumenterClassWriter (com.intellij.compiler.instrumentation.InstrumenterClassWriter)1 Ref (com.intellij.openapi.util.Ref)1 CodeGenerationException (com.intellij.uiDesigner.compiler.CodeGenerationException)1 FormErrorInfo (com.intellij.uiDesigner.compiler.FormErrorInfo)1 LwRootContainer (com.intellij.uiDesigner.lw.LwRootContainer)1 TObjectIntHashMap (gnu.trove.TObjectIntHashMap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 JarFile (java.util.jar.JarFile)1 NonNls (org.jetbrains.annotations.NonNls)1 Nullable (org.jetbrains.annotations.Nullable)1 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)1 RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile (org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile)1