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);
}
}
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();
}
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();
}
Aggregations