Example usage for org.objectweb.asm Opcodes ACC_STATIC

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

Introduction

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

Prototype

int ACC_STATIC

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

Click Source Link

Usage

From source file:org.openjdk.jmh.generators.asm.ASMFieldInfo.java

License:Open Source License

@Override
public boolean isStatic() {
    return (access & Opcodes.ACC_STATIC) > 0;
}

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  a2  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.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**
 * Add the <clinit> method. This initializes the static fields that have initializers.
 * @param classRep//w  w w .j a v a 2s  . c  o  m
 * @param cv
 * @param initializeForAsserts
 * @throws JavaGenerationException
 */
private static void encodeClassInitializer(JavaClassRep classRep, ClassVisitor cv, boolean initializeForAsserts)
        throws JavaGenerationException {

    // Add initializers for the statically-initialized fields.
    final int nFields = classRep.getNFieldDeclarations();

    if (!classRep.hasInitializedStaticField() && !initializeForAsserts) {
        //we don't need to bother adding a static initializer if there are no static fields to initialize.
        //note that javac also has this optimization.
        return;
    }

    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);

    // Start the method's code.
    mv.visitCode();

    final JavaTypeName classRepTypeName = classRep.getClassName();

    /*
     * If this class contains assert statements we need to initialize the static final synthetic boolean field
     * '$assertionsDisabled'.
     * This is done by loading the Class constant for the top level class. The method 'desiredAssertionStatus()'
     * is then invoked on this Class object and the returned value is used to initialize $assertionsDisabled. 
     */
    if (initializeForAsserts) {
        // Get the fully-qualified internal class  name.    
        final String className = classRepTypeName.getJVMInternalName();

        // Get the top level class name.
        String topLevelClassName = getTopLevelClassJVMInternalName(className);

        // Load the Class constant for the top level class
        mv.visitLdcInsn(Type.getType("L" + topLevelClassName + ";"));

        // Now that we have the Class constant for the top level class we invoke the method
        // desiredAssertionStatus on it and use the resulting value to initialize the static field $assertionsDisabled in this class.
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "desiredAssertionStatus", "()Z");
        Label l0 = new Label();
        mv.visitJumpInsn(Opcodes.IFNE, l0);
        mv.visitInsn(Opcodes.ICONST_1);
        Label l1 = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, l1);
        mv.visitLabel(l0);
        mv.visitInsn(Opcodes.ICONST_0);
        mv.visitLabel(l1);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, className, "$assertionsDisabled", "Z");
    }

    for (int i = 0; i < nFields; ++i) {

        JavaFieldDeclaration fieldDecl = classRep.getFieldDeclaration(i);
        JavaExpression initializer = fieldDecl.getInitializer();

        if (initializer != null && Modifier.isStatic(fieldDecl.getModifiers())) {

            GenerationContext context = new GenerationContext(new HashMap<String, JavaTypeName>(),
                    new HashMap<String, Integer>(), 0, mv, classRepTypeName);

            //evaluate the initializer
            encodeExpr(initializer, context);

            //do the assignment
            mv.visitFieldInsn(Opcodes.PUTSTATIC, classRep.getClassName().getJVMInternalName(),
                    fieldDecl.getFieldName(), fieldDecl.getFieldType().getJVMDescriptor());
        }
    }

    // return.
    mv.visitInsn(Opcodes.RETURN);

    //mark the end of the method with a call to visitMaxs
    mv.visitMaxs(0, 0);

    // End the method.
    mv.visitEnd();
}

From source file:org.openspotlight.bundle.language.java.resolver.JavaGraphNodeSupport.java

License:Open Source License

/**
 * Adds the type on current context. This method should be used on current classpath elements only. The types declared outside
 * this classpath should be added with method {@link #addTypeOnAbstractContext(Class, String, String)}.
 * //from   ww w.  j av a2s . co  m
 * @param nodeType the node type
 * @param packageName the package name
 * @param nodeName the node name
 * @param access the access
 * @param parentType the parent type, if null will use package as parent
 * @return the t
 * @throws Exception the exception
 */
