javassist.bytecode.AccessFlag Java Examples

The following examples show how to use javassist.bytecode.AccessFlag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavasisstUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenTableOfInstructions_whenAddNewInstruction_thenShouldConstructProperSequence() throws NotFoundException, BadBytecode, CannotCompileException, IllegalAccessException, InstantiationException {
    // given
    ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.ThreeDimensionalPoint").getClassFile();

    // when
    FieldInfo f = new FieldInfo(cf.getConstPool(), "id", "I");
    f.setAccessFlags(AccessFlag.PUBLIC);
    cf.addField(f);

    ClassPool classPool = ClassPool.getDefault();
    Field[] fields = classPool.makeClass(cf).toClass().getFields();
    List<String> fieldsList = Stream.of(fields).map(Field::getName).collect(Collectors.toList());
    assertTrue(fieldsList.contains("id"));

}
 
Example #2
Source File: AsmInsertImpl.java    From Robust with Apache License 2.0 6 votes vote down vote up
@Override
protected void insertCode(List<CtClass> box, File jarFile) throws IOException, CannotCompileException {
    ZipOutputStream outStream = new JarOutputStream(new FileOutputStream(jarFile));
    //get every class in the box ,ready to insert code
    for (CtClass ctClass : box) {
        //change modifier to public ,so all the class in the apk will be public ,you will be able to access it in the patch
        ctClass.setModifiers(AccessFlag.setPublic(ctClass.getModifiers()));
        if (isNeedInsertClass(ctClass.getName()) && !(ctClass.isInterface() || ctClass.getDeclaredMethods().length < 1)) {
            //only insert code into specific classes
            zipFile(transformCode(ctClass.toBytecode(), ctClass.getName().replaceAll("\\.", "/")), outStream, ctClass.getName().replaceAll("\\.", "/") + ".class");
        } else {
            zipFile(ctClass.toBytecode(), outStream, ctClass.getName().replaceAll("\\.", "/") + ".class");

        }
        ctClass.defrost();
    }
    outStream.close();
}
 
Example #3
Source File: JavasisstUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenJavasisstAPI_whenConstructClass_thenGenerateAClassFile() throws CannotCompileException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    // given
    String classNameWithPackage = "com.baeldung.JavassistGeneratedClass";
    ClassFile cf = new ClassFile(false, classNameWithPackage, null);
    cf.setInterfaces(new String[] { "java.lang.Cloneable" });

    FieldInfo f = new FieldInfo(cf.getConstPool(), "id", "I");
    f.setAccessFlags(AccessFlag.PUBLIC);
    cf.addField(f);

    // when
    String className = "JavassistGeneratedClass.class";
    cf.write(new DataOutputStream(new FileOutputStream(className)));

    // then
    ClassPool classPool = ClassPool.getDefault();
    Field[] fields = classPool.makeClass(cf).toClass().getFields();
    assertEquals(fields[0].getName(), "id");

    String classContent = new String(Files.readAllBytes(Paths.get(className)));
    assertTrue(classContent.contains("java/lang/Cloneable"));
}
 
Example #4
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int getModifiers() {
    //this code reimplements CtClassType.getModifiers() to circumvent a bug
    int acc = this.cf.getAccessFlags();
    acc = clear(acc, SUPER);
    int inner = this.cf.getInnerAccessFlags();
    if (inner != -1) {
        if ((inner & STATIC) != 0) {
            acc |= STATIC;
        }
        if (AccessFlag.isPublic(inner)) {
            //seems that public nested classes already have the PUBLIC modifier set
            //but we are paranoid and we set it again
            acc = setPublic(acc);
        } else if (AccessFlag.isProtected(inner)) {
            acc = setProtected(acc);
        } else if (AccessFlag.isPrivate(inner)) {
            acc = setPrivate(acc);
        } else { //package visibility
            acc = setPackage(acc); //clear the PUBLIC modifier in case it is set
        }
    }
    return AccessFlag.toModifier(acc);
}
 
Example #5
Source File: BulkAccessorFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Declares a constructor that takes no parameter.
 *
 * @param classfile The class descriptor
 *
 * @throws CannotCompileException Indicates trouble with the underlying Javassist calls
 */
