use of org.hotswap.agent.javassist.NotFoundException in project HotswapAgent by HotswapProjects.
the class Executor method paramTypesFromDesc.
private Type[] paramTypesFromDesc(String desc) throws BadBytecode {
CtClass[] classes = null;
try {
classes = Descriptor.getParameterTypes(desc, classPool);
} catch (NotFoundException e) {
throw new BadBytecode("Could not find class in descriptor [pos = " + lastPos + "]: " + e.getMessage());
}
if (classes == null)
throw new BadBytecode("Could not obtain parameters for descriptor [pos = " + lastPos + "]: " + desc);
Type[] types = new Type[classes.length];
for (int i = 0; i < types.length; i++) types[i] = Type.get(classes[i]);
return types;
}
use of org.hotswap.agent.javassist.NotFoundException in project HotswapAgent by HotswapProjects.
the class Javac method compileMethod.
private CtBehavior compileMethod(Parser p, MethodDecl md) throws CompileError {
int mod = MemberResolver.getModifiers(md.getModifiers());
CtClass[] plist = gen.makeParamList(md);
CtClass[] tlist = gen.makeThrowsList(md);
recordParams(plist, Modifier.isStatic(mod));
md = p.parseMethod2(stable, md);
try {
if (md.isConstructor()) {
CtConstructor cons = new CtConstructor(plist, gen.getThisClass());
cons.setModifiers(mod);
md.accept(gen);
cons.getMethodInfo().setCodeAttribute(bytecode.toCodeAttribute());
cons.setExceptionTypes(tlist);
return cons;
} else {
Declarator r = md.getReturn();
CtClass rtype = gen.resolver.lookupClass(r);
recordReturnType(rtype, false);
CtMethod method = new CtMethod(rtype, r.getVariable().get(), plist, gen.getThisClass());
method.setModifiers(mod);
gen.setThisMethod(method);
md.accept(gen);
if (md.getBody() != null)
method.getMethodInfo().setCodeAttribute(bytecode.toCodeAttribute());
else
method.setModifiers(mod | Modifier.ABSTRACT);
method.setExceptionTypes(tlist);
return method;
}
} catch (NotFoundException e) {
throw new CompileError(e.toString());
}
}
use of org.hotswap.agent.javassist.NotFoundException in project HotswapAgent by HotswapProjects.
the class ClassInitPlugin method patch.
@OnClassLoadEvent(classNameRegexp = ".*", events = LoadEvent.REDEFINE)
public static void patch(final CtClass ctClass, final ClassLoader classLoader, final Class<?> originalClass) throws IOException, CannotCompileException, NotFoundException {
if (isSyntheticClass(originalClass)) {
return;
}
final String className = ctClass.getName();
try {
CtMethod origMethod = ctClass.getDeclaredMethod(HOTSWAP_AGENT_CLINIT_METHOD);
ctClass.removeMethod(origMethod);
} catch (org.hotswap.agent.javassist.NotFoundException ex) {
// swallow
}
CtConstructor clinit = ctClass.getClassInitializer();
if (clinit != null) {
LOGGER.debug("Adding __ha_clinit to class: {}", className);
CtConstructor haClinit = new CtConstructor(clinit, ctClass, null);
haClinit.getMethodInfo().setName(HOTSWAP_AGENT_CLINIT_METHOD);
haClinit.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
ctClass.addConstructor(haClinit);
final boolean[] reinitializeStatics = new boolean[] { false };
haClinit.instrument(new ExprEditor() {
public void edit(FieldAccess f) throws CannotCompileException {
try {
if (f.isStatic() && f.isWriter()) {
Field originalField = null;
try {
originalField = originalClass.getDeclaredField(f.getFieldName());
} catch (NoSuchFieldException e) {
LOGGER.debug("New field will be initialized {}", f.getFieldName());
reinitializeStatics[0] = true;
}
if (originalField != null) {
// ENUM$VALUES is last in enumeration
if (originalClass.isEnum() && "ENUM$VALUES".equals(f.getFieldName())) {
if (reinitializeStatics[0]) {
LOGGER.debug("New field will be initialized {}", f.getFieldName());
} else {
reinitializeStatics[0] = checkOldEnumValues(ctClass, originalClass);
}
} else {
LOGGER.debug("Skipping old field {}", f.getFieldName());
f.replace("{}");
}
}
}
} catch (Exception e) {
LOGGER.error("Patching __ha_clinit method failed.", e);
}
}
});
if (reinitializeStatics[0]) {
PluginManager.getInstance().getScheduler().scheduleCommand(new Command() {
@Override
public void executeCommand() {
try {
Class<?> clazz = classLoader.loadClass(className);
Method m = clazz.getDeclaredMethod(HOTSWAP_AGENT_CLINIT_METHOD, new Class[] {});
if (m != null) {
m.invoke(null, new Object[] {});
}
} catch (Exception e) {
LOGGER.error("Error initializing redefined class {}", e, className);
} finally {
reloadFlag = false;
}
}
}, // Hack : init should be done after dependant class redefinition. Since the class can
150);
// be proxied by syntetic proxy, the class init must be scheduled after proxy redefinition.
// Currently proxy redefinition (in ProxyPlugin) is scheduled with 100ms delay, therefore
// the class init must be scheduled after it.
} else {
reloadFlag = false;
}
}
}
use of org.hotswap.agent.javassist.NotFoundException in project HotswapAgent by HotswapProjects.
the class Expr method mayThrow.
/**
* Returns the list of exceptions that the expression may throw. This list
* includes both the exceptions that the try-catch statements including the
* expression can catch and the exceptions that the throws declaration
* allows the method to throw.
*/
public CtClass[] mayThrow() {
ClassPool pool = thisClass.getClassPool();
ConstPool cp = thisMethod.getConstPool();
LinkedList list = new LinkedList();
try {
CodeAttribute ca = thisMethod.getCodeAttribute();
ExceptionTable et = ca.getExceptionTable();
int pos = currentPos;
int n = et.size();
for (int i = 0; i < n; ++i) if (et.startPc(i) <= pos && pos < et.endPc(i)) {
int t = et.catchType(i);
if (t > 0)
try {
addClass(list, pool.get(cp.getClassInfo(t)));
} catch (NotFoundException e) {
}
}
} catch (NullPointerException e) {
}
ExceptionsAttribute ea = thisMethod.getExceptionsAttribute();
if (ea != null) {
String[] exceptions = ea.getExceptions();
if (exceptions != null) {
int n = exceptions.length;
for (int i = 0; i < n; ++i) try {
addClass(list, pool.get(exceptions[i]));
} catch (NotFoundException e) {
}
}
}
return (CtClass[]) list.toArray(new CtClass[list.size()]);
}
use of org.hotswap.agent.javassist.NotFoundException in project HotswapAgent by HotswapProjects.
the class TransformCall method matchClass.
private boolean matchClass(String name, ClassPool pool) {
if (classname.equals(name))
return true;
try {
CtClass clazz = pool.get(name);
CtClass declClazz = pool.get(classname);
if (clazz.subtypeOf(declClazz))
try {
CtMethod m = clazz.getMethod(methodname, methodDescriptor);
return m.getDeclaringClass().getName().equals(classname);
} catch (NotFoundException e) {
// maybe the original method has been removed.
return true;
}
} catch (NotFoundException e) {
return false;
}
return false;
}
Aggregations