本文整理汇总了Java中javassist.bytecode.ClassFile类的典型用法代码示例。如果您正苦于以下问题:Java ClassFile类的具体用法?Java ClassFile怎么用?Java ClassFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ClassFile类属于javassist.bytecode包,在下文中一共展示了ClassFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
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 );
}
}
示例2: addDefaultConstructor
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 );
}
示例3: transform
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 );
}
}
示例4: transformInvokevirtualsIntoPutAndGetfields
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 );
}
}
示例5: transformInvokevirtualsIntoGetfields
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;
}
示例6: transformInvokevirtualsIntoPutfields
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;
}
示例7: updateInnerEntry
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;
}
}
示例8: getRefClasses
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<String></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;
}
示例9: getClassFile
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;
}
示例10: CtNewClass
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;
}
示例11: main
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);
}
示例12: writeFile0
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();
}
}
示例13: subtypeOf
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;
}
示例14: replaceClassName
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);
}
}
示例15: getNestedClasses
import javassist.bytecode.ClassFile; //导入依赖的package包/类
public CtClass[] getNestedClasses() throws NotFoundException {
ClassFile cf = getClassFile2();
InnerClassesAttribute ica
= (InnerClassesAttribute)cf.getAttribute(InnerClassesAttribute.tag);
if (ica == null)
return new CtClass[0];
String thisName = cf.getName() + "$";
int n = ica.tableLength();
ArrayList<CtClass> list = new ArrayList<CtClass>(n);
for (int i = 0; i < n; i++) {
String name = ica.innerClass(i);
if (name != null)
if (name.startsWith(thisName)) {
// if it is an immediate nested class
if (name.lastIndexOf('$') < thisName.length())
list.add(classPool.get(name));
}
}
return (CtClass[])list.toArray(new CtClass[list.size()]);
}