private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
	final ConstPool constPool = classfile.getConstPool();
	final String constructorSignature = "()V";
	final MethodInfo constructorMethodInfo = new MethodInfo( constPool, MethodInfo.nameInit, constructorSignature );

	final Bytecode code = new Bytecode( constPool, 0, 1 );
	// aload_0
	code.addAload( 0 );
	// invokespecial
	code.addInvokespecial( BulkAccessor.class.getName(), MethodInfo.nameInit, constructorSignature );
	// return
	code.addOpcode( Opcode.RETURN );

	constructorMethodInfo.setCodeAttribute( code.toCodeAttribute() );
	constructorMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
	classfile.addMethod( constructorMethodInfo );
}
 
Example #6
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 6 votes vote down vote up
private ArrayList<Signature> getDeclaredFields(boolean areStatic) {
    if ((areStatic ? this.fieldsStatic : this.fieldsObject) == null) {
        final ArrayList<Signature> fields = new ArrayList<Signature>();
        final List<FieldInfo> fieldsJA = this.cf.getFields();
        for (FieldInfo fld : fieldsJA) {
            if (Modifier.isStatic(AccessFlag.toModifier(fld.getAccessFlags())) == areStatic) {
                final Signature sig = new Signature(getClassName(), fld.getDescriptor(), fld.getName());
                fields.add(sig);
            }
        }
        if (areStatic) {
            this.fieldsStatic = fields;
        } else {
            this.fieldsObject = fields;
        }
    }
    return (areStatic ? this.fieldsStatic : this.fieldsObject);
}
 
Example #7
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean hasOneSignaturePolymorphicMethodDeclaration(String methodName) {
    //cannot be signature polymorphic if it is not in JAVA_METHODHANDLE
    if (!JAVA_METHODHANDLE.equals(getClassName())) {
        return false;
    }
    
    //the method declaration must be unique
    final MethodInfo uniqueMethod = findUniqueMethodDeclarationWithName(methodName);
    if (uniqueMethod == null) {
        return false;
    }
    
    //cannot be signature polymorphic if it has wrong descriptor
    if (!SIGNATURE_POLYMORPHIC_DESCRIPTOR.equals(uniqueMethod.getDescriptor())) {
        return false;
    }
    
    //cannot be signature polymorphic if it not native or if it is not varargs
    if (!Modifier.isNative(AccessFlag.toModifier(uniqueMethod.getAccessFlags())) || (AccessFlag.toModifier(uniqueMethod.getAccessFlags()) & Modifier.VARARGS) == 0) {
        return false;
    }

    //all checks passed
    return true;
}
 
Example #8
Source File: JavassistAdapter.java    From panda with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isPublic(@Nullable Object o) {
    if (o == null) {
        return false;
    }

    if (o instanceof ClassFile) {
        return AccessFlag.isPublic(((ClassFile) o).getAccessFlags());
    }

    if (o instanceof MethodInfo) {
        return AccessFlag.isPublic(((MethodInfo) o).getAccessFlags());
    }

    if (o instanceof FieldInfo) {
        return AccessFlag.isPublic(((FieldInfo) o).getAccessFlags());
    }

    if (o instanceof Class) {
        return Modifier.isPublic(((Class) o).getModifiers());
    }

    return false;
}
 
Example #9
Source File: BulkAccessorFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Declares a constructor that takes no parameter.
 *
 * @param classfile
 * @throws CannotCompileException
 */
private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
	ConstPool cp = classfile.getConstPool();
	String cons_desc = "()V";
	MethodInfo mi = new MethodInfo( cp, MethodInfo.nameInit, cons_desc );

	Bytecode code = new Bytecode( cp, 0, 1 );
	// aload_0
	code.addAload( 0 );
	// invokespecial
	code.addInvokespecial( BulkAccessor.class.getName(), MethodInfo.nameInit, cons_desc );
	// return
	code.addOpcode( Opcode.RETURN );

	mi.setCodeAttribute( code.toCodeAttribute() );
	mi.setAccessFlags( AccessFlag.PUBLIC );
	classfile.addMethod( mi );
}
 
