• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Java ClassFile类的典型用法和代码示例

java 7次浏览

本文整理汇总了Java中javassist.bytecode.ClassFile的典型用法代码示例。如果您正苦于以下问题:Java ClassFile类的具体用法?Java ClassFile怎么用?Java ClassFile使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。

ClassFile类属于javassist.bytecode包,在下文中一共展示了ClassFile类的34个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: create

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
BulkAccessor create() {
	final Method[] getters = new Method[getterNames.length];
	final Method[] setters = new Method[setterNames.length];
	findAccessors( targetBean, getterNames, setterNames, types, getters, setters );

	final Class beanClass;
	try {
		final ClassFile classfile = make( getters, setters );
		final ClassLoader loader = this.getClassLoader();
		if ( writeDirectory != null ) {
			FactoryHelper.writeFile( classfile, writeDirectory );
		}

		beanClass = FactoryHelper.toClass( classfile, loader, getDomain() );
		return (BulkAccessor) this.newInstance( beanClass );
	}
	catch ( Exception e ) {
		throw new BulkAccessorException( e.getMessage(), e );
	}
}
 

开发者ID:lamsfoundation,
项目名称:lams,
代码行数:21,
代码来源:BulkAccessorFactory.java

示例2: addDefaultConstructor

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
/**
 * 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 );
}
 

开发者ID:lamsfoundation,
项目名称:lams,
代码行数:25,
代码来源:BulkAccessorFactory.java

示例3: transform

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
/**
 * Transform the class defined by the given ClassFile descriptor.  The ClassFile descriptor itself is mutated
 *
 * @param classFile The class file descriptor
 *
 * @throws Exception Indicates a problem performing the transformation
 */
public void transform(ClassFile classFile) throws Exception {
	if ( classFile.isInterface() ) {
		return;
	}
	try {
		addFieldHandlerField( classFile );
		addGetFieldHandlerMethod( classFile );
		addSetFieldHandlerMethod( classFile );
		addFieldHandledInterface( classFile );
		addReadWriteMethods( classFile );
		transformInvokevirtualsIntoPutAndGetfields( classFile );
	}
	catch (CannotCompileException e) {
		throw new RuntimeException( e.getMessage(), e );
	}
}
 

开发者ID:lamsfoundation,
项目名称:lams,
代码行数:24,
代码来源:FieldTransformer.java

示例4: transformInvokevirtualsIntoPutAndGetfields

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
private void transformInvokevirtualsIntoPutAndGetfields(ClassFile classfile) throws CannotCompileException, BadBytecode {
	for ( Object o : classfile.getMethods() ) {
		final MethodInfo methodInfo = (MethodInfo) o;
		final String methodName = methodInfo.getName();
		if ( methodName.startsWith( EACH_READ_METHOD_PREFIX )
				|| methodName.startsWith( EACH_WRITE_METHOD_PREFIX )
				|| methodName.equals( GETFIELDHANDLER_METHOD_NAME )
				|| methodName.equals( SETFIELDHANDLER_METHOD_NAME ) ) {
			continue;
		}

		final CodeAttribute codeAttr = methodInfo.getCodeAttribute();
		if ( codeAttr == null ) {
			continue;
		}

		final CodeIterator iter = codeAttr.iterator();
		while ( iter.hasNext() ) {
			int pos = iter.next();
			pos = transformInvokevirtualsIntoGetfields( classfile, iter, pos );
			transformInvokevirtualsIntoPutfields( classfile, iter, pos );
		}
		final StackMapTable smt = MapMaker.make( classPool, methodInfo );
		codeAttr.setAttribute( smt );
	}
}
 

开发者ID:lamsfoundation,
项目名称:lams,
代码行数:27,
代码来源:FieldTransformer.java

