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:org.gradle.initialization.ExceptionDecoratingClassGenerator.java

License:Apache License

private <T> Class<? extends T> doGenerate(Class<T> type) {
    ClassWriter visitor = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    String typeName = StringUtils.substringBeforeLast(type.getName(), ".") + ".LocationAware"
            + type.getSimpleName();//from  w w  w.  j  av  a 2 s .co m
    Type generatedType = Type.getType("L" + typeName.replaceAll("\\.", "/") + ";");
    Type superclassType = Type.getType(type);

    visitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, generatedType.getInternalName(), null,
            superclassType.getInternalName(),
            new String[] { Type.getType(LocationAwareException.class).getInternalName() });

    Type helperType = Type.getType(ExceptionHelper.class);
    Type throwableType = Type.getType(Throwable.class);
    Type scriptSourceType = Type.getType(ScriptSource.class);
    Type integerType = Type.getType(Integer.class);

    // GENERATE private ExceptionHelper helper;
    visitor.visitField(Opcodes.ACC_PRIVATE, "helper", helperType.getDescriptor(), null, null);

    // GENERATE <init>(<type> target, ScriptSource source, Integer lineNumber)

    String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE,
            new Type[] { superclassType, scriptSourceType, integerType });
    MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, null,
            new String[0]);
    methodVisitor.visitCode();

    boolean noArgsConstructor;
    try {
        type.getConstructor(type);
        noArgsConstructor = false;
    } catch (NoSuchMethodException e) {
        try {
            type.getConstructor();
            noArgsConstructor = true;
        } catch (NoSuchMethodException e1) {
            throw new IllegalArgumentException(String.format(
                    "Cannot create subtype for exception '%s'. It needs a zero-args or copy constructor.",
                    type.getName()));
        }
    }

    if (noArgsConstructor) {
        // GENERATE super()
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[0]));
        // END super()
    } else {
        // GENERATE super(target)
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { superclassType }));
        // END super(target)
    }

    // GENERATE helper = new ExceptionHelper(this, target, source, lineNumber)
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);

    methodVisitor.visitTypeInsn(Opcodes.NEW, helperType.getInternalName());
    methodVisitor.visitInsn(Opcodes.DUP);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 2);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 3);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, helperType.getInternalName(), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { throwableType, throwableType, scriptSourceType, integerType }));

    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, generatedType.getInternalName(), "helper",
            helperType.getDescriptor());

    // END helper = new ExceptionHelper(target)

    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    // END <init>(<type> target, ScriptSource source, Integer lineNumber)

    for (Method method : ExceptionHelper.class.getDeclaredMethods()) {
        // GENERATE public <type> <method>() { return helper.<method>(); }
        methodDescriptor = Type.getMethodDescriptor(Type.getType(method.getReturnType()), new Type[0]);
        methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methodDescriptor, null,
                new String[0]);
        methodVisitor.visitCode();

        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitFieldInsn(Opcodes.GETFIELD, generatedType.getInternalName(), "helper",
                helperType.getDescriptor());
        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, helperType.getInternalName(), method.getName(),
                methodDescriptor);

        methodVisitor.visitInsn(Opcodes.ARETURN);
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
        // END public <type> <method>() { return helper.<method>(); }
    }

    visitor.visitEnd();

    byte[] bytecode = visitor.toByteArray();
    return (Class<T>) ReflectionUtil.invoke(type.getClassLoader(), "defineClass",
            new Object[] { typeName, bytecode, 0, bytecode.length });
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(ParameterDeclaration node) throws ASTVisitorException {
    node.getType().accept(this);
    //is the access flag private?

    mn.parameters.add(new ParameterNode(node.getIdentifier(), Opcodes.ACC_PRIVATE));
    System.out.println("hereeeee " + node.getIdentifier());
}

From source file:org.jacoco.core.internal.analysis.filter.EnumEmptyConstructorFilterTest.java

License:Open Source License

@Test
public void should_filter() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_PRIVATE, "<init>",
            "(Ljava/lang/String;I)V", null, null);
    m.visitVarInsn(Opcodes.ALOAD, 0);//  w  ww  .  j av  a2  s.  c o  m
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitVarInsn(Opcodes.ILOAD, 2);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V", false);
    m.visitInsn(Opcodes.RETURN);
    context.superClassName = "java/lang/Enum";

    filter.filter(m, context, output);

    assertIgnored(new Range(m.instructions.getFirst(), m.instructions.getLast()));
}

