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.elasticsearch.painless.node.SFunction.java

License:Apache License

/** Writes the function to given ClassVisitor. */
void write(ClassVisitor writer, CompilerSettings settings, Globals globals) {
    int access = Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC;
    if (synthetic) {
        access |= Opcodes.ACC_SYNTHETIC;
    }/*  ww w  . ja v  a  2 s  .  c  om*/
    final MethodWriter function = new MethodWriter(access, method.method, writer, globals.getStatements(),
            settings);
    function.visitCode();
    write(function, globals);
    function.endMethod();
}

From source file:org.elasticsearch.painless.node.SSource.java

License:Apache License

public void write() {
    // Create the ClassWriter.

    int classFrames = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS;
    int classAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL;
    String classBase = BASE_CLASS_TYPE.getInternalName();
    String className = CLASS_TYPE.getInternalName();
    String classInterfaces[] = reserved.usesScore()
            ? new String[] { WriterConstants.NEEDS_SCORE_TYPE.getInternalName() }
            : null;/*from w ww .j a  v a2s . co m*/

    ClassWriter writer = new ClassWriter(classFrames);
    ClassVisitor visitor = writer;

    // if picky is enabled, turn on some checks. instead of VerifyError at the end, you get a helpful stacktrace.
    if (settings.isPicky()) {
        visitor = new SimpleChecksAdapter(visitor);
    }

    if (debugStream != null) {
        visitor = new TraceClassVisitor(visitor, debugStream, null);
    }
    visitor.visit(WriterConstants.CLASS_VERSION, classAccess, className, null, classBase, classInterfaces);
    visitor.visitSource(Location.computeSourceName(name, source), null);

    // Write the constructor:
    MethodWriter constructor = new MethodWriter(Opcodes.ACC_PUBLIC, CONSTRUCTOR, visitor,
            globals.getStatements(), settings);
    constructor.visitCode();
    constructor.loadThis();
    constructor.loadArgs();
    constructor.invokeConstructor(org.objectweb.asm.Type.getType(Executable.class), CONSTRUCTOR);
    constructor.returnValue();
    constructor.endMethod();

    // Write the execute method:
    MethodWriter execute = new MethodWriter(Opcodes.ACC_PUBLIC, EXECUTE, visitor, globals.getStatements(),
            settings);
    execute.visitCode();
    write(execute, globals);
    execute.endMethod();

    // Write all functions:
    for (SFunction function : functions) {
        function.write(visitor, settings, globals);
    }

    // Write all synthetic functions. Note that this process may add more :)
    while (!globals.getSyntheticMethods().isEmpty()) {
        List<SFunction> current = new ArrayList<>(globals.getSyntheticMethods().values());
        globals.getSyntheticMethods().clear();
        for (SFunction function : current) {
            function.write(visitor, settings, globals);
        }
    }

    // Write the constants
    if (false == globals.getConstantInitializers().isEmpty()) {
        Collection<Constant> inits = globals.getConstantInitializers().values();

        // Fields
        for (Constant constant : inits) {
            visitor.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, constant.name,
                    constant.type.getDescriptor(), null, null).visitEnd();
        }

        // Initialize the constants in a static initializer
        final MethodWriter clinit = new MethodWriter(Opcodes.ACC_STATIC, WriterConstants.CLINIT, visitor,
                globals.getStatements(), settings);
        clinit.visitCode();
        for (Constant constant : inits) {
            constant.initializer.accept(clinit);
            clinit.putStatic(CLASS_TYPE, constant.name, constant.type);
        }
        clinit.returnValue();
        clinit.endMethod();
    }

    // End writing the class and store the generated bytes.

    visitor.visitEnd();
    bytes = writer.toByteArray();
}

From source file:org.elasticsearch.painless.PainlessScriptEngine.java

License:Apache License

/**
 * Generates a stateful factory class that will return script instances.  Acts as a middle man between
 * the {@link ScriptContext#factoryClazz} and the {@link ScriptContext#instanceClazz} when used so that
 * the stateless factory can be used for caching and the stateful factory can act as a cache for new
 * script instances.  Uses the newInstance method from a {@link ScriptContext#statefulFactoryClazz} to
 * define the factory method to create new instances of the {@link ScriptContext#instanceClazz}.
 * @param loader The {@link ClassLoader} that is used to define the factory class and script class.
 * @param context The {@link ScriptContext}'s semantics are used to define the factory class.
 * @param <T> The factory class./*from   w w w  .j a  v  a  2  s .  c o m*/
 * @return A factory class that will return script instances.
 */