@SuppressWarnings("unchecked")
public <T extends JavaType> T addTypeOnCurrentContext(final Class<T> nodeType, final String packageName,
        final String nodeName, final int access, final Node parentType) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug(" adding type on current context " + packageName + "_" + nodeName + " with parent "
                + (parentType != null ? parentType.getName() : "null"));
    }

    if (usingCache && nodesFromThisContext.containsKey(packageName + nodeName)) {
        return (T) nodesFromThisContext.get(packageName + nodeName);
    }
    if (JavaTypePrimitive.class.equals(nodeType)) {
        final T newType = abstractContextRootNode.addChildNode(nodeType, nodeName);
        newType.setSimpleName(nodeName);
        newType.setQualifiedName(nodeName);
        return newType;
    }
    final JavaPackage newPackage = currentContextRootNode.addChildNode(JavaPackage.class, packageName);
    T newType = null;

    if (parentType != null) {
        newType = parentType.addChildNode(nodeType, nodeName);
    } else {
        newType = newPackage.addChildNode(nodeType, nodeName);
    }

    newType.setSimpleName(nodeName);
    newType.setQualifiedName(Strings.tryToRemoveBegginingFrom(JavaConstants.DEFAULT_PACKAGE + ".",
            packageName + "." + nodeName.replaceAll("[$]", ".")));
    session.addLink(PackageType.class, newPackage, newType, false);
    final boolean isPublic = (access & Opcodes.ACC_PUBLIC) != 0;
    final boolean isPrivate = (access & Opcodes.ACC_PRIVATE) != 0;
    final boolean isStatic = (access & Opcodes.ACC_STATIC) != 0;
    final boolean isFinal = (access & Opcodes.ACC_FINAL) != 0;
    final boolean isProtected = (access & Opcodes.ACC_PROTECTED) != 0;
    newType.setPublic(isPublic);
    newType.setPrivate(isPrivate);
    newType.setStatic(isStatic);
    newType.setFinal(isFinal);
    newType.setProtected(isProtected);

    final JavaPackage newAbstractPackage = abstractContextRootNode.addChildNode(JavaPackage.class, packageName);
    final JavaType newAbstractType = newAbstractPackage.addChildNode(JavaType.class, nodeName);
    newAbstractType.setQualifiedName(Strings.tryToRemoveBegginingFrom(JavaConstants.DEFAULT_PACKAGE + ".",
            packageName + "." + nodeName.replaceAll("[$]", ".")));
    newAbstractType.setSimpleName(nodeName);

    session.addLink(PackageType.class, newPackage, newType, false);
    session.addLink(AbstractTypeBind.class, newAbstractType, newType, false);
    nodesFromThisContext.put(packageName + nodeName, newType);
    if (logger.isInfoEnabled()) {
        logger.info("added class " + nodeType.getSimpleName() + " " + packageName + "." + nodeName);
    }
    return newType;
}

From source file:org.openspotlight.bundle.language.java.resolver.JavaGraphNodeSupport.java

License:Open Source License

/**
 * Insert field data.//from  w  w w .  j a v  a 2s  . c  om
 * 
 * @param field the field
 * @param fieldType the field type
 * @param access the access
 * @param isArray the is array
 * @param dimension the dimension
 * @throws Exception the exception
 */