Example #10
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void addReadWriteMethods(ClassFile classfile)
		throws CannotCompileException {
	List fields = classfile.getFields();
	for (Iterator field_iter = fields.iterator(); field_iter.hasNext();) {
		FieldInfo finfo = (FieldInfo) field_iter.next();
		if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0
		    && (!finfo.getName().equals(HANDLER_FIELD_NAME))) {
			// case of non-static field
			if (filter.handleRead(finfo.getDescriptor(), finfo
					.getName())) {
				addReadMethod(classfile, finfo);
				readableFields.put(finfo.getName(), finfo
						.getDescriptor());
			}
			if (filter.handleWrite(finfo.getDescriptor(), finfo
					.getName())) {
				addWriteMethod(classfile, finfo);
				writableFields.put(finfo.getName(), finfo
						.getDescriptor());
			}
		}
	}
}
 
Example #11
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void addSetFieldHandlerMethod(ClassFile classfile)
		throws CannotCompileException {
	ConstPool cp = classfile.getConstPool();
	int this_class_index = cp.getThisClassInfo();
	MethodInfo minfo = new MethodInfo(cp, SETFIELDHANDLER_METHOD_NAME,
	                                  SETFIELDHANDLER_METHOD_DESCRIPTOR);
	/* local variables | this | callback | */
	Bytecode code = new Bytecode(cp, 3, 3);
	// aload_0 // load this
	code.addAload(0);
	// aload_1 // load callback
	code.addAload(1);
	// putfield // put field "$JAVASSIST_CALLBACK" defined already
	code.addOpcode(Opcode.PUTFIELD);
	int field_index = cp.addFieldrefInfo(this_class_index,
	                                     HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR);
	code.addIndex(field_index);
	// return
	code.addOpcode(Opcode.RETURN);
	minfo.setCodeAttribute(code.toCodeAttribute());
	minfo.setAccessFlags(AccessFlag.PUBLIC);
	classfile.addMethod(minfo);
}
 
Example #12
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void addGetFieldHandlerMethod(ClassFile classfile)
		throws CannotCompileException {
	ConstPool cp = classfile.getConstPool();
	int this_class_index = cp.getThisClassInfo();
	MethodInfo minfo = new MethodInfo(cp, GETFIELDHANDLER_METHOD_NAME,
	                                  GETFIELDHANDLER_METHOD_DESCRIPTOR);
	/* local variable | this | */
	Bytecode code = new Bytecode(cp, 2, 1);
	// aload_0 // load this
	code.addAload(0);
	// getfield // get field "$JAVASSIST_CALLBACK" defined already
	code.addOpcode(Opcode.GETFIELD);
	int field_index = cp.addFieldrefInfo(this_class_index,
	                                     HANDLER_FIELD_NAME, HANDLER_FIELD_DESCRIPTOR);
	code.addIndex(field_index);
	// areturn // return the value of the field
	code.addOpcode(Opcode.ARETURN);
	minfo.setCodeAttribute(code.toCodeAttribute());
	minfo.setAccessFlags(AccessFlag.PUBLIC);
	classfile.addMethod(minfo);
}
 
Example #13
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMethodAbstract(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return Modifier.isAbstract(AccessFlag.toModifier(m.getAccessFlags()));
}
 
Example #14
Source File: ClassPathScanner.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void scan(final Object cls) {
  final ClassFile classFile = (ClassFile)cls;
  String className = classFile.getName();
  String superclass = classFile.getSuperclass();
  boolean isAbstract = (classFile.getAccessFlags() & (AccessFlag.INTERFACE | AccessFlag.ABSTRACT)) != 0;
  ChildClassDescriptor scannedClass = new ChildClassDescriptor(className, isAbstract);
  if (!superclass.equals(Object.class.getName())) {
    children.put(superclass, scannedClass);
  }
  for (String anInterface : classFile.getInterfaces()) {
    children.put(anInterface, scannedClass);
  }
}
 