示例5: transformInvokevirtualsIntoGetfields

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
private int transformInvokevirtualsIntoGetfields(ClassFile classfile, CodeIterator iter, int pos) {
	final ConstPool constPool = classfile.getConstPool();
	final int c = iter.byteAt( pos );
	if ( c != Opcode.GETFIELD ) {
		return pos;
	}

	final int index = iter.u16bitAt( pos + 1 );
	final String fieldName = constPool.getFieldrefName( index );
	final String className = constPool.getFieldrefClassName( index );
	if ( !filter.handleReadAccess( className, fieldName ) ) {
		return pos;
	}

	final String fieldReaderMethodDescriptor = "()" + constPool.getFieldrefType( index );
	final int fieldReaderMethodIndex = constPool.addMethodrefInfo(
			constPool.getThisClassInfo(),
			EACH_READ_METHOD_PREFIX + fieldName,
			fieldReaderMethodDescriptor
	);
	iter.writeByte( Opcode.INVOKEVIRTUAL, pos );
	iter.write16bit( fieldReaderMethodIndex, pos + 1 );
	return pos;
}
 

开发者ID:lamsfoundation,
项目名称:lams,
代码行数:25,
代码来源:FieldTransformer.java

示例6: transformInvokevirtualsIntoPutfields

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
private int transformInvokevirtualsIntoPutfields(ClassFile classfile, CodeIterator iter, int pos) {
	final ConstPool constPool = classfile.getConstPool();
	final int c = iter.byteAt( pos );
	if ( c != Opcode.PUTFIELD ) {
		return pos;
	}

	final int index = iter.u16bitAt( pos + 1 );
	final String fieldName = constPool.getFieldrefName( index );
	final String className = constPool.getFieldrefClassName( index );
	if ( !filter.handleWriteAccess( className, fieldName ) ) {
		return pos;
	}

	final String fieldWriterMethodDescriptor = "(" + constPool.getFieldrefType( index ) + ")V";
	final int fieldWriterMethodIndex = constPool.addMethodrefInfo(
			constPool.getThisClassInfo(),
			EACH_WRITE_METHOD_PREFIX + fieldName,
			fieldWriterMethodDescriptor
	);
	iter.writeByte( Opcode.INVOKEVIRTUAL, pos );
	iter.write16bit( fieldWriterMethodIndex, pos + 1 );
	return pos;
}
 

开发者ID:lamsfoundation,
项目名称:lams,
代码行数:25,
代码来源:FieldTransformer.java

示例7: updateInnerEntry

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
private static void updateInnerEntry(int mod, String name, CtClass clazz, boolean outer) {
    ClassFile cf = clazz.getClassFile2();
    InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(
                                            InnerClassesAttribute.tag);
    if (ica == null)
        return;

    int n = ica.tableLength();
    for (int i = 0; i < n; i++)
        if (name.equals(ica.innerClass(i))) {
            int acc = ica.accessFlags(i) & AccessFlag.STATIC;
            ica.setAccessFlags(i, mod | acc);
            String outName = ica.outerClass(i);
            if (outName != null && outer)
                try {
                    CtClass parent = clazz.getClassPool().get(outName);
                    updateInnerEntry(mod, name, parent, false);
                }
                catch (NotFoundException e) {
                    throw new RuntimeException("cannot find the declaring class: "
                                               + outName);
                }

            break;
        }
}
 

开发者ID:AndreJCL,
项目名称:JCL,
代码行数:27,
代码来源:CtNewNestedClass.java

示例8: getRefClasses

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
/**
  * Returns a collection of the names of all the classes
  * referenced in this class.
  * That collection includes the name of this class.
  *
  * <p>This method may return <code>null</code>.
  *
  * @return a <code>Collection&lt;String&gt;</code> object.
  */
 public synchronized Collection<String> getRefClasses() {
     ClassFile cf = getClassFile2();
     if (cf != null) {
         @SuppressWarnings("serial")
ClassMap cm = new ClassMap() {
             public String put(String oldname, String newname) {
                 return put0(oldname, newname);
             }

             public String get(Object jvmClassName) {
                 String n = toJavaName((String)jvmClassName);
                 put0(n, n);
                 return null;
             }

             public void fix(String name) {}
         };
         cf.getRefClasses(cm);
         return cm.values();
     }
     else
         return null;
 }
 

开发者ID:AndreJCL,
项目名称:JCL,
代码行数:33,
代码来源:CtClass.java

示例9: getClassFile

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
@Override
public ClassFile getClassFile(String classname) throws NotFoundException {
	Class<?> clazz = null;
	try {
		clazz = this.apk.getClass(classname);
	} catch (ClassNotFoundException e) {
		throw new NotFoundException(e.getMessage(), e);
	}
	
	final Class<?> superClass = clazz.getSuperclass();
	final ClassFile cf = new ClassFile(clazz.isInterface(), classname, null == superClass ? null : superClass.getName());
	
	addFields(cf, clazz);
	addConstructors(cf, clazz);
	addMethods(cf, clazz);
	
	return cf;
}
 

