Example usage for org.objectweb.asm Opcodes V1_6

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

Introduction

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

Prototype

int V1_6

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

Click Source Link

Usage

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

/**
 * Returns the enhanced class bytecode./*from   w  ww .  ja va2 s .  c o  m*/
 * 
 * @param clazz
 *            the class to enhance
 * @return the enhanced class
 * @throws Exception
 *             thrown in case of an error
 * 
 * @since 2.0.0
 */
//@formatter:off
public static byte[] create(Class<?> clazz) throws Exception {

    final String enhancingClassName = Type.getInternalName(clazz);
    final String enhancedClassName = enhancingClassName + Enhancer.SUFFIX_ENHANCED;
    final String descEnhancer = Enhancer.makeClassDesc(enhancedClassName);

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, enhancedClassName, null, enhancingClassName,
            new String[] { Type.getInternalName(EnhancedInstance.class) });

    // Field: serialVersionUID
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
            Enhancer.FIELD_SERIAL_VERSION_UID, Type.getDescriptor(Long.TYPE), null, Long.valueOf(1L))
            .visitEnd();

    // Container fields
    cw.visitField(Opcodes.ACC_PRIVATE, Enhancer.FIELD_ENHANCED_INITIALIZED, Enhancer.DESCRIPTOR_BOOLEAN, null,
            null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE, Enhancer.FIELD_ENHANCED_INTERNAL, Enhancer.DESCRIPTOR_BOOLEAN, null,
            null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_TRANSIENT, Enhancer.FIELD_ENHANCED_ID,
            Enhancer.DESCRIPTOR_OBJECT, null, null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_TRANSIENT, Enhancer.FIELD_ENHANCED_TYPE,
            Enhancer.DESCRIPTOR_CLASS, null, null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_TRANSIENT,
            Enhancer.FIELD_ENHANCED_SESSION, Enhancer.DESCRIPTOR_SESSION, null, null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_TRANSIENT, Enhancer.FIELD_ENHANCED_MANAGED_INSTANCE,
            Enhancer.DESCRIPTOR_MANAGED_INSTANCE, null, null).visitEnd();

    // Constructors
    Enhancer.createNoArgConstructor(enhancingClassName, enhancedClassName, descEnhancer, cw);
    Enhancer.createContainerConstructor(enhancingClassName, enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodIsInitialized(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodSetInitialized(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodCheck(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodGetManagedInstance(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodSetManagedInstance(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodSetInternal(enhancedClassName, descEnhancer, cw);

    final Map<String, Method> methods = Maps.newHashMap();

    Class<?> currentClass = clazz;
    while (currentClass != Object.class) { // we are not interested in Object.class
        for (final Method method : currentClass.getDeclaredMethods()) {
            int modifiers = method.getModifiers();

            if (Modifier.isAbstract(modifiers) || Modifier.isStatic(modifiers) || Modifier.isPrivate(modifiers)
                    || method.isSynthetic() || method.isBridge()) {
                continue;
            }

            // Filter out the details that we are not interested
            modifiers &= Modifier.ABSTRACT;
            modifiers &= Modifier.FINAL;
            modifiers &= Modifier.NATIVE;
            modifiers &= Modifier.PRIVATE;
            modifiers &= Modifier.PROTECTED;
            modifiers &= Modifier.STATIC;
            modifiers &= Modifier.STRICT;

            if ((modifiers == Modifier.PUBLIC) || (modifiers == 0)) {
                // we are not interested in the return type to omit the overridden methods
                final String desc = method.getName()
                        + Enhancer.makeDescription(Void.TYPE, method.getParameterTypes());

                if (methods.get(desc) == null) {
                    methods.put(desc, method);
                }
            }
        }

        currentClass = currentClass.getSuperclass();
    }

    for (final Method method : methods.values()) {
        if (!Enhancer.IGNORED_METHODS.contains(method.getName())) {
            Enhancer.createOverrriddenMethod(enhancingClassName, enhancedClassName, descEnhancer, cw, method);
        }
    }

    cw.visitEnd();

    return cw.toByteArray();
}

From source file:org.boretti.drools.integration.drools5.DroolsClassVisitor.java

License:Open Source License

@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {// w  ww  .j av a  2s  .  c  om
    if (version < Opcodes.V1_6) {
        allowed = false;
        logger.warn("Version of the class " + name + " is not enough to be supported");
    } else if ((access & Opcodes.ACC_INTERFACE) > 0) {
        allowed = false;
    }
    me = name;
    super.visit(version, access, name, signature, superName, interfaces);
}

From source file:org.eclipselabs.nullness.NullnessCompiler.java

License:Open Source License

private int getClassWriterFlags(int version) {
    return version >= Opcodes.V1_6 && version != Opcodes.V1_1 ? ClassWriter.COMPUTE_FRAMES
            : ClassWriter.COMPUTE_MAXS;//from  w  w w. ja va  2s .  c o  m
}

From source file:org.gradle.model.internal.manage.schema.extract.ManagedCollectionProxyClassGenerator.java

License:Apache License

private void generateClass(ClassWriter visitor, Type generatedType, Type superclassType, Type publicType) {
    visitor.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, generatedType.getInternalName(), null,
            superclassType.getInternalName(), new String[] { publicType.getInternalName() });
}

From source file:org.jacoco.core.internal.analysis.ContentTypeDetector.java

License:Open Source License

private static int determineType(final InputStream in) throws IOException {
    switch (readInt(in)) {
    case ZIPFILE:
        return ZIPFILE;
    case CLASSFILE:
        // also verify version to distinguish from Mach Object files:
        switch (readInt(in)) {
        case Opcodes.V1_1:
        case Opcodes.V1_2:
        case Opcodes.V1_3:
        case Opcodes.V1_4:
        case Opcodes.V1_5:
        case Opcodes.V1_6:
        case Opcodes.V1_7:
            return CLASSFILE;
        }/*  w w  w  .  j  ava2  s .com*/
    }
    return UNKNOWN;
}

From source file:org.jacoco.core.internal.ContentTypeDetector.java

License:Open Source License

private static int determineType(final InputStream in) throws IOException {
    final int header = readInt(in);
    switch (header) {
    case ZIPFILE:
        return ZIPFILE;
    case PACK200FILE:
        return PACK200FILE;
    case CLASSFILE:
        // also verify version to distinguish from Mach Object files:
        switch (readInt(in)) {
        case Opcodes.V1_1:
        case Opcodes.V1_2:
        case Opcodes.V1_3:
        case Opcodes.V1_4:
        case Opcodes.V1_5:
        case Opcodes.V1_6:
        case Opcodes.V1_7:
            return CLASSFILE;
        }// w ww .ja va2  s.c  o  m
    }
    if ((header & 0xffff0000) == GZFILE) {
        return GZFILE;
    }
    return UNKNOWN;
}

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

License:Open Source License

@Override
public void visit(final int version, final int access, final String name, final String signature,
        final String superName, final String[] interfaces) {
    this.className = name;
    withFrames = (version & 0xff) >= Opcodes.V1_6;
    if ((access & Opcodes.ACC_INTERFACE) == 0) {
        this.probeArrayStrategy = new ClassTypeStrategy();
    } else {//from w ww.  j a  v  a2 s . c o m
        this.probeArrayStrategy = new InterfaceTypeStrategy();
    }
    super.visit(version, access, name, signature, superName, interfaces);
}

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

License:Open Source License

@Test
public void testClass6() {
    final IProbeArrayStrategy strategy = test(Opcodes.V1_6, 0, false, true, true);
    assertEquals(ClassFieldProbeArrayStrategy.class, strategy.getClass());
    assertDataField(InstrSupport.DATAFIELD_ACC);
    assertInitMethod(true);/*from www .j  av a2  s  . c om*/
}

From source file:org.jacoco.core.test.validation.FramesTest.java

License:Open Source License

private byte[] calculateFrames(byte[] source) {
    ClassReader rc = new ClassReader(source);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    // Adjust Version to 1.6 to enable frames:
    rc.accept(new ClassVisitor(Opcodes.ASM4, cw) {

        @Override/*  www. j  a va  2  s . c  om*/
        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
            super.visit(Opcodes.V1_6, access, name, signature, superName, interfaces);
        }
    }, 0);
    return cw.toByteArray();
}

From source file:org.jacoco.core.test.validation.java5.FramesTest.java

License:Open Source License

private byte[] calculateFrames(byte[] source) {
    ClassReader rc = InstrSupport.classReaderFor(source);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    // Adjust Version to 1.6 to enable frames:
    rc.accept(new ClassVisitor(InstrSupport.ASM_API_VERSION, cw) {

        @Override/*from  w  ww  . j av a2  s.  c  o m*/
        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
            super.visit(Opcodes.V1_6, access, name, signature, superName, interfaces);
        }
    }, 0);
    return cw.toByteArray();
}