Search in sources :

Example 21 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateToArray.

private void generateToArray() {
    if (descriptor.getKind() == ClassKind.INTERFACE)
        return;
    final KotlinBuiltIns builtIns = DescriptorUtilsKt.getBuiltIns(descriptor);
    if (!isSubclass(descriptor, builtIns.getCollection()))
        return;
    if (CollectionsKt.any(DescriptorUtilsKt.getAllSuperclassesWithoutAny(descriptor), new Function1<ClassDescriptor, Boolean>() {

        @Override
        public Boolean invoke(ClassDescriptor classDescriptor) {
            return !(classDescriptor instanceof JavaClassDescriptor) && isSubclass(classDescriptor, builtIns.getCollection());
        }
    }))
        return;
    Collection<SimpleFunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getContributedFunctions(Name.identifier("toArray"), NoLookupLocation.FROM_BACKEND);
    boolean hasGenericToArray = false;
    boolean hasNonGenericToArray = false;
    for (FunctionDescriptor function : functions) {
        hasGenericToArray |= isGenericToArray(function);
        hasNonGenericToArray |= isNonGenericToArray(function);
    }
    if (!hasNonGenericToArray) {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, ACC_PUBLIC, "toArray", "()[Ljava/lang/Object;", null, null);
        InstructionAdapter iv = new InstructionAdapter(mv);
        mv.visitCode();
        iv.load(0, classAsmType);
        iv.invokestatic("kotlin/jvm/internal/CollectionToArray", "toArray", "(Ljava/util/Collection;)[Ljava/lang/Object;", false);
        iv.areturn(Type.getType("[Ljava/lang/Object;"));
        FunctionCodegen.endVisit(mv, "toArray", myClass);
    }
    if (!hasGenericToArray) {
        MethodVisitor mv = v.newMethod(NO_ORIGIN, ACC_PUBLIC, "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;", "<T:Ljava/lang/Object;>([TT;)[TT;", null);
        InstructionAdapter iv = new InstructionAdapter(mv);
        mv.visitCode();
        iv.load(0, classAsmType);
        iv.load(1, Type.getType("[Ljava/lang/Object;"));
        iv.invokestatic("kotlin/jvm/internal/CollectionToArray", "toArray", "(Ljava/util/Collection;[Ljava/lang/Object;)[Ljava/lang/Object;", false);
        iv.areturn(Type.getType("[Ljava/lang/Object;"));
        FunctionCodegen.endVisit(mv, "toArray", myClass);
    }
}
Also used : JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) KotlinBuiltIns(org.jetbrains.kotlin.builtins.KotlinBuiltIns) JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 22 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project android by JetBrains.

the class ResourceClassGenerator method generateConstructor.

/** Generate an empty constructor. */
private static void generateConstructor(@NotNull ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}
Also used : MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 23 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project android by JetBrains.

the class ResourceClassGenerator method generateStyleable.

private void generateStyleable(@NotNull ClassWriter cw, @NotNull TObjectIntHashMap<String> styleableIntCache, String className) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("generateStyleable(%s)", anonymizeClassName(className)));
    }
    boolean debug = LOG.isDebugEnabled() && isPublicClass(className);
    Collection<String> declaredStyleables = myAppResources.getItemsOfType(ResourceType.DECLARE_STYLEABLE);
    // Generate all declarations - both int[] and int for the indices into the array.
    for (String styleableName : declaredStyleables) {
        List<ResourceItem> items = myAppResources.getResourceItem(ResourceType.DECLARE_STYLEABLE, styleableName);
        if (items == null || items.isEmpty()) {
            if (debug) {
                LOG.debug("  No items for " + styleableName);
            }
            continue;
        }
        String fieldName = AndroidResourceUtil.getFieldNameByResourceName(styleableName);
        cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, fieldName, "[I", null, null);
        if (debug) {
            LOG.debug("  Defined styleable " + fieldName);
        }
        ResourceValue resourceValue = items.get(0).getResourceValue(false);
        assert resourceValue instanceof DeclareStyleableResourceValue;
        DeclareStyleableResourceValue dv = (DeclareStyleableResourceValue) resourceValue;
        List<AttrResourceValue> attributes = dv.getAllAttributes();
        int idx = 0;
        for (AttrResourceValue value : attributes) {
            Integer initialValue = idx++;
            String styleableEntryName = getResourceName(fieldName, value);
            cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, styleableEntryName, "I", null, initialValue);
            styleableIntCache.put(styleableEntryName, initialValue);
            if (debug) {
                LOG.debug("  Defined styleable " + styleableEntryName);
            }
        }
    }
    // Generate class initializer block to initialize the arrays declared above.
    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    for (String styleableName : declaredStyleables) {
        List<ResourceItem> items = myAppResources.getResourceItem(ResourceType.DECLARE_STYLEABLE, styleableName);
        if (items == null || items.isEmpty()) {
            continue;
        }
        ResourceValue resourceValue = items.get(0).getResourceValue(false);
        assert resourceValue instanceof DeclareStyleableResourceValue;
        DeclareStyleableResourceValue dv = (DeclareStyleableResourceValue) resourceValue;
        List<AttrResourceValue> attributes = dv.getAllAttributes();
        if (attributes.isEmpty()) {
            continue;
        }
        Integer[] valuesArray = myAppResources.getDeclaredArrayValues(attributes, styleableName);
        if (valuesArray == null) {
            valuesArray = new Integer[attributes.size()];
        }
        List<Integer> values = Arrays.asList(valuesArray);
        String fieldName = AndroidResourceUtil.getFieldNameByResourceName(styleableName);
        myStyleableCache.put(fieldName, values);
        int idx = -1;
        for (AttrResourceValue value : attributes) {
            if (valuesArray[++idx] == null || !value.isFramework()) {
                valuesArray[idx] = myAppResources.getResourceId(ResourceType.ATTR, value.getName());
            }
        }
        generateArrayInitialization(mv, className, fieldName, values);
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(4, 0);
    mv.visitEnd();
}
Also used : DeclareStyleableResourceValue(com.android.ide.common.rendering.api.DeclareStyleableResourceValue) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) DeclareStyleableResourceValue(com.android.ide.common.rendering.api.DeclareStyleableResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem)

Aggregations

MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)23 NotNull (org.jetbrains.annotations.NotNull)7 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)7 Type (org.jetbrains.org.objectweb.asm.Type)5 KotlinType (org.jetbrains.kotlin.types.KotlinType)4 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)3 Label (org.jetbrains.org.objectweb.asm.Label)3 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)3 SourcePosition (com.intellij.debugger.SourcePosition)2 DebuggerUtilsEx (com.intellij.debugger.impl.DebuggerUtilsEx)2 MethodBytecodeUtil (com.intellij.debugger.jdi.MethodBytecodeUtil)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Document (com.intellij.openapi.editor.Document)2 com.intellij.psi (com.intellij.psi)2 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)2 Opcodes (org.jetbrains.org.objectweb.asm.Opcodes)2