private void insertFieldData(final JavaDataField field, final JavaType fieldType, final int access,
        final boolean isArray, final int dimension) throws Exception {
    final DataType fieldTypeLink = session.addLink(DataType.class, field, fieldType, false);
    fieldTypeLink.setArray(isArray);
    fieldTypeLink.setArrayDimension(dimension);
    final boolean isFieldPublic = (access & Opcodes.ACC_PUBLIC) != 0;
    final boolean isFieldPrivate = (access & Opcodes.ACC_PRIVATE) != 0;
    final boolean isFieldStatic = (access & Opcodes.ACC_STATIC) != 0;
    final boolean isFieldFinal = (access & Opcodes.ACC_FINAL) != 0;
    final boolean isFieldProtected = (access & Opcodes.ACC_PROTECTED) != 0;
    final boolean isFieldTransient = (access & Opcodes.ACC_TRANSIENT) != 0;
    final boolean isFieldVolatile = (access & Opcodes.ACC_VOLATILE) != 0;
    field.setPublic(isFieldPublic);
    field.setPrivate(isFieldPrivate);
    field.setStatic(isFieldStatic);
    field.setFinal(isFieldFinal);
    field.setProtected(isFieldProtected);
    field.setTransient(isFieldTransient);
    field.setVolatile(isFieldVolatile);
}

From source file:org.openspotlight.bundle.language.java.resolver.JavaGraphNodeSupport.java

License:Open Source License

/**
 * Sets the method data.// www . j av  a 2 s.c  o  m
 * 
 * @param method the method
 * @param access the access
 */
private void setMethodData(final JavaMethod method, final int access) {
    final boolean isMethodPublic = (access & Opcodes.ACC_PUBLIC) != 0;
    final boolean isMethodPrivate = (access & Opcodes.ACC_PRIVATE) != 0;
    final boolean isMethodStatic = (access & Opcodes.ACC_STATIC) != 0;
    final boolean isMethodFinal = (access & Opcodes.ACC_FINAL) != 0;
    final boolean isMethodProtected = (access & Opcodes.ACC_PROTECTED) != 0;
    final boolean isMethodSynchronized = (access & Opcodes.ACC_SYNCHRONIZED) != 0;
    method.setPublic(isMethodPublic);
    method.setPrivate(isMethodPrivate);
    method.setStatic(isMethodStatic);
    method.setFinal(isMethodFinal);
    method.setProtected(isMethodProtected);
    method.setSynchronized(isMethodSynchronized);
}

From source file:org.osjava.jardiff.Tools.java

License:Apache License

/**
 * Returns whether a method's newAccess is incompatible with oldAccess
 * following <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html">Java Language Specification, Java SE 7 Edition</a>:
 * <ul>//from   w w w.j  ava 2s .  com
 *   <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html#jls-13.4.9">13.4.7 Access to Members and Constructors</a><ul>
 *     <li>Changing the declared access of a member or constructor to permit less access
 *        <b>may break compatibility</b> with pre-existing binaries, causing a linkage error to be thrown when these binaries are resolved.
 *     </li>
 *     <li>The binary format is defined so that changing a member or constructor to be more accessible does not cause a
 *         linkage error when a subclass (already) defines a method to have less access.
 *     </li>
 *     </ul></li>
 *   <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html#jls-13.4.16">13.4.16 abstract Methods</a><ul>
 *     <li>Changing a method that is declared abstract to no longer be declared abstract
 *         <b>does not break compatibility</b> with pre-existing binaries.</li>
 *     <li>Changing a method that is not declared abstract to be declared abstract
 *         <b>will break compatibility</b> with pre-existing binaries that previously invoked the method, causing an AbstractMethodError.</li>
 *     </ul></li>
 *   <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html#jls-13.4.17">13.4.17 final</a><ul>
 *     <li>Changing a method that is declared final to no longer be declared final
 *         <b>does not break compatibility</b> with pre-existing binaries.</li>
 *     <li>Changing an instance method that is not declared final to be declared final
 *         <b>may break compatibility</b> with existing binaries that depend on the ability to override the method.</li>
 *     <li>Changing a class (static) method that is not declared final to be declared final
 *         <b>does not break compatibility</b> with existing binaries, because the method could not have been overridden.</li>
 *     </ul></li>
 *   <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html#jls-13.4.18">13.4.18 native Methods</a><ul>
 *     <li>Adding or deleting a native modifier of a method
 *         <b>does not break compatibility</b> with pre-existing binaries.</li>
 *     </ul></li>
 *   <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html#jls-13.4.19">13.4.19 static Methods</a><ul>
 *     <li>If a method that is not declared private is also declared static (that is, a class method)
 *         and is changed to not be declared static (that is, to an instance method), or vice versa,
 *         then <i>compatibility with pre-existing binaries may be broken</i>, resulting in a linkage time error,
 *         namely an IncompatibleClassChangeError, if these methods are used by the pre-existing binaries.
 *         Such changes <b>break functional backward compatibility</b>!</li>
 *     </ul></li>
 *   <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html#jls-13.4.20">13.4.20 synchronized Methods</a><ul>
 *     <li>Adding or deleting a synchronized modifier of a method
 *         <b>does not break compatibility</b> with pre-existing binaries.</li>
 *   </ul></li>
 *   <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html#jls-13.4.21">13.4.21 Method and Constructor Throws</a><ul>
 *     <li>Changes to the throws clause of methods or constructors
 *         <b>do not break compatibility</b> with pre-existing binaries; these clauses are checked only at compile time.</li>
 *   </ul></li>
 * </ul>
 *
 * @param oldAccess
 * @param newAccess
 * @return
 */
