Example usage for org.objectweb.asm Opcodes ACC_PRIVATE

List of usage examples for org.objectweb.asm Opcodes ACC_PRIVATE

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ACC_PRIVATE.

Prototype

int ACC_PRIVATE

To view the source code for org.objectweb.asm Opcodes ACC_PRIVATE.

Click Source Link

Usage

From source file:co.cask.common.internal.io.FieldAccessorGenerator.java

License:Apache License

private void generateConstructor(Field field) {
    if (isPrivate) {
        classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "field",
                Type.getDescriptor(Field.class), null, null).visitEnd();
    }/*from w  w w. j av a 2  s.c  om*/

    // Constructor(TypeToken<?> classType)
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC,
            getMethod(void.class, "<init>", TypeToken.class), null, new Type[0], classWriter);
    mg.loadThis();
    mg.loadArg(0);
    mg.invokeConstructor(Type.getType(AbstractFieldAccessor.class),
            getMethod(void.class, "<init>", TypeToken.class));
    if (isPrivate) {
        initializeReflectionField(mg, field);
    }

    mg.returnValue();
    mg.endMethod();
}

From source file:co.cask.tigon.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Generates a {@link DatumWriter} class for encoding data of the given output type with the given schema.
 * @param outputType Type information of the output data type.
 * @param schema Schema of the output data type.
 * @return A {@link co.cask.tigon.internal.asm.ClassDefinition} that contains generated class information.
 *///from www .j ava  2s .  co  m
ClassDefinition generate(TypeToken<?> outputType, Schema schema) {
    classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    TypeToken<?> interfaceType = getInterfaceType(outputType);

    // Generate the class
    String className = getClassName(interfaceType, schema);
    classType = Type.getObjectType(className);
    classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className,
            Signatures.getClassSignature(interfaceType), Type.getInternalName(Object.class),
            new String[] { Type.getInternalName(interfaceType.getRawType()) });

    // Static schema hash field, for verification
    classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "SCHEMA_HASH",
            Type.getDescriptor(String.class), null, schema.getSchemaHash().toString()).visitEnd();

    // Schema field
    classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "schema", Type.getDescriptor(Schema.class),
            null, null).visitEnd();

    // Encode method
    generateEncode(outputType, schema);

    // Constructor
    generateConstructor();

    ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className);
    // DEBUG block. Uncomment for debug
    //    co.cask.tigon.internal.asm.Debugs.debugByteCode(classDefinition, new java.io.PrintWriter(System.out));
    // End DEBUG block
    return classDefinition;
}

From source file:co.cask.tigon.internal.io.DatumWriterGenerator.java

License:Apache License

/**
 * Returns the encode method for the given type and schema. The same method will be returned if the same
 * type and schema has been passed to the method before.
 *
 * @param outputType Type information of the data type for output
 * @param schema Schema to use for output.
 * @return A method for encoding the given output type and schema.
 *///w w  w  . j  a v  a  2  s . c om
private Method getEncodeMethod(TypeToken<?> outputType, Schema schema) {
    String key = String.format("%s%s", normalizeTypeName(outputType), schema.getSchemaHash());

    Method method = encodeMethods.get(key);
    if (method != null) {
        return method;
    }

    // Generate the encode method (value, encoder, schema, set)
    TypeToken<?> callOutputType = getCallTypeToken(outputType, schema);
    String methodName = String.format("encode%s", key);
    method = getMethod(void.class, methodName, callOutputType.getRawType(), Encoder.class, Schema.class,
            Set.class);

    // Put the method into map first before generating the body in order to support recursive data type.
    encodeMethods.put(key, method);

    String methodSignature = Signatures.getMethodSignature(method,
            new TypeToken[] { callOutputType, null, null, new TypeToken<Set<Object>>() {
            } });
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PRIVATE, method, methodSignature,
            new Type[] { Type.getType(IOException.class) }, classWriter);

    generateEncodeBody(mg, schema, outputType, 0, 1, 2, 3);
    mg.returnValue();
    mg.endMethod();

    return method;
}

From source file:com.android.apigenerator.AndroidJarReader.java

License:Apache License