Example #15
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isFieldProtected(Signature fieldSignature) throws FieldNotFoundException {
    final FieldInfo fld = findField(fieldSignature);
    if (fld == null) {
        throw new FieldNotFoundException(fieldSignature.toString());
    }
    return Modifier.isProtected(AccessFlag.toModifier(fld.getAccessFlags()));
}
 
Example #16
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isFieldPackage(Signature fieldSignature) throws FieldNotFoundException {
    final FieldInfo fld = findField(fieldSignature);
    if (fld == null) {
        throw new FieldNotFoundException(fieldSignature.toString());
    }
    return Modifier.isPackage(AccessFlag.toModifier(fld.getAccessFlags()));
}
 
Example #17
Source File: ClassPathScanner.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public void scan(final Object cls) {
  final ClassFile classFile = (ClassFile)cls;
  String className = classFile.getName();
  String superclass = classFile.getSuperclass();
  boolean isAbstract = (classFile.getAccessFlags() & (AccessFlag.INTERFACE | AccessFlag.ABSTRACT)) != 0;
  ChildClassDescriptor scannedClass = new ChildClassDescriptor(className, isAbstract);
  if (!superclass.equals(Object.class.getName())) {
    children.put(superclass, scannedClass);
  }
  for (String anInterface : classFile.getInterfaces()) {
    children.put(anInterface, scannedClass);
  }
}
 
Example #18
Source File: BulkAccessorFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private ClassFile make(Method[] getters, Method[] setters) throws CannotCompileException {
	String className = targetBean.getName();
	// set the name of bulk accessor.
	className = className + "_$$_bulkaccess_" + counter++;
	if ( className.startsWith( "java." ) ) {
		className = "org.javassist.tmp." + className;
	}

	ClassFile classfile = new ClassFile( false, className, BULKACESSOR_CLASS_NAME );
	classfile.setAccessFlags( AccessFlag.PUBLIC );
	addDefaultConstructor( classfile );
	addGetter( classfile, getters );
	addSetter( classfile, setters );
	return classfile;
}
 
Example #19
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getFieldModifiers(Signature fieldSignature) 
throws FieldNotFoundException {
    final FieldInfo fld = findField(fieldSignature);
    if (fld == null) {
        throw new FieldNotFoundException(fieldSignature.toString());
    }
    return AccessFlag.toModifier(fld.getAccessFlags());
}
 
Example #20
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMethodNative(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return Modifier.isNative(AccessFlag.toModifier(m.getAccessFlags()));
}
 
Example #21
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMethodVarargs(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return (AccessFlag.toModifier(m.getAccessFlags()) & Modifier.VARARGS) != 0;
}
 
Example #22
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMethodFinal(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return Modifier.isFinal(AccessFlag.toModifier(m.getAccessFlags()));
}
 
Example #23
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getMethodModifiers(Signature methodSignature) 
throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return AccessFlag.toModifier(m.getAccessFlags());
}
 
Example #24
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMethodStatic(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return Modifier.isStatic(AccessFlag.toModifier(m.getAccessFlags()));
}
 
Example #25
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMethodPublic(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return Modifier.isPublic(AccessFlag.toModifier(m.getAccessFlags()));
}
 
Example #26
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMethodProtected(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return Modifier.isProtected(AccessFlag.toModifier(m.getAccessFlags()));
}
 
Example #27
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMethodPackage(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return Modifier.isPackage(AccessFlag.toModifier(m.getAccessFlags()));
}
 
Example #28
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMethodPrivate(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    return Modifier.isPrivate(AccessFlag.toModifier(m.getAccessFlags()));
}
 
Example #29
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isFieldFinal(Signature fieldSignature) throws FieldNotFoundException {
    final FieldInfo fld = findField(fieldSignature);
    if (fld == null) {
        throw new FieldNotFoundException(fieldSignature.toString());
    }
    return Modifier.isFinal(AccessFlag.toModifier(fld.getAccessFlags()));
}
 
Example #30
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isFieldPublic(Signature fieldSignature) throws FieldNotFoundException {
    final FieldInfo fld = findField(fieldSignature);
    if (fld == null) {
        throw new FieldNotFoundException(fieldSignature.toString());
    }
    return Modifier.isPublic(AccessFlag.toModifier(fld.getAccessFlags()));
}