public static boolean isMethodAccessChange(final int oldAccess, final int newAccess) {
    if (isAccessIncompatible(oldAccess, newAccess)) {
        return true; // 13.4.7
    }
    if (not(oldAccess, Opcodes.ACC_ABSTRACT) && has(newAccess, Opcodes.ACC_ABSTRACT)) {
        return true; // 13.4.16 #2
    } else if (not(oldAccess, Opcodes.ACC_FINAL) && not(oldAccess, Opcodes.ACC_STATIC)
            && has(newAccess, Opcodes.ACC_FINAL)) {
        return true; // 13.4.17 #2 excluding and #3
    } else {
        final int compatibleChanges = Opcodes.ACC_ABSTRACT | // 13.4.16 #1
                Opcodes.ACC_FINAL | // 13.4.17 #1
                Opcodes.ACC_NATIVE | // 13.4.18 #1
                Opcodes.ACC_SYNCHRONIZED; // 13.4.20 #1
        final int accessPermissions = Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE;
        final int oldAccess2 = oldAccess & ~compatibleChanges & ~accessPermissions;
        final int newAccess2 = newAccess & ~compatibleChanges & ~accessPermissions;
        return oldAccess2 != newAccess2;
    }
}

From source file:org.osjava.jardiff.ToolsTest.java

License:Apache License

@Test
public void isFieldAccessChange() {
    // A field can't become final.
    assertTrue(Tools.isFieldAccessChange(0, Opcodes.ACC_FINAL));
    assertTrue(Tools.isFieldAccessChange(0, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL));
    // ... but can become non-final.
    assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_FINAL, 0));
    assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));

    // A field can become more accessible
    assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PUBLIC));
    assertFalse(Tools.isFieldAccessChange(0, Opcodes.ACC_PUBLIC));
    assertFalse(Tools.isFieldAccessChange(0, Opcodes.ACC_PROTECTED));
    assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PRIVATE, Opcodes.ACC_PUBLIC));
    assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PRIVATE, Opcodes.ACC_PROTECTED));
    assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PRIVATE, 0));
    // ...but can't become less accessible
    assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, 0));
    assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PROTECTED));
    assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PRIVATE));
    assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_PROTECTED, 0));
    assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PRIVATE));
    assertTrue(Tools.isFieldAccessChange(0, Opcodes.ACC_PRIVATE));

    // A field can't change static
    assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_STATIC, 0));
    assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));

    // A field can't change volatile
    assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_VOLATILE, 0));
    assertTrue(Tools.isFieldAccessChange(Opcodes.ACC_VOLATILE + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));

    // A field can change transient
    assertFalse(Tools.isFieldAccessChange(0, Opcodes.ACC_TRANSIENT));
    assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC + Opcodes.ACC_TRANSIENT));
    assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_TRANSIENT, 0));
    assertFalse(Tools.isFieldAccessChange(Opcodes.ACC_PUBLIC + Opcodes.ACC_TRANSIENT, Opcodes.ACC_PUBLIC));
}

