Example usage for org.objectweb.asm Opcodes ACC_SYNCHRONIZED

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

Introduction

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

Prototype

int ACC_SYNCHRONIZED

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

Click Source Link

Usage

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>/*  www.j  a  v a 2s.co  m*/
 *   <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 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  w  w  .  ja v a 2s .  c  o  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.rascalmpl.library.lang.java.m3.internal.JarConverter.java

License:Open Source License

private IConstructor mapFieldAccesCode(int code, int where) {
    // Check the original M3 implementation for possible IConstructor types.
    switch (code) {
    case Opcodes.ACC_PUBLIC:
        return constructModifierNode("public");
    case Opcodes.ACC_PRIVATE:
        return constructModifierNode("private");
    case Opcodes.ACC_PROTECTED:
        return constructModifierNode("protected");
    case Opcodes.ACC_STATIC:
        return constructModifierNode("static");
    case Opcodes.ACC_FINAL:
        return constructModifierNode("final");
    case Opcodes.ACC_SYNCHRONIZED:
        if (where == CLASSE)
            return null;
        return constructModifierNode("synchronized");
    case Opcodes.ACC_ABSTRACT:
        return constructModifierNode("abstract");
    case Opcodes.ACC_VOLATILE:
        return constructModifierNode("volatile");
    case Opcodes.ACC_TRANSIENT:
        return constructModifierNode("transient");
    case Opcodes.ACC_NATIVE:
        return constructModifierNode("native");

    // TODO: GIT PULL/MERGE ORIGINAL RASCAL VERSION < 2013-11-30 (Shahin commit)
    // case Opcodes.ACC_DEPRECATED:
    // return constructModifierNode("deprecated");

    default:// w  w w  .  j  a va2  s .  c o  m
        return null;
    }
}

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 av a 2 s. com
@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.springframework.migrationanalyzer.contributions.bytecode.AsmUtils.java

License:Apache License

/**
 * Converts the given bitwise ORed <code>modifiers</code> into a {@link String}.
 * //from   w ww.j  a va  2  s  . c  o m
 * <p/>
 * 
 * The returned String should reflect the ordering recommended in the JLS:
 * 
 * <ul>
 * <li>Classes: <code>public protected private abstract static final strictfp</code></li>
 * <li>Fields: <code>public protected private static final transient volatile</code></li>
 * <li>Constructors: <code>public protected private</code></li>
 * <li>Methods: <code>public protected private abstract static final synchronized native strictfp</code></li>
 * </ul>
 * 
 * @param modifiers the modifiers
 * 
 * @return The String representation of the modifiers
 * 
 * @see Opcodes
 */
