use of org.hotswap.agent.javassist.bytecode.MethodInfo in project HotswapAgent by HotswapProjects.
the class FieldInitLink method modifyClassConstructor.
private void modifyClassConstructor(ClassFile cf, Bytecode code, int stacksize, int localsize) throws CannotCompileException {
MethodInfo m = cf.getStaticInitializer();
if (m == null) {
code.add(Bytecode.RETURN);
code.setMaxStack(stacksize);
code.setMaxLocals(localsize);
m = new MethodInfo(cf.getConstPool(), "<clinit>", "()V");
m.setAccessFlags(AccessFlag.STATIC);
m.setCodeAttribute(code.toCodeAttribute());
cf.addMethod(m);
CtMember.Cache cache = hasMemberCache();
if (cache != null)
cache.addConstructor(new CtConstructor(m, this));
} else {
CodeAttribute codeAttr = m.getCodeAttribute();
if (codeAttr == null)
throw new CannotCompileException("empty <clinit>");
try {
CodeIterator it = codeAttr.iterator();
int pos = it.insertEx(code.get());
it.insert(code.getExceptionTable(), pos);
int maxstack = codeAttr.getMaxStack();
if (maxstack < stacksize)
codeAttr.setMaxStack(stacksize);
int maxlocals = codeAttr.getMaxLocals();
if (maxlocals < localsize)
codeAttr.setMaxLocals(localsize);
} catch (BadBytecode e) {
throw new CannotCompileException(e);
}
}
try {
m.rebuildStackMapIf6(classPool, cf);
} catch (BadBytecode e) {
throw new CannotCompileException(e);
}
}
use of org.hotswap.agent.javassist.bytecode.MethodInfo in project HotswapAgent by HotswapProjects.
the class Reflection method rebuildClassFile.
public void rebuildClassFile(ClassFile cf) throws BadBytecode {
if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_6)
return;
Iterator methods = cf.getMethods().iterator();
while (methods.hasNext()) {
MethodInfo mi = (MethodInfo) methods.next();
mi.rebuildStackMap(classPool);
}
}
use of org.hotswap.agent.javassist.bytecode.MethodInfo in project HotswapAgent by HotswapProjects.
the class FramePrinter method print.
/**
* Prints the instructions and the frame states of the given method.
*/
public void print(CtMethod method) {
stream.println("\n" + getMethodString(method));
MethodInfo info = method.getMethodInfo2();
ConstPool pool = info.getConstPool();
CodeAttribute code = info.getCodeAttribute();
if (code == null)
return;
Frame[] frames;
try {
frames = (new Analyzer()).analyze(method.getDeclaringClass(), info);
} catch (BadBytecode e) {
throw new RuntimeException(e);
}
int spacing = String.valueOf(code.getCodeLength()).length();
CodeIterator iterator = code.iterator();
while (iterator.hasNext()) {
int pos;
try {
pos = iterator.next();
} catch (BadBytecode e) {
throw new RuntimeException(e);
}
stream.println(pos + ": " + InstructionPrinter.instructionString(iterator, pos, pool));
addSpacing(spacing + 3);
Frame frame = frames[pos];
if (frame == null) {
stream.println("--DEAD CODE--");
continue;
}
printStack(frame);
addSpacing(spacing + 3);
printLocals(frame);
}
}
use of org.hotswap.agent.javassist.bytecode.MethodInfo in project HotswapAgent by HotswapProjects.
the class AnnotationImpl method getDefault.
private Object getDefault(String name, Method method) throws ClassNotFoundException, RuntimeException {
String classname = annotation.getTypeName();
if (pool != null) {
try {
CtClass cc = pool.get(classname);
ClassFile cf = cc.getClassFile2();
MethodInfo minfo = cf.getMethod(name);
if (minfo != null) {
AnnotationDefaultAttribute ainfo = (AnnotationDefaultAttribute) minfo.getAttribute(AnnotationDefaultAttribute.tag);
if (ainfo != null) {
MemberValue mv = ainfo.getDefaultValue();
return mv.getValue(classLoader, pool, method);
}
}
} catch (NotFoundException e) {
throw new RuntimeException("cannot find a class file: " + classname);
}
}
throw new RuntimeException("no default value: " + classname + "." + name + "()");
}
use of org.hotswap.agent.javassist.bytecode.MethodInfo in project HotswapAgent by HotswapProjects.
the class Expr method where.
/**
* Returns the constructor or method containing the expression.
*/
public CtBehavior where() {
MethodInfo mi = thisMethod;
CtBehavior[] cb = thisClass.getDeclaredBehaviors();
for (int i = cb.length - 1; i >= 0; --i) if (cb[i].getMethodInfo2() == mi)
return cb[i];
CtConstructor init = thisClass.getClassInitializer();
if (init != null && init.getMethodInfo2() == mi)
return init;
/* getDeclaredBehaviors() returns a list of methods/constructors.
* Although the list is cached in a CtClass object, it might be
* recreated for some reason. Thus, the member name and the signature
* must be also checked.
*/
for (int i = cb.length - 1; i >= 0; --i) {
if (thisMethod.getName().equals(cb[i].getMethodInfo2().getName()) && thisMethod.getDescriptor().equals(cb[i].getMethodInfo2().getDescriptor())) {
return cb[i];
}
}
throw new RuntimeException("fatal: not found");
}
Aggregations