From source file:org.osjava.jardiff.ToolsTest.java

License:Apache License

@Test
public void isMethodAccessChange() {
    // A non-static method can't become final.
    assertTrue(Tools.isMethodAccessChange(0, Opcodes.ACC_FINAL));
    assertTrue(Tools.isMethodAccessChange(0, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL));
    // ... but can become non-final.
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_FINAL, 0));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));
    // ... but a static method can become final!
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_STATIC, Opcodes.ACC_STATIC + Opcodes.ACC_FINAL));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC,
            Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL));

    // A method can become more accessible
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PUBLIC));
    assertFalse(Tools.isMethodAccessChange(0, Opcodes.ACC_PUBLIC));
    assertFalse(Tools.isMethodAccessChange(0, Opcodes.ACC_PROTECTED));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PRIVATE, Opcodes.ACC_PUBLIC));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PRIVATE, Opcodes.ACC_PROTECTED));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PRIVATE, 0));
    // ...but can't become less accessible
    assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_FINAL + Opcodes.ACC_PUBLIC, 0));
    assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PROTECTED));
    assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PRIVATE));
    assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PROTECTED, 0));
    assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PRIVATE));
    assertTrue(Tools.isMethodAccessChange(0, Opcodes.ACC_PRIVATE));

    // A class or method can become concrete.
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_ABSTRACT, 0));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_ABSTRACT + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));
    assertFalse(//w ww.j  a va 2 s  . co m
            Tools.isMethodAccessChange(Opcodes.ACC_ABSTRACT + Opcodes.ACC_PROTECTED, Opcodes.ACC_PROTECTED));
    // ...but can't become abstract
    assertTrue(Tools.isMethodAccessChange(0, Opcodes.ACC_ABSTRACT));
    assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC + Opcodes.ACC_ABSTRACT));
    assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_PROTECTED, Opcodes.ACC_PROTECTED + Opcodes.ACC_ABSTRACT));

    // A method can't change static
    assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_STATIC, 0));
    assertTrue(Tools.isMethodAccessChange(Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC));

    // A method can change synchronized
    assertFalse(Tools.isMethodAccessChange(0, Opcodes.ACC_SYNCHRONIZED));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC + Opcodes.ACC_SYNCHRONIZED));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_SYNCHRONIZED, 0));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC + Opcodes.ACC_SYNCHRONIZED, Opcodes.ACC_PUBLIC));

    // A method can change native
    assertFalse(Tools.isMethodAccessChange(0, Opcodes.ACC_NATIVE));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC + Opcodes.ACC_NATIVE));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_NATIVE, 0));
    assertFalse(Tools.isMethodAccessChange(Opcodes.ACC_PUBLIC + Opcodes.ACC_NATIVE, Opcodes.ACC_PUBLIC));
}

From source file:org.parboiled.compiler.notNullVerification.MyMethodAdapter.java

License:Apache License

@Override
public void visitCode() {
    if (myNotNullParams.size() > 0) {
        myStartGeneratedCodeLabel = new Label();
        mv.visitLabel(myStartGeneratedCodeLabel);
    }//from ww  w .  j a va 2 s  .co  m
    for (Object myNotNullParam : myNotNullParams) {
        int var = ((access & Opcodes.ACC_STATIC) == 0) ? 1 : 0;
        int param = (Integer) myNotNullParam;
        for (int i = 0; i < param + startParameter; ++i) {
            var += args[i].getSize();
        }
        mv.visitVarInsn(Opcodes.ALOAD, var);

        Label end = new Label();
        mv.visitJumpInsn(Opcodes.IFNONNULL, end);

        generateThrow(
                "java/lang/IllegalArgumentException", toOrdinalString(param) + " argument of method "
                        + getFullMethodName() + "(...) corresponds to @NotNull parameter and must not be null",
                end);
    }
}