private <T> Type generateStatefulFactory(Loader loader, ScriptContext<T> context, MainMethodReserved reserved) {
    int classFrames = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS;
    int classAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL;
    String interfaceBase = Type.getType(context.statefulFactoryClazz).getInternalName();
    String className = interfaceBase + "$StatefulFactory";
    String classInterfaces[] = new String[] { interfaceBase };

    ClassWriter writer = new ClassWriter(classFrames);
    writer.visit(WriterConstants.CLASS_VERSION, classAccess, className, null, OBJECT_TYPE.getInternalName(),
            classInterfaces);

    Method newFactory = null;

    for (Method method : context.factoryClazz.getMethods()) {
        if ("newFactory".equals(method.getName())) {
            newFactory = method;

            break;
        }
    }

    for (int count = 0; count < newFactory.getParameterTypes().length; ++count) {
        writer.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "$arg" + count,
                Type.getType(newFactory.getParameterTypes()[count]).getDescriptor(), null, null).visitEnd();
    }

    org.objectweb.asm.commons.Method base = new org.objectweb.asm.commons.Method("<init>",
            MethodType.methodType(void.class).toMethodDescriptorString());
    org.objectweb.asm.commons.Method init = new org.objectweb.asm.commons.Method("<init>",
            MethodType.methodType(void.class, newFactory.getParameterTypes()).toMethodDescriptorString());

    GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ASM5, init,
            writer.visitMethod(Opcodes.ACC_PUBLIC, init.getName(), init.getDescriptor(), null, null));
    constructor.visitCode();
    constructor.loadThis();
    constructor.invokeConstructor(OBJECT_TYPE, base);

    for (int count = 0; count < newFactory.getParameterTypes().length; ++count) {
        constructor.loadThis();
        constructor.loadArg(count);
        constructor.putField(Type.getType(className), "$arg" + count,
                Type.getType(newFactory.getParameterTypes()[count]));
    }

    constructor.returnValue();
    constructor.endMethod();

    Method newInstance = null;

    for (Method method : context.statefulFactoryClazz.getMethods()) {
        if ("newInstance".equals(method.getName())) {
            newInstance = method;

            break;
        }
    }

    org.objectweb.asm.commons.Method instance = new org.objectweb.asm.commons.Method(newInstance.getName(),
            MethodType.methodType(newInstance.getReturnType(), newInstance.getParameterTypes())
                    .toMethodDescriptorString());

    List<Class<?>> parameters = new ArrayList<>(Arrays.asList(newFactory.getParameterTypes()));
    parameters.addAll(Arrays.asList(newInstance.getParameterTypes()));

    org.objectweb.asm.commons.Method constru = new org.objectweb.asm.commons.Method("<init>", MethodType
            .methodType(void.class, parameters.toArray(new Class<?>[] {})).toMethodDescriptorString());

    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ASM5, instance, writer.visitMethod(
            Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, instance.getName(), instance.getDescriptor(), null, null));
    adapter.visitCode();
    adapter.newInstance(WriterConstants.CLASS_TYPE);
    adapter.dup();

    for (int count = 0; count < newFactory.getParameterTypes().length; ++count) {
        adapter.loadThis();
        adapter.getField(Type.getType(className), "$arg" + count,
                Type.getType(newFactory.getParameterTypes()[count]));
    }

    adapter.loadArgs();
    adapter.invokeConstructor(WriterConstants.CLASS_TYPE, constru);
    adapter.returnValue();
    adapter.endMethod();

    writeNeedsMethods(context.statefulFactoryClazz, writer, reserved);
    writer.visitEnd();

    loader.defineFactory(className.replace('/', '.'), writer.toByteArray());

    return Type.getType(className);
}

From source file:org.evosuite.classpath.ResourceList.java

License:Open Source License

/**
 * Returns {@code true} if there is at least one public method in the class; returns {@code false} otherwise.
 * //from  w w  w. j a  va2  s.  c  o m
 * @param input the input stream
 * @return {@code true} if there is at least one public method in the class, {@code false} otherwise
 * @throws IOException if an error occurs while reading the input stream
 */