From source file:org.jacoco.core.internal.analysis.filter.EnumEmptyConstructorFilterTest.java

License:Open Source License

/**
 * <code><pre>//from   ww w.  j av a2  s .c om
 * enum E {
 *   ;
 *   private E() {
 *     ...
 *   }
 * }
 * </pre></code>
 */
@Test
public void should_not_filter_non_empty_constructor() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_PRIVATE, "<init>",
            "(Ljava/lang/String;I)V", null, null);
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitVarInsn(Opcodes.ILOAD, 2);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V", false);
    m.visitInsn(Opcodes.NOP);
    m.visitInsn(Opcodes.RETURN);
    context.superClassName = "java/lang/Enum";

    filter.filter(m, context, output);

    assertIgnored();
}

From source file:org.jacoco.core.internal.analysis.filter.EnumEmptyConstructorFilterTest.java

License:Open Source License

/**
 * <code><pre>/*www  .  jav  a  2 s . c  om*/
 * enum E {
 *   ;
 *   private E(long p) {
 *   }
 * }
 * </pre></code>
 */
@Test
public void should_not_filter_constructor_with_additional_parameters() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_PRIVATE, "<init>",
            "(Ljava/lang/String;IJ)V", null, null);
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitVarInsn(Opcodes.ILOAD, 2);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V", false);
    m.visitInsn(Opcodes.RETURN);
    context.superClassName = "java/lang/Enum";

    filter.filter(m, context, output);

    assertIgnored();
}

From source file:org.jacoco.core.internal.analysis.filter.EnumEmptyConstructorFilterTest.java

License:Open Source License

/**
 * <code><pre>/*from w  w  w  .  j  av  a2  s  .co m*/
 * enum E {
 *   ;
 *   private void method(String p1, int p2) {
 *   }
 * }
 * </pre></code>
 */
@Test
public void should_not_filter_non_constructor() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_PRIVATE, "method",
            "(Ljava/lang/String;I)V", null, null);
    m.visitInsn(Opcodes.NOP);
    context.superClassName = "java/lang/Enum";

    filter.filter(m, context, output);

    assertIgnored();
}

From source file:org.jacoco.core.internal.analysis.filter.EnumEmptyConstructorFilterTest.java

License:Open Source License

@Test
public void should_not_filter_non_Enum() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_PRIVATE, "<init>",
            "(Ljava/lang/String;I)V", null, null);
    m.visitInsn(Opcodes.NOP);//  w  ww  .  j a  va2 s  .c o  m

    filter.filter(m, context, output);

    assertIgnored();
}

From source file:org.jacoco.core.internal.analysis.filter.PrivateEmptyNoArgConstructorFilter.java

License:Open Source License

public void filter(final MethodNode methodNode, final IFilterContext context, final IFilterOutput output) {
    if ((methodNode.access & Opcodes.ACC_PRIVATE) != 0 && CONSTRUCTOR_NAME.equals(methodNode.name)
            && CONSTRUCTOR_DESC.equals(methodNode.desc)
            && new Matcher().match(methodNode, context.getSuperClassName())) {
        output.ignore(methodNode.instructions.getFirst(), methodNode.instructions.getLast());
    }//from   w  ww  .j  av a2  s . com
}

From source file:org.jacoco.core.internal.analysis.filter.PrivateEmptyNoArgConstructorFilterTest.java

License:Open Source License

@Test
public void test() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_PRIVATE, "<init>", "()V",
            null, null);/*from ww w  . j  a  va2  s.  c  om*/

    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    m.visitInsn(Opcodes.RETURN);

    filter.filter(m, context, output);

    assertMethodIgnored(m);
}

From source file:org.jacoco.core.internal.instr.CondyProbeArrayStrategyTest.java

License:Open Source License

@Test
public void should_add_bootstrap_method() {
    final ClassNode c = new ClassNode();
    strategy.addMembers(c, 1);//www.  j  a v  a  2  s  . c o m

    assertEquals(1, c.methods.size());

    final MethodNode m = c.methods.get(0);
    assertEquals(Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, m.access);
    assertEquals("$jacocoInit", m.name);
    assertEquals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)[Z", m.desc);

    assertEquals(4, m.maxStack);
    assertEquals(3, m.maxLocals);
}