// CHECKSTYLE:OFF
static final String modifiersToString(int modifiers) {
    StringBuilder accessBuilder = new StringBuilder();

    if ((modifiers & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC) {
        accessBuilder.append("public ");
    }
    if ((modifiers & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED) {
        accessBuilder.append("protected ");
    }
    if ((modifiers & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) {
        accessBuilder.append("private ");
    }
    if ((modifiers & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT) {
        accessBuilder.append("abstract ");
    }
    if ((modifiers & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC) {
        accessBuilder.append("static ");
    }
    if ((modifiers & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL) {
        accessBuilder.append("final ");
    }
    if ((modifiers & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED) {
        accessBuilder.append("synchronized ");
    }
    if ((modifiers & Opcodes.ACC_NATIVE) == Opcodes.ACC_NATIVE) {
        accessBuilder.append("native ");
    }
    if ((modifiers & Opcodes.ACC_TRANSIENT) == Opcodes.ACC_TRANSIENT) {
        accessBuilder.append("transient ");
    }
    if ((modifiers & Opcodes.ACC_VOLATILE) == Opcodes.ACC_VOLATILE) {
        accessBuilder.append("volatile ");
    }
    if ((modifiers & Opcodes.ACC_STRICT) == Opcodes.ACC_STRICT) {
        accessBuilder.append("strictfp ");
    }

    return accessBuilder.toString().trim();
}

From source file:org.springframework.migrationanalyzer.contributions.bytecode.AsmUtilsTests.java

License:Apache License

@Test
public void synchronizedAccess() {
    assertEquals("synchronized", AsmUtils.modifiersToString(Opcodes.ACC_SYNCHRONIZED));
}

From source file:org.springframework.migrationanalyzer.contributions.bytecode.AsmUtilsTests.java

License:Apache License

@Test
public void methodAccessOrdering() {
    assertEquals("public protected private abstract static final synchronized native strictfp",
            AsmUtils.modifiersToString(Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE
                    | Opcodes.ACC_ABSTRACT | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_SYNCHRONIZED
                    | Opcodes.ACC_NATIVE | Opcodes.ACC_STRICT));
}

From source file:org.springsource.loaded.test.infra.ClassPrinter.java

License:Apache License

private String toAccessForClass(int flags) {
    StringBuilder sb = new StringBuilder();
    if ((flags & Opcodes.ACC_PUBLIC) != 0) {
        sb.append("public ");
    }/*from  w  w  w  .ja  v a2 s .co  m*/
    if ((flags & Opcodes.ACC_PRIVATE) != 0) {
        sb.append("private ");
    }
    if ((flags & Opcodes.ACC_PROTECTED) != 0) {
        sb.append("protected ");
    }
    if ((flags & Opcodes.ACC_STATIC) != 0) {
        sb.append("static ");
    }
    if ((flags & Opcodes.ACC_FINAL) != 0) {
        sb.append("final ");
    }
    if ((flags & Opcodes.ACC_SYNCHRONIZED) != 0) {
        sb.append("synchronized ");
    }
    if ((flags & Opcodes.ACC_BRIDGE) != 0) {
        sb.append("bridge ");
    }
    if ((flags & Opcodes.ACC_VARARGS) != 0) {
        sb.append("varargs ");
    }
    if ((flags & Opcodes.ACC_NATIVE) != 0) {
        sb.append("native ");
    }
    if ((flags & Opcodes.ACC_ABSTRACT) != 0) {
        sb.append("abstract ");
    }
    if ((flags & Opcodes.ACC_SYNTHETIC) != 0) {
        sb.append("synthetic ");
    }
    if ((flags & Opcodes.ACC_DEPRECATED) != 0) {
        sb.append("deprecated ");
    }
    if ((flags & Opcodes.ACC_INTERFACE) != 0) {
        sb.append("interface ");
    }
    return sb.toString().trim();
}

From source file:org.teavm.parsing.Parser.java

License:Apache License

public static void parseModifiers(int access, ElementHolder member) {
    if ((access & Opcodes.ACC_PRIVATE) != 0) {
        member.setLevel(AccessLevel.PRIVATE);
    } else if ((access & Opcodes.ACC_PROTECTED) != 0) {
        member.setLevel(AccessLevel.PROTECTED);
    } else if ((access & Opcodes.ACC_PUBLIC) != 0) {
        member.setLevel(AccessLevel.PUBLIC);
    }/*from  w ww  .  j  a va2s.  c om*/

    if ((access & Opcodes.ACC_ABSTRACT) != 0) {
        member.getModifiers().add(ElementModifier.ABSTRACT);
    }
    if ((access & Opcodes.ACC_ANNOTATION) != 0) {
        member.getModifiers().add(ElementModifier.ANNOTATION);
    }
    if ((access & Opcodes.ACC_BRIDGE) != 0) {
        member.getModifiers().add(ElementModifier.BRIDGE);
    }
    if ((access & Opcodes.ACC_DEPRECATED) != 0) {
        member.getModifiers().add(ElementModifier.DEPRECATED);
    }
    if ((access & Opcodes.ACC_ENUM) != 0) {
        member.getModifiers().add(ElementModifier.ENUM);
    }
    if ((access & Opcodes.ACC_FINAL) != 0) {
        member.getModifiers().add(ElementModifier.FINAL);
    }
    if ((access & Opcodes.ACC_INTERFACE) != 0) {
        member.getModifiers().add(ElementModifier.INTERFACE);
    }
    if ((access & Opcodes.ACC_NATIVE) != 0) {
        member.getModifiers().add(ElementModifier.NATIVE);
    }
    if ((access & Opcodes.ACC_STATIC) != 0) {
        member.getModifiers().add(ElementModifier.STATIC);
    }
    if ((access & Opcodes.ACC_STRICT) != 0) {
        member.getModifiers().add(ElementModifier.STRICT);
    }
    if ((access & Opcodes.ACC_SUPER) != 0) {
        member.getModifiers().add(ElementModifier.SUPER);
    }
    if ((access & Opcodes.ACC_SYNCHRONIZED) != 0) {
        member.getModifiers().add(ElementModifier.SYNCHRONIZED);
    }
    if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
        member.getModifiers().add(ElementModifier.SYNTHETIC);
    }
    if ((access & Opcodes.ACC_TRANSIENT) != 0) {
        member.getModifiers().add(ElementModifier.TRANSIENT);
    }
    if ((access & Opcodes.ACC_VARARGS) != 0) {
        member.getModifiers().add(ElementModifier.VARARGS);
    }
    if ((access & Opcodes.ACC_VOLATILE) != 0) {
        member.getModifiers().add(ElementModifier.VOLATILE);
    }
}

From source file:pt.minha.kernel.instrument.SyncToMonitorClassVisitor.java

License:Open Source License

public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (trans.isSynchronized()) {
        if (name.equals("<clinit>")) {
            hasClinit = true;//  w w  w .j a  v  a 2 s.  c om
            return new ClinitVisitor(super.visitMethod(access, name, desc, signature, exceptions));
        }

        if ((access & Opcodes.ACC_SYNCHRONIZED) != 0) {
            makeStub(access & ~Opcodes.ACC_SYNCHRONIZED, name, desc, signature, exceptions);

            return super.visitMethod(
                    (access & ~(Opcodes.ACC_SYNCHRONIZED | Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)
                            | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PRIVATE),
                    "_" + name, desc, signature, exceptions);
        }
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}