本文整理汇总了Java中javassist.bytecode.ConstPool类的典型用法代码示例。如果您正苦于以下问题:Java ConstPool类的具体用法?Java ConstPool怎么用?Java ConstPool使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ConstPool类属于javassist.bytecode包,在下文中一共展示了ConstPool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addDefaultConstructor
import javassist.bytecode.ConstPool; //导入依赖的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 );
}
示例2: transformInvokevirtualsIntoGetfields
import javassist.bytecode.ConstPool; //导入依赖的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;
}
示例3: transformInvokevirtualsIntoPutfields
import javassist.bytecode.ConstPool; //导入依赖的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;
}
示例4: updateClass
import javassist.bytecode.ConstPool; //导入依赖的package包/类
public static void updateClass(MethodInfo info, List<String> names) {
// add the names to the class const pool
ConstPool constPool = info.getConstPool();
List<Integer> parameterNameIndices = new ArrayList<Integer>();
for (String name : names) {
if (name != null) {
parameterNameIndices.add(constPool.addUtf8Info(name));
} else {
parameterNameIndices.add(0);
}
}
// add the attribute to the method
info.addAttribute(new MethodParametersAttribute(constPool, parameterNameIndices));
}
示例5: renameLocalVariableTypeAttribute
import javassist.bytecode.ConstPool; //导入依赖的package包/类
private static void renameLocalVariableTypeAttribute(LocalVariableTypeAttribute attribute, ReplacerClassMap map) {
// adapted from LocalVariableAttribute.renameClass()
ConstPool cp = attribute.getConstPool();
int n = attribute.tableLength();
byte[] info = attribute.get();
for (int i = 0; i < n; ++i) {
int pos = i * 10 + 2;
int index = ByteArray.readU16bit(info, pos + 6);
if (index != 0) {
String signature = cp.getUtf8Info(index);
String newSignature = renameLocalVariableSignature(signature, map);
if (newSignature != null) {
ByteArray.write16bit(cp.addUtf8Info(newSignature), info, pos + 6);
}
}
}
}
示例6: rename
import javassist.bytecode.ConstPool; //导入依赖的package包/类
public void rename(CtClass c) {
for (CtBehavior behavior : c.getDeclaredBehaviors()) {
// if there's a local variable table, just rename everything to v1, v2, v3, ... for now
CodeAttribute codeAttribute = behavior.getMethodInfo().getCodeAttribute();
if (codeAttribute == null) {
continue;
}
BehaviorEntry behaviorEntry = EntryFactory.getBehaviorEntry(behavior);
ConstPool constants = c.getClassFile().getConstPool();
LocalVariableAttribute table = (LocalVariableAttribute)codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (table != null) {
renameLVT(behaviorEntry, constants, table);
}
LocalVariableTypeAttribute typeTable = (LocalVariableTypeAttribute)codeAttribute.getAttribute(LocalVariableAttribute.typeTag);
if (typeTable != null) {
renameLVTT(typeTable, table);
}
}
}
示例7: newConstPool
import javassist.bytecode.ConstPool; //导入依赖的package包/类
public static ConstPool newConstPool() {
// const pool expects the name of a class to initialize itself
// but we want an empty pool
// so give it a bogus name, and then clear the entries afterwards
ConstPool pool = new ConstPool("a");
ConstPoolEditor editor = new ConstPoolEditor(pool);
int size = pool.getSize();
for (int i = 0; i < size - 1; i++) {
editor.removeLastItem();
}
// make sure the pool is actually empty
// although, in this case "empty" means one thing in it
// the JVM spec says index 0 should be reserved
assert (pool.getSize() == 1);
assert (editor.getItem(0) == null);
assert (editor.getItem(1) == null);
assert (editor.getItem(2) == null);
assert (editor.getItem(3) == null);
// also, clear the cache
editor.getCache().clear();
return pool;
}
示例8: Annotation
import javassist.bytecode.ConstPool; //导入依赖的package包/类
/**
* Constructs an annotation that can be accessed through the interface
* represented by <code>clazz</code>. The values of the members are
* not specified.
*
* @param cp the constant pool table.
* @param clazz the interface.
* @throws NotFoundException when the clazz is not found
*/
public Annotation(ConstPool cp, CtClass clazz)
throws NotFoundException
{
// todo Enums are not supported right now.
this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp);
if (!clazz.isInterface())
throw new RuntimeException(
"Only interfaces are allowed for Annotation creation.");
CtMethod methods[] = clazz.getDeclaredMethods();
if (methods.length > 0) {
members = new LinkedHashMap<String, Pair>();
}
for (int i = 0; i < methods.length; i++) {
CtClass returnType = methods[i].getReturnType();
addMemberValue(methods[i].getName(),
createMemberValue(cp, returnType));
}
}
示例9: buildExceptionInfo
import javassist.bytecode.ConstPool; //导入依赖的package包/类
private ExceptionInfo[] buildExceptionInfo(MethodInfo method) {
ConstPool constPool = method.getConstPool();
ClassPool classes = clazz.getClassPool();
ExceptionTable table = method.getCodeAttribute().getExceptionTable();
ExceptionInfo[] exceptions = new ExceptionInfo[table.size()];
for (int i = 0; i < table.size(); i++) {
int index = table.catchType(i);
Type type;
try {
type = index == 0 ? Type.THROWABLE : Type.get(classes.get(constPool.getClassInfo(index)));
} catch (NotFoundException e) {
throw new IllegalStateException(e.getMessage());
}
exceptions[i] = new ExceptionInfo(table.startPc(i), table.endPc(i), table.handlerPc(i), type);
}
return exceptions;
}
示例10: doLDC
import javassist.bytecode.ConstPool; //导入依赖的package包/类
private void doLDC(int index) {
TypeData[] stackTypes = this.stackTypes;
int tag = cpool.getTag(index);
if (tag == ConstPool.CONST_String)
stackTypes[stackTop++] = new TypeData.ClassName("java.lang.String");
else if (tag == ConstPool.CONST_Integer)
stackTypes[stackTop++] = INTEGER;
else if (tag == ConstPool.CONST_Float)
stackTypes[stackTop++] = FLOAT;
else if (tag == ConstPool.CONST_Long) {
stackTypes[stackTop++] = LONG;
stackTypes[stackTop++] = TOP;
}
else if (tag == ConstPool.CONST_Double) {
stackTypes[stackTop++] = DOUBLE;
stackTypes[stackTop++] = TOP;
}
else if (tag == ConstPool.CONST_Class)
stackTypes[stackTop++] = new TypeData.ClassName("java.lang.Class");
else
throw new RuntimeException("bad LDC: " + tag);
}
示例11: getClassFields
import javassist.bytecode.ConstPool; //导入依赖的package包/类
public List<FieldInfo> getClassFields(String classname, ConstPool cp) {
ClassPathList list = pathList;
List<FieldInfo> fields = null;
while (list != null) {
if (!(list.path instanceof DalvikClassPath)) {
list = list.next;
continue;
}
fields = ((DalvikClassPath)list.path).getClassFields(classname, cp);
if (fields == null)
list = list.next;
else
return fields;
}
return null; // not found
}
示例12: getClassMethods
import javassist.bytecode.ConstPool; //导入依赖的package包/类
public List<MethodInfo> getClassMethods(String classname, ConstPool cp) {
ClassPathList list = pathList;
List<MethodInfo> methods = null;
while (list != null) {
if (!(list.path instanceof DalvikClassPath)) {
list = list.next;
continue;
}
methods = ((DalvikClassPath)list.path).getClassMethods(classname, cp);
if (methods == null)
list = list.next;
else
return methods;
}
return null; // not found
}
示例13: updateClass
import javassist.bytecode.ConstPool; //导入依赖的package包/类
public static void updateClass(MethodInfo info, List<String> names) {
// add the names to the class const pool
ConstPool constPool = info.getConstPool();
List<Integer> parameterNameIndices = new ArrayList<>();
for (String name : names) {
if (name != null) {
parameterNameIndices.add(constPool.addUtf8Info(name));
} else {
parameterNameIndices.add(0);
}
}
// add the attribute to the method
info.addAttribute(new MethodParametersAttribute(constPool, parameterNameIndices));
}
示例14: getBehaviourBytecode
import javassist.bytecode.ConstPool; //导入依赖的package包/类
/**
* This is basically the InstructionPrinter.getMethodBytecode but with a
* CtBehaviour parameter instead of a CtMethod
*
* @param behavior
* @return
*/
public static String getBehaviourBytecode(CtBehavior behavior) {
MethodInfo info = behavior.getMethodInfo2();
CodeAttribute code = info.getCodeAttribute();
if (code == null) {
return "";
}
ConstPool pool = info.getConstPool();
StringBuilder sb = new StringBuilder(1024);
CodeIterator iterator = code.iterator();
while (iterator.hasNext()) {
int pos;
try {
pos = iterator.next();
} catch (BadBytecode e) {
throw new JVoidIntrumentationException("BadBytecoode", e);
}
sb.append(pos + ": " + InstructionPrinter.instructionString(iterator, pos, pool) + "\n");
}
return sb.toString();
}
示例15: getEntityListeners
import javassist.bytecode.ConstPool; //导入依赖的package包/类
protected Annotation getEntityListeners(ConstPool constantPool, Annotation existingEntityListeners, Annotation templateEntityListeners) {
Annotation listeners = new Annotation(EntityListeners.class.getName(), constantPool);
ArrayMemberValue listenerArray = new ArrayMemberValue(constantPool);
Set<MemberValue> listenerMemberValues = new HashSet<MemberValue>();
{
ArrayMemberValue templateListenerValues = (ArrayMemberValue) templateEntityListeners.getMemberValue("value");
listenerMemberValues.addAll(Arrays.asList(templateListenerValues.getValue()));
logger.debug("Adding template values to new EntityListeners");
}
if (existingEntityListeners != null) {
ArrayMemberValue oldListenerValues = (ArrayMemberValue) existingEntityListeners.getMemberValue("value");
listenerMemberValues.addAll(Arrays.asList(oldListenerValues.getValue()));
logger.debug("Adding previous values to new EntityListeners");
}
listenerArray.setValue(listenerMemberValues.toArray(new MemberValue[listenerMemberValues.size()]));
listeners.addMemberValue("value", listenerArray);
return listeners;
}