use of javassist.CtClass in project ysoserial by frohoff.
the class JRMPListener method makeDummyObject.
@SuppressWarnings({ "deprecation" })
protected static Object makeDummyObject(String className) {
try {
ClassLoader isolation = new ClassLoader() {
};
ClassPool cp = new ClassPool();
cp.insertClassPath(new ClassClassPath(Dummy.class));
CtClass clazz = cp.get(Dummy.class.getName());
clazz.setName(className);
return clazz.toClass(isolation).newInstance();
} catch (Exception e) {
e.printStackTrace();
return new byte[0];
}
}
use of javassist.CtClass in project transmittable-thread-local by alibaba.
the class TtlTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String classFile, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) {
try {
// Lambda has no class file, no need to transform, just return.
if (classFile == null) {
return EMPTY_BYTE_ARRAY;
}
final String className = toClassName(classFile);
if (EXECUTOR_CLASS_NAMES.contains(className)) {
logger.info("Transforming class " + className);
CtClass clazz = getCtClass(classFileBuffer, loader);
for (CtMethod method : clazz.getDeclaredMethods()) {
updateMethod(clazz, method);
}
return clazz.toBytecode();
} else if (TIMER_TASK_CLASS_NAME.equals(className)) {
CtClass clazz = getCtClass(classFileBuffer, loader);
while (true) {
String name = clazz.getSuperclass().getName();
if (Object.class.getName().equals(name)) {
break;
}
if (TIMER_TASK_CLASS_NAME.equals(name)) {
logger.info("Transforming class " + className);
// FIXME add code here
return EMPTY_BYTE_ARRAY;
}
}
}
} catch (Throwable t) {
String msg = "Fail to transform class " + classFile + ", cause: " + t.toString();
if (logger.isLoggable(Level.SEVERE)) {
logger.log(Level.SEVERE, msg, t);
}
throw new IllegalStateException(msg, t);
}
return EMPTY_BYTE_ARRAY;
}
use of javassist.CtClass in project transmittable-thread-local by alibaba.
the class TtlTransformer method updateMethod.
private static void updateMethod(CtClass clazz, CtMethod method) throws NotFoundException, CannotCompileException {
if (method.getDeclaringClass() != clazz) {
return;
}
final int modifiers = method.getModifiers();
if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) {
return;
}
CtClass[] parameterTypes = method.getParameterTypes();
StringBuilder insertCode = new StringBuilder();
for (int i = 0; i < parameterTypes.length; i++) {
CtClass paraType = parameterTypes[i];
if (RUNNABLE_CLASS_NAME.equals(paraType.getName())) {
String code = String.format("$%d = %s.get($%d, false, true);", i + 1, TTL_RUNNABLE_CLASS_NAME, i + 1);
logger.info("insert code before method " + method + " of class " + method.getDeclaringClass().getName() + ": " + code);
insertCode.append(code);
} else if (CALLABLE_CLASS_NAME.equals(paraType.getName())) {
String code = String.format("$%d = %s.get($%d, false, true);", i + 1, TTL_CALLABLE_CLASS_NAME, i + 1);
logger.info("insert code before method " + method + " of class " + method.getDeclaringClass().getName() + ": " + code);
insertCode.append(code);
}
}
if (insertCode.length() > 0) {
method.insertBefore(insertCode.toString());
}
}
use of javassist.CtClass in project libSBOLj by SynBioDex.
the class OBOTermCreator method createTerm.
// /**
// * Dynamically generates a Class file, for an interface that represents an OBO term with the given fields.
// *
// * The OBO term is uniquely identified by the id, and if a term with this id has been converted into a Class
// * file already, then that Class file (cached within the OBOTermCreator object) will be returned immediately.
// *
// * @param id
// * @param name
// * @param def
// * @param comments
// * @param is_a
// * @param relTypes
// * @param relTypedefs
// * @return term
// * @throws CannotCompileException
// */
public Class createTerm(String id, String name, String def, String[] comments, Class[] is_a, Class[] relTypes, String[] relTypedefs) throws CannotCompileException {
String ccClassName = unmangleName(name);
if (created.containsKey(id)) {
return created.get(id);
}
if (created.containsKey(ccClassName)) {
return created.get(ccClassName);
}
OBOAnnotationParser obo = new OBOAnnotationParser();
ClassPool cp = ClassPool.getDefault();
CtClass stringClass = null, stringArrayClass = null;
try {
stringClass = cp.get("java.lang.String");
stringArrayClass = cp.get("java.lang.String[]");
} catch (NotFoundException e) {
throw new IllegalStateException(e);
}
CtClass cc = cp.makeInterface(ccClassName);
cc.setModifiers(javassist.Modifier.INTERFACE | javassist.Modifier.PUBLIC);
ClassFile ccFile = cc.getClassFile();
ConstPool constpool = ccFile.getConstPool();
Annotation termAnnotation = new Annotation("org.sc.obo.annotations.Term", constpool);
CtField idField = new CtField(stringClass, "id", cc);
idField.setModifiers(javassist.Modifier.PUBLIC | javassist.Modifier.STATIC | javassist.Modifier.FINAL);
CtField nameField = new CtField(stringClass, "name", cc);
nameField.setModifiers(javassist.Modifier.PUBLIC | javassist.Modifier.STATIC | javassist.Modifier.FINAL);
CtField defField = new CtField(stringClass, "def", cc);
defField.setModifiers(javassist.Modifier.PUBLIC | javassist.Modifier.STATIC | javassist.Modifier.FINAL);
cc.addField(idField, CtField.Initializer.constant(removeSlashes(id)));
cc.addField(nameField, CtField.Initializer.constant(removeSlashes(name)));
cc.addField(defField, CtField.Initializer.constant(removeSlashes(def)));
if (is_a != null) {
for (Class superClass : is_a) {
if (!obo.isTerm(superClass)) {
throw new IllegalArgumentException(superClass.getCanonicalName());
}
try {
CtClass superCtClass = cp.get(superClass.getCanonicalName());
cc.addInterface(superCtClass);
} catch (NotFoundException e) {
throw new IllegalArgumentException(e);
}
}
}
/*
* Finally, we convert all the relTypes/relTypedefs into methods.
*
* The main trick here is to do this in a way that the method names don't clash!
*
* We want to rename each method to a form that's unique to this class as well -- the reason is that
* Class A and Class B can have incompatible methods with the same name, which is fine since neither of
* them is a superclass (super-interface, whatevs) of the other.
*
* However, as soon as we define a Class C that extends both interfaces A & B, we have a problem -- suddenly
* we inherit both methods, but with incompatible types.
*
* So we need to mangle the names of A and B's classes, so that they are (a) descriptive, but (b) don't
* clash with other class's method names. This leads to ugly code generation, but ... it works.
*/
if (relTypes != null && relTypedefs != null) {
if (relTypes.length != relTypedefs.length) {
throw new IllegalArgumentException();
}
String[] nonDups = renameDuplicates(relTypedefs);
for (int i = 0; i < relTypes.length; i++) {
try {
if (relTypes[i] == null) {
throw new IllegalArgumentException(id + " " + Arrays.asList(relTypes));
}
Class arrayType = relTypes[i].isArray() ? relTypes[i] : getArrayType(relTypes[i]);
String typeName = arrayType.getCanonicalName();
String methodName = findNonConflictingName(ccClassName, arrayType, nonDups[i], null, is_a);
CtClass relTypeClass = cp.get(typeName);
CtMethod relMethod = new CtMethod(relTypeClass, methodName, new CtClass[] {}, cc);
relMethod.setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT);
// We need to create this with the *original* typedef name,
// not the new (non-clashing) method name. That way, we can recover the original
// name of the property from the (mangled) method name.
Annotation relAnnotation = new Annotation("org.sc.obo.annotations.Relates", constpool);
relAnnotation.addMemberValue("value", new StringMemberValue(relTypedefs[i], ccFile.getConstPool()));
AnnotationsAttribute annotations = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
annotations.addAnnotation(relAnnotation);
relMethod.getMethodInfo().addAttribute(annotations);
cc.addMethod(relMethod);
} catch (NotFoundException e) {
throw new IllegalArgumentException(e);
}
}
}
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
attr.addAnnotation(termAnnotation);
ccFile.addAttribute(attr);
Class c = cc.toClass();
created.put(id, c);
created.put(ccClassName, c);
return c;
}
use of javassist.CtClass in project fakereplace by fakereplace.
the class ClassReplacer method replaceQueuedClasses.
public void replaceQueuedClasses(boolean useFakereplace) {
try {
ClassDefinition[] definitions = new ClassDefinition[queuedClassReplacements.size()];
AddedClass[] newClasses = new AddedClass[addedClasses.size()];
for (Class<?> o : queuedClassReplacements.keySet()) {
Class<?> n = queuedClassReplacements.get(o);
String newName = o.getName();
String oldName = n.getName();
nameReplacements.put(oldName, newName);
}
for (Entry<Class<?>, String> o : addedClasses.entrySet()) {
nameReplacements.put(o.getKey().getName(), o.getValue());
}
int count = 0;
for (Class<?> o : queuedClassReplacements.keySet()) {
Class<?> n = queuedClassReplacements.get(o);
CtClass nc = pool.get(n.getName());
if (nc.isFrozen()) {
nc.defrost();
}
for (String oldName : nameReplacements.keySet()) {
String newName = nameReplacements.get(oldName);
nc.replaceClassName(oldName, newName);
}
nc.setName(o.getName());
ClassDefinition cd = new ClassDefinition(o, nc.toBytecode());
definitions[count++] = cd;
}
count = 0;
for (Entry<Class<?>, String> o : addedClasses.entrySet()) {
CtClass nc = pool.get(o.getKey().getName());
if (nc.isFrozen()) {
nc.defrost();
}
for (String newName : nameReplacements.keySet()) {
String oldName = nameReplacements.get(newName);
nc.replaceClassName(newName, oldName);
}
AddedClass ncd = new AddedClass(o.getValue(), nc.toBytecode(), o.getKey().getClassLoader());
newClasses[count++] = ncd;
}
if (useFakereplace) {
Fakereplace.redefine(definitions, newClasses);
} else {
Fakereplace.getInstrumentation().redefineClasses(definitions);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations