Example usage for org.objectweb.asm Opcodes ACC_SYNTHETIC

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

Introduction

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

Prototype

int ACC_SYNTHETIC

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

Click Source Link

Usage

From source file:org.mbte.groovypp.compiler.TypeUtil.java

License:Apache License

public static MethodNode getReferenceBoxingMethod(ClassNode classNode, ClassNode arg) {
    if (classNode instanceof ClosureClassNode || (classNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0
            || ClassHelper.isPrimitiveType(classNode))
        return null;
    if (!isTransparentClass(classNode))
        return null;
    MethodNode method = MethodSelection.findPublicMethodInClass(classNode, "set", new ClassNode[] { arg });
    if (method == null || method.getParameters().length != 1)
        return null;
    ClassNode paramType = method.getParameters()[0].getType();
    ClassNode substituted = getSubstitutedType(paramType, method.getDeclaringClass(), classNode);
    if (!isAssignableFrom(ClassHelper.getWrapper(substituted), ClassHelper.getWrapper(arg)))
        return null;
    return method;
}

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**
 * Accepts a visitor and fills it with information for a given class.
 * @param classRep the representation for the top-level class.
 * @param cv the class visitor to accept.
 * @throws JavaGenerationException/*w w  w .  ja  v a  2  s .c o  m*/
 */
private static void encodeClassAcceptingVisitor(JavaClassRep classRep, ClassVisitor cv)
        throws JavaGenerationException {

    // Get the fully-qualified internal class and superclass names.    
    final JavaTypeName classRepTypeName = classRep.getClassName();
    final String className = classRepTypeName.getJVMInternalName();
    final String superclassName = classRep.getSuperclassName().getJVMInternalName();

    // Determine if the class or any inner class contains assert statements.
    int assertPresence = classRep.getAssertionContainment();
    if ((assertPresence & JavaClassRep.ASSERTS_UNKNOWN) > 0) {
        assertPresence = AsmJavaBytecodeGenerator.containsAsserts(classRep);
    }

    // Create the interfaces[] array.
    final int nInterfaces = classRep.getNInterfaces();
    final String[] interfaces = new String[nInterfaces];
    for (int i = 0; i < nInterfaces; i++) {
        interfaces[i] = classRep.getInterface(i).getJVMInternalName();
    }

    //ACC_SUPER flag should always be set for the flags defining a class file.
    //(see the Java language specification under ACC_SUPER in the index. The flag is not set only
    //by older Java compilers and exists for backwards compatibility reasons).
    int classModifiers = classRep.getModifiers() | Opcodes.ACC_SUPER;
    //static inner classes are marked with the static modifier, but this is not a valid access flag for a class.
    classModifiers &= ~Modifier.STATIC;

    // We aren't generating or using generics, so the signature can be null
    String classSignature = null;
    cv.visit(Opcodes.V1_5, classModifiers, className, classSignature, superclassName, interfaces);

    //sourcefileName = null, since this class was not compiled from a Java source file.
    //However, if we are debugging byte codes, use a "fake" source file name as if this class were generated from a Java source file.
    //This eliminates a trivial difference between the byte code generated by ASM and that of javac and makes inspecting the
    //differences in a differencing tool easier.
    String sourceFileName = null;
    //        if (AsmJavaBytecodeGenerator.DEBUG_GENERATED_BYTECODE) {               
    String unqualifiedName = classRepTypeName.getUnqualifiedJavaSourceName();
    int dotPosition = unqualifiedName.indexOf('.');
    if (dotPosition != -1) {
        //get the top level class name.
        unqualifiedName = unqualifiedName.substring(0, dotPosition);
    }
    sourceFileName = unqualifiedName + ".java";
    //        }

    cv.visitSource(sourceFileName, null);

    //add the fields        
    for (int i = 0, nFields = classRep.getNFieldDeclarations(); i < nFields; ++i) {

        JavaFieldDeclaration fieldDeclaration = classRep.getFieldDeclaration(i);

        //todoBI it may be more efficient to handle initializers for static-fields here in the cases where it is possible
        //(int, long, float, double, String).
        cv.visitField(fieldDeclaration.getModifiers(), fieldDeclaration.getFieldName(),
                fieldDeclaration.getFieldType().getJVMDescriptor(), null, null);
    }

    /*
     * When dealing with assert statements there is possibly an additional field that needs to be
     * added.
     * If a class contains an assert statement a static final synthetic boolean field called '$assertionsDisabled' is
     * added.  This field is initialized in the class static initializer and is used to determine whether to
     * check or skip assertions.
     */
    if (assertPresence != JavaClassRep.ASSERTS_NONE) {

        if ((assertPresence & JavaClassRep.ASSERTS_IN_CLASS) > 0) {
            // We need to add a static final synthetic boolean field to indicate the enabled/disabled state of assertions.
            cv.visitField(Opcodes.ACC_FINAL + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC, "$assertionsDisabled",
                    "Z", null, null);
        }
    }

    //add the constructors
    final int nConstructors = classRep.getNConstructors();
    if (nConstructors == 0) {
        //if empty, add the default constructor.             

        JavaConstructor defaultConstructor = new JavaConstructor(Modifier.PUBLIC,
                ((JavaTypeName.Reference.Object) classRepTypeName).getBaseName());
        defaultConstructor.addStatement(new JavaStatement.ReturnStatement());
        encodeConstructor(classRep, defaultConstructor, cv);

    } else {

        for (int i = 0; i < nConstructors; ++i) {
            encodeConstructor(classRep, classRep.getConstructor(i), cv);
        }
    }

    //add the methods
    for (int i = 0, nMethods = classRep.getNMethods(); i < nMethods; ++i) {

        encodeMethod(classRep, classRep.getMethod(i), cv);
    }

    //add the initializers for the static fields
    encodeClassInitializer(classRep, cv, (assertPresence & JavaClassRep.ASSERTS_IN_CLASS) > 0);

    //add the inner classes (these are basically just references to the inner classes)

    //if classRep itself is an inner class, call visitInnerClass on itself. This is what the eclipse java compiler does.
    //javac annotates for every inner class reference occurring within the class file e.g. a field declared of inner class type,
    //an instance of expression of inner class type, a throws declaration on a method where an inner class is thrown.
    if (classRepTypeName.isInnerClass()) {

        JavaTypeName.Reference.Object classTypeName = (JavaTypeName.Reference.Object) classRepTypeName;
        String internalImportName = classTypeName.getImportName().replace('.', '/');

        cv.visitInnerClass(classTypeName.getJVMInternalName(), internalImportName, classTypeName.getBaseName(),
                classRep.getModifiers());
    }

    /*
     * Previously we would call visitInnerClass for any inner classes associated with this class.  However,
     * we are no longer doing this.  
     * Bytecode is generated in different scenarios (i.e. static generation, dynamic generation, etc).  In some
     * scenarios inner classes are generated separately from the containing class.  In order to keep the generated
     * bytecode consistent between the dynamic and static scenarios wer are not adding the attributes for contained 
     * inner classes to the bytecode.
     * 
     for (int i = 0, nInnerClasses = classRep.getNInnerClasses(); i < nInnerClasses; ++i) {
            
    JavaClassRep innerClass = classRep.getInnerClass(i);
    JavaTypeName.Reference.Object innerClassTypeName = (JavaTypeName.Reference.Object)innerClass.getClassName();            
            
    cw.visitInnerClass(innerClassTypeName.getJVMInternalName(), className, innerClassTypeName.getBaseName(), innerClass.getModifiers());
    }               
            
    */

    cv.visitEnd();
}

From source file:org.rascalmpl.library.lang.java.m3.internal.JarConverter.java

License:Open Source License

private void emitFields(List<FieldNode> fields) {
    try {// w ww . j  av  a 2 s.  c  o  m
        for (int i = 0; i < fields.size(); ++i) {
            FieldNode field = fields.get(i);

            if ((field.access & Opcodes.ACC_SYNTHETIC) != 0)
                continue;

            if (field.name.startsWith("this$")) {
                if ((field.desc.length() > 0) && (className
                        .contains(field.desc.substring(1, field.desc.length() - 1).replace('$', '/') + "/")))

                    break;
            }

            boolean isEnum = (field.access & Opcodes.ACC_ENUM) != 0;
            String fieldScheme = isEnum ? "java+enumConstant" : "java+field";

            // System.out.println("Debug......." + field.name);
            this.insert(this.declarations, values.sourceLocation(fieldScheme, "", LogPath + "/" + field.name),
                    values.sourceLocation(jarFile + "!" + ClassFile));

            // Containment of fields.
            this.insert(this.containment, values.sourceLocation(classScheme, "", LogPath),
                    values.sourceLocation(fieldScheme, "", LogPath + "/" + field.name));

            if (!isEnum) {
                // The jvm acces codes specify 15 different modifiers (more then in the Java language
                // itself)
                for (int fs = 0; fs < 15; fs++) {
                    if ((field.access & (0x0001 << fs)) != 0) {
                        this.insert(this.modifiers,
                                values.sourceLocation("java+field", "", LogPath + "/" + field.name),
                                mapFieldAccesCode(1 << fs, FIELDE));
                    }
                }
            }
            // Put deprecated field in the annotations anno.
            if ((field.access & 0x20000) == 0x20000)
                this.insert(this.annotations,
                        values.sourceLocation("java+field", "", LogPath + "/" + field.name),
                        values.sourceLocation("java+interface", "", "/java/lang/Deprecated"));
            // <|java+method:///Main/Main/FindMe(java.lang.String)|,|java+interface:///java/lang/Deprecated|>,

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.revapi.java.compilation.ClassTreeInitializer.java

License:Apache License

private static boolean isAccessible(int access) {
    return (access & Opcodes.ACC_SYNTHETIC) == 0 && (access & Opcodes.ACC_BRIDGE) == 0
            && ((access & Opcodes.ACC_PUBLIC) != 0 || (access & Opcodes.ACC_PROTECTED) != 0);
}

From source file:org.sonar.java.resolve.FlagsTest.java

License:Open Source License

/**
 * Flags can be easily loaded from class-files into symbols.
 *///  www .j a  v a 2s .  co  m
@Test
public void flags_match_asm_opcodes() {
    assertThat(Flags.PUBLIC).isEqualTo(Opcodes.ACC_PUBLIC);
    assertThat(Flags.PRIVATE).isEqualTo(Opcodes.ACC_PRIVATE);
    assertThat(Flags.PROTECTED).isEqualTo(Opcodes.ACC_PROTECTED);
    assertThat(Flags.STATIC).isEqualTo(Opcodes.ACC_STATIC);
    assertThat(Flags.FINAL).isEqualTo(Opcodes.ACC_FINAL);
    assertThat(Flags.SYNCHRONIZED).isEqualTo(Opcodes.ACC_SYNCHRONIZED);
    assertThat(Flags.VOLATILE).isEqualTo(Opcodes.ACC_VOLATILE);
    assertThat(Flags.TRANSIENT).isEqualTo(Opcodes.ACC_TRANSIENT);
    assertThat(Flags.NATIVE).isEqualTo(Opcodes.ACC_NATIVE);
    assertThat(Flags.INTERFACE).isEqualTo(Opcodes.ACC_INTERFACE);
    assertThat(Flags.ABSTRACT).isEqualTo(Opcodes.ACC_ABSTRACT);
    assertThat(Flags.STRICTFP).isEqualTo(Opcodes.ACC_STRICT);
    assertThat(Flags.SYNTHETIC).isEqualTo(Opcodes.ACC_SYNTHETIC);
    assertThat(Flags.ANNOTATION).isEqualTo(Opcodes.ACC_ANNOTATION);
    assertThat(Flags.ENUM).isEqualTo(Opcodes.ACC_ENUM);
}

From source file:org.sonatype.guice.bean.scanners.QualifiedTypeVisitor.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) {
    if ((access & (Opcodes.ACC_INTERFACE | access & Opcodes.ACC_ABSTRACT | Opcodes.ACC_SYNTHETIC)) == 0) {
        clazzName = name; // concrete type
    }/*  w  w w  . j a v  a  2s.  co  m*/
}

From source file:org.sonatype.guice.nexus.scanners.NexusTypeVisitor.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) {
    final String clazz = name.replace('/', '.');
    nexusTypeListener.hear(clazz);// w w w.  j a v a 2s .com

    if ((access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_SYNTHETIC)) == 0) {
        scanForNexusMarkers(clazz, interfaces);
    }
    plexusTypeVisitor.visit(version, access, name, signature, superName, interfaces);
}

From source file:org.sonatype.guice.plexus.scanners.PlexusTypeVisitor.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) {
    if ((access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_SYNTHETIC)) == 0) {
        implementation = name.replace('/', '.');
    }//  w  w  w  . j av  a  2  s  . c o  m
    qualifiedTypeVisitor.visit(version, access, name, signature, superName, interfaces);
}

From source file:org.spockframework.compiler.AstUtil.java

License:Apache License

public static boolean isSynthetic(MethodNode method) {
    return method.isSynthetic() || (method.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0;
}

From source file:org.spockframework.compiler.SpecRewriter.java

License:Apache License

private void createSharedFieldGetter(Field field) {
    String getterName = "get" + MetaClassHelper.capitalize(field.getName());
    MethodNode getter = spec.getAst().getMethod(getterName, Parameter.EMPTY_ARRAY);
    if (getter != null) {
        errorReporter.error(field.getAst(),
                "@Shared field '%s' conflicts with method '%s'; please rename either of them", field.getName(),
                getter.getName());/*w w w. jav  a2  s.  co m*/
        return;
    }

    BlockStatement getterBlock = new BlockStatement();
    getter = new MethodNode(getterName,
            determineVisibilityForSharedFieldAccessor(field) | Opcodes.ACC_SYNTHETIC, field.getAst().getType(),
            Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);

    getterBlock.addStatement(
            new ReturnStatement(new ExpressionStatement(new AttributeExpression(getSharedInstance(),
                    // use internal name
                    new ConstantExpression(field.getAst().getName())))));

    getter.setSourcePosition(field.getAst());
    spec.getAst().addMethod(getter);
}