private static boolean isClassTestable(InputStream input) throws IOException {
    try {
        ClassReader reader = new ClassReader(input);
        ClassNode cn = new ClassNode();
        reader.accept(cn, ClassReader.SKIP_FRAMES);
        @SuppressWarnings("unchecked")
        List<MethodNode> l = cn.methods;
        for (MethodNode m : l) {
            if ((m.access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC
                    || (m.access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED
                    || (m.access & Opcodes.ACC_PRIVATE) == 0 /* default */ ) {
                return true;
            }
        }
        return false;
    } finally {
        input.close(); //VERY IMPORTANT, as ASM does not close the stream
    }
}

From source file:org.evosuite.graphs.cfg.CFGMethodAdapter.java

License:Open Source License

/**
 * See description of CFGMethodAdapter.EXCLUDE
 * /*from  w  w w.jav a  2s. c om*/
 * @return
 */
private boolean isUsable() {
    if ((this.access & Opcodes.ACC_SYNTHETIC) != 0)
        return false;

    if ((this.access & Opcodes.ACC_BRIDGE) != 0)
        return false;

    if ((this.access & Opcodes.ACC_NATIVE) != 0)
        return false;

    if (methodName.contains("<clinit>"))
        return false;

    // If we are not using reflection, covering private constructors is difficult?
    if (Properties.P_REFLECTION_ON_PRIVATE <= 0.0) {
        if (methodName.contains("<init>") && (access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE)
            return false;
    }

    return true;
}

From source file:org.evosuite.instrumentation.AccessibleClassAdapter.java

License:Open Source License

/**
 * {@inheritDoc}/*from  ww  w  .j  a  va  2s.  c  om*/
 *
 * Change subclasses to public
 */
@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {
    if (!exclude && (access & Opcodes.ACC_PRIVATE) != Opcodes.ACC_PRIVATE
            && (access & Opcodes.ACC_PROTECTED) != Opcodes.ACC_PROTECTED) {
        access = access | Opcodes.ACC_PUBLIC;
        // access = access & ~Opcodes.ACC_PROTECTED;
    }
    super.visit(version, access, name, signature, superName, interfaces);
}

From source file:org.evosuite.instrumentation.AccessibleClassAdapter.java

License:Open Source License

/**
 * {@inheritDoc}/*  w  w  w  .  j  a v  a2 s.co m*/
 *
 * Change fields to public
 */
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    if (!exclude && (access & Opcodes.ACC_PRIVATE) != Opcodes.ACC_PRIVATE
            && (access & Opcodes.ACC_PROTECTED) != Opcodes.ACC_PROTECTED) {
        access = access | Opcodes.ACC_PUBLIC;
        // access = access & ~Opcodes.ACC_PROTECTED;
        //System.out.println("Setting field to public: "+name);
    }

    return super.visitField(access, name, desc, signature, value);
}

From source file:org.evosuite.instrumentation.AccessibleClassAdapter.java

License:Open Source License

/**
 * {@inheritDoc}/* w  w  w.  j  ava  2 s.  co m*/
 *
 * Change methods to public
 */
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature,
        final String[] exceptions) {
    int skipMask = Opcodes.ACC_NATIVE | Opcodes.ACC_BRIDGE;

    if (!exclude && ((access & Opcodes.ACC_PRIVATE) != Opcodes.ACC_PRIVATE) && ((access & skipMask) == 0)) {
        access = access | Opcodes.ACC_PUBLIC;
        access = access & ~Opcodes.ACC_PROTECTED;
    }

    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    return mv;
}

From source file:org.evosuite.instrumentation.AccessibleClassAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from www.j a  v a  2  s  .c o  m*/
public void visitInnerClass(String name, String outerName, String innerName, int access) {
    if (!exclude && (access & Opcodes.ACC_PRIVATE) != Opcodes.ACC_PRIVATE) {
        access = access | Opcodes.ACC_PUBLIC;
        access = access & ~Opcodes.ACC_PROTECTED;
    }

    super.visitInnerClass(name, outerName, innerName, access);
}

From source file:org.evosuite.instrumentation.CreateClassResetClassAdapter.java

License:Open Source License

private void createSerialisableUID() {
    // Only add this for serialisable classes
    if (serialUID < 0)
        return;//  w w w  .  java 2s.  c o  m
    /*
     * If the class is serializable, then adding a hashCode will change the serialVersionUID
     * if it is not defined in the class. Hence, if it is not defined, we have to define it to
     * avoid problems in serialising the class.
     */
    logger.info("Adding serialId to class {}", className);
    visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "serialVersionUID", "J", null,
            serialUID);
}