public Map<String, ApiClass> getClasses() {
    HashMap<String, ApiClass> map = new HashMap<String, ApiClass>();

    // Get all the android.jar. They are in platforms-#
    int apiLevel = 0;
    while (true) {
        apiLevel++;/*w  w  w  .  j  a  v a2  s  . co  m*/
        try {
            File jar = new File(mSdkFolder, "platforms/android-" + apiLevel + "/android.jar");
            if (jar.exists() == false) {
                System.out.println("Last API level found: " + (apiLevel - 1));
                break;
            }

            FileInputStream fis = new FileInputStream(jar);
            ZipInputStream zis = new ZipInputStream(fis);
            ZipEntry entry = zis.getNextEntry();
            while (entry != null) {
                String name = entry.getName();

                if (name.endsWith(".class")) {

                    int index = 0;
                    do {
                        int size = zis.read(BUFFER, index, BUFFER.length - index);
                        if (size >= 0) {
                            index += size;
                        } else {
                            break;
                        }
                    } while (true);

                    byte[] b = new byte[index];
                    System.arraycopy(BUFFER, 0, b, 0, index);

                    ClassReader reader = new ClassReader(b);
                    ClassNode classNode = new ClassNode();
                    reader.accept(classNode, 0 /*flags*/);

                    if (classNode != null) {
                        ApiClass theClass = addClass(map, classNode.name, apiLevel);

                        // super class
                        if (classNode.superName != null) {
                            theClass.addSuperClass(classNode.superName, apiLevel);
                        }

                        // interfaces
                        for (Object interfaceName : classNode.interfaces) {
                            theClass.addInterface((String) interfaceName, apiLevel);
                        }

                        // fields
                        for (Object field : classNode.fields) {
                            FieldNode fieldNode = (FieldNode) field;
                            if ((fieldNode.access & Opcodes.ACC_PRIVATE) != 0) {
                                continue;
                            }
                            if (fieldNode.name.startsWith("this$") == false
                                    && fieldNode.name.equals("$VALUES") == false) {
                                theClass.addField(fieldNode.name, apiLevel);
                            }
                        }

                        // methods
                        for (Object method : classNode.methods) {
                            MethodNode methodNode = (MethodNode) method;
                            if ((methodNode.access & Opcodes.ACC_PRIVATE) != 0) {
                                continue;
                            }
                            if (methodNode.name.equals("<clinit>") == false) {
                                theClass.addMethod(methodNode.name + methodNode.desc, apiLevel);
                            }
                        }
                    }
                }
                entry = zis.getNextEntry();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }

    postProcessClasses(map);

    return map;
}

From source file:com.android.build.gradle.internal.incremental.IncrementalSupportVisitor.java

License:Apache License

@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
    int newAccess = access & (~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) | Opcodes.ACC_PUBLIC;
    super.visitInnerClass(name, outerName, innerName, newAccess);
}

From source file:com.android.build.gradle.internal.incremental.IncrementalSupportVisitor.java

License:Apache License

/**
 * If a method/field is not private, make it public. This is to workaround the fact
 * <ul>Our restart.dex files are loaded with a different class loader than the main dex file
 * class loader on restart. so we need methods/fields to be public</ul>
 * <ul>Our reload.dex are loaded from a different class loader as well but methods/fields
 * are accessed through reflection, yet you need class visibility.</ul>
 *
 * remember that in Java, protected methods or fields can be acessed by classes in the same
 * package :/*from ww w  .  ja v  a2s  . c o m*/
 * {@see https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html}
 *
 * @param access the original class/method/field access.
 * @return the new access or the same one depending on the original access rights.
 */
private static int transformAccessForInstantRun(int access) {
    AccessRight accessRight = AccessRight.fromNodeAccess(access);
    if (accessRight != AccessRight.PRIVATE) {
        access &= ~Opcodes.ACC_PROTECTED;
        access &= ~Opcodes.ACC_PRIVATE;
        return access | Opcodes.ACC_PUBLIC;
    }
    return access;
}

From source file:com.android.build.gradle.internal.incremental.IncrementalSupportVisitor.java

License:Apache License

@SuppressWarnings("SimplifiableIfStatement")
private static boolean isCallableFromSubclass(@NonNull MethodNode method, @NonNull ClassNode superClass,
        @NonNull ClassNode subclass) {/*from www  .  j  a va 2 s.c  om*/
    if ((method.access & Opcodes.ACC_PRIVATE) != 0) {
        return false;
    } else if ((method.access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) != 0) {
        return true;
    } else {
        // "package private" access modifier.
        return Objects.equal(ByteCodeUtils.getPackageName(superClass.name),
                ByteCodeUtils.getPackageName(subclass.name));
    }
}

From source file:com.android.build.gradle.internal.incremental.IncrementalSupportVisitor.java

License:Apache License

/**
 * Add all constructors from the passed ClassNode's methods. {@see ClassNode#methods}
 * @param methods the constructors already encountered in the ClassNode hierarchy
 * @param classNode the class to save all new methods from.
 * @param keepPrivateConstructors whether to keep the private constructors.
 *///from  w  ww  .j  a  va 2 s.c  o m
private void addAllNewConstructors(Map<String, MethodNode> methods, ClassNode classNode,
        boolean keepPrivateConstructors) {
    //noinspection unchecked
    for (MethodNode method : (List<MethodNode>) classNode.methods) {
        if (!method.name.equals(ByteCodeUtils.CONSTRUCTOR)) {
            continue;
        }

        if (!isAccessCompatibleWithInstantRun(method.access)) {
            continue;
        }

        if (!keepPrivateConstructors && (method.access & Opcodes.ACC_PRIVATE) != 0) {
            continue;
        }
        if (!classNode.name.equals(visitedClassName) && !classNode.name.equals(visitedSuperName)) {
            continue;
        }
        String key = classNode.name + "." + method.desc;
        if (methods.containsKey(key)) {
            continue;
        }
        methods.put(key, method);
    }
}

From source file:com.android.mkstubs.sourcer.AccessSourcerTest.java

License:Apache License

@Test
public void testPrivateFinalStatic() throws Exception {
    mSourcer.write(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, AccessSourcer.IS_METHOD);

    String s = mWriter.toString();
    Assert.assertEquals("private static final", s);
}

From source file:com.android.mkstubs.sourcer.FieldSourcerTest.java

License:Apache License

@Test
public void testTemplateTypeField() throws Exception {

    FieldSourcer fs = new FieldSourcer(new Output(mWriter), Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, // access
            "mList", // name
            "Ljava/util/ArrayList;", // desc
            "Ljava/util/ArrayList<Ljava/lang/String;>;" // signature
    );//from   ww w. ja va2  s.c  om
    fs.visitEnd();

    String s = mWriter.toString();
    Assert.assertEquals("private final java.util.ArrayList<java.lang.String> mList;\n", s);
}