开发者ID:AndreJCL,
项目名称:JCL,
代码行数:19,
代码来源:DalvikClassClassPath.java

示例10: CtNewClass

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
CtNewClass(String name, ClassPool cp,
           boolean isInterface, CtClass superclass) {
    super(name, cp);
    wasChanged = true;
    String superName;
    if (isInterface || superclass == null)
        superName = null;
    else
        superName = superclass.getName();

    classfile = new ClassFile(isInterface, name, superName);
    if (isInterface && superclass != null)
        classfile.setInterfaces(new String[] { superclass.getName() });

    setModifiers(Modifier.setPublic(getModifiers()));
    hasConstructor = isInterface;
}
 

开发者ID:AndreJCL,
项目名称:JCL,
代码行数:18,
代码来源:CtNewClass.java

示例11: main

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
/**
 * Main method.
 *
 * @param args           <code>args[0]</code> is the class file name.
 */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Usage: java Dump <class file name>");
        return;
    }

    DataInputStream in = new DataInputStream(
                                     new FileInputStream(args[0]));
    ClassFile w = new ClassFile(in);
    PrintWriter out = new PrintWriter(System.out, true);
    out.println("*** constant pool ***");
    w.getConstPool().print(out);
    out.println();
    out.println("*** members ***");
    ClassFilePrinter.print(w, out);
}
 

开发者ID:AndreJCL,
项目名称:JCL,
代码行数:22,
代码来源:Dump.java

示例12: writeFile0

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
private static void writeFile0(ClassFile cf, String directoryName)
        throws CannotCompileException, IOException {
    String classname = cf.getName();
    String filename = directoryName + File.separatorChar
            + classname.replace('.', File.separatorChar) + ".class";
    int pos = filename.lastIndexOf(File.separatorChar);
    if (pos > 0) {
        String dir = filename.substring(0, pos);
        if (!dir.equals("."))
            new File(dir).mkdirs();
    }

    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(
            new FileOutputStream(filename)));
    try {
        cf.write(out);
    }
    catch (IOException e) {
        throw e;
    }
    finally {
        out.close();
    }
}
 

开发者ID:AndreJCL,
项目名称:JCL,
代码行数:25,
代码来源:FactoryHelper.java

示例13: subtypeOf

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
public boolean subtypeOf(CtClass clazz) throws NotFoundException {
    int i;
    String cname = clazz.getName();
    if (this == clazz || getName().equals(cname))
        return true;

    ClassFile file = getClassFile2();
    String supername = file.getSuperclass();
    if (supername != null && supername.equals(cname))
        return true;

    String[] ifs = file.getInterfaces();
    int num = ifs.length;
    for (i = 0; i < num; ++i)
        if (ifs[i].equals(cname))
            return true;

    if (supername != null && classPool.get(supername).subtypeOf(clazz))
        return true;

    for (i = 0; i < num; ++i)
        if (classPool.get(ifs[i]).subtypeOf(clazz))
            return true;

    return false;
}
 

开发者ID:AndreJCL,
项目名称:JCL,
代码行数:27,
代码来源:CtClassType.java

示例14: replaceClassName

点赞 3

import javassist.bytecode.ClassFile; //导入依赖的package包/类
public void replaceClassName(ClassMap classnames)
    throws RuntimeException
{
    String oldClassName = getName();
    String newClassName
        = (String)classnames.get(Descriptor.toJvmName(oldClassName));
    if (newClassName != null) {
        newClassName = Descriptor.toJavaName(newClassName);
        // check this in advance although classNameChanged() below does.
        classPool.checkNotFrozen(newClassName);
    }

    super.replaceClassName(classnames);
    ClassFile cf = getClassFile2();
    cf.renameClass(classnames);
    nameReplaced();

    if (newClassName != null) {
        super.setName(newClassName);
        classPool.classNameChanged(oldClassName, this);
    }
}
 

开发者ID:AndreJCL,
项目名称:JCL,
代码行数:23,
代码来源:CtClassType.java

示例15: getNestedClasses


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)