use of javassist.CtClass in project uavstack by uavorg.
the class TtlTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String classFile, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws IllegalClassFormatException {
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 (THREAD_POOL_CLASS_FILE.equals(classFile) || SCHEDULER_CLASS_FILE.equals(classFile)) {
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_FILE.equals(classFile)) {
CtClass clazz = getCtClass(classFileBuffer, loader);
while (true) {
String name = clazz.getSuperclass().getName();
if (Object.class.getName().equals(name)) {
break;
}
if (TIMER_TASK_CLASS_FILE.equals(name)) {
logger.info("Transforming class " + className);
// FIXME add code here
return EMPTY_BYTE_ARRAY;
}
}
}
} catch (Throwable t) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
t.printStackTrace(printWriter);
String msg = "Fail to transform class " + classFile + ", cause: " + stringWriter.toString();
logger.severe(msg);
throw new IllegalStateException(msg, t);
}
return EMPTY_BYTE_ARRAY;
}
use of javassist.CtClass in project BIMserver by opensourceBIM.
the class RealtimeReflectorFactoryBuilder method build1.
private void build1(String newClassPrefix, Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
try {
CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Impl" + newClassPrefix);
reflectorImplClass.addInterface(pool.get(interfaceClass.getName()));
CtClass reflectorClass = pool.get(Reflector.class.getName());
CtField reflectorField = new CtField(reflectorClass, "reflector", reflectorImplClass);
reflectorImplClass.addField(reflectorField);
CtConstructor constructor = new CtConstructor(new CtClass[] { reflectorClass }, reflectorImplClass);
StringBuilder sb = new StringBuilder();
reflectorImplClass.addConstructor(constructor);
sb.append("{");
sb.append("this.reflector = $1;");
sb.append("}");
constructor.setBody(sb.toString());
for (SMethod sMethod : sService.getMethods()) {
CtClass[] parameters = new CtClass[sMethod.getParameters().size()];
int i = 0;
for (org.bimserver.shared.meta.SParameter sParameter : sMethod.getParameters()) {
parameters[i] = pool.get(sParameter.getType().toJavaCode());
i++;
}
CtMethod method = new CtMethod(pool.get(sMethod.getReturnType().toJavaCode()), sMethod.getName(), parameters, reflectorImplClass);
StringBuilder methodBuilder = new StringBuilder();
methodBuilder.append("{");
if (sMethod.getReturnType().isVoid()) {
} else {
methodBuilder.append("return (" + sMethod.getReturnType().toJavaCode() + ")");
}
methodBuilder.append("reflector.callMethod(\"" + interfaceClass.getSimpleName() + "\", \"" + sMethod.getName() + "\", " + sMethod.getReturnType().toJavaCode() + ".class");
if (sMethod.getParameters().isEmpty()) {
methodBuilder.append(", new " + KeyValuePair.class.getName() + "[0]");
} else {
methodBuilder.append(", new " + KeyValuePair.class.getName() + "[]{");
int x = 1;
for (SParameter sParameter : sMethod.getParameters()) {
methodBuilder.append("new " + KeyValuePair.class.getName() + "(\"" + sParameter.getName() + "\", $" + x + ")");
if (sMethod.getParameter(sMethod.getParameters().size() - 1) != sParameter) {
methodBuilder.append(", ");
}
x++;
}
methodBuilder.append("}");
}
methodBuilder.append(");");
methodBuilder.append("}");
method.setBody(methodBuilder.toString());
reflectorImplClass.addMethod(method);
}
pool.toClass(reflectorImplClass, GeneratedNeighbourClass.class, getClass().getClassLoader(), getClass().getProtectionDomain());
} catch (Exception e) {
LOGGER.error("", e);
}
}
use of javassist.CtClass in project BIMserver by opensourceBIM.
the class RealtimeReflectorFactoryBuilder method createCreateReflectorMethod1.
private void createCreateReflectorMethod1(String newClassPrefix, CtClass reflectorFactoryImpl) throws NotFoundException, CannotCompileException {
CtClass[] parameters = new CtClass[2];
parameters[0] = pool.get(Class.class.getName());
parameters[1] = pool.get(Reflector.class.getName());
CtMethod method = new CtMethod(pool.get(PublicInterface.class.getName()), "createReflector", parameters, reflectorFactoryImpl);
StringBuilder methodBuilder = new StringBuilder();
methodBuilder.append("{");
methodBuilder.append("if (1==0) {");
for (String name : servicesMap.keySetName()) {
SService sService = servicesMap.getByName(name);
methodBuilder.append("} else if ($1.getSimpleName().equals(\"" + sService.getSimpleName() + "\")) {");
methodBuilder.append("return new " + GENERATED_CLASSES_PACKAGE + "." + sService.getSimpleName() + "Impl" + newClassPrefix + "($2);");
}
methodBuilder.append("}");
methodBuilder.append("return null;");
methodBuilder.append("}");
method.setBody(methodBuilder.toString());
reflectorFactoryImpl.addMethod(method);
}
use of javassist.CtClass in project BIMserver by opensourceBIM.
the class RealtimeReflectorFactoryBuilder method build2.
private void build2(String newClassPrefix, Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
try {
CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Reflector" + newClassPrefix);
CtClass reflectorClass = pool.get(Reflector.class.getName());
CtClass interfaceCtClass = pool.get(interfaceClass.getName());
reflectorImplClass.addInterface(reflectorClass);
CtField reflectorField = new CtField(interfaceCtClass, "publicInterface", reflectorImplClass);
reflectorImplClass.addField(reflectorField);
CtConstructor constructor = new CtConstructor(new CtClass[] { interfaceCtClass }, reflectorImplClass);
StringBuilder sb = new StringBuilder();
reflectorImplClass.addConstructor(constructor);
sb.append("{");
sb.append("this.publicInterface = $1;");
sb.append("}");
constructor.setBody(sb.toString());
CtClass[] parameters = new CtClass[4];
parameters[0] = pool.get(String.class.getName());
parameters[1] = pool.get(String.class.getName());
parameters[2] = pool.get(Class.class.getName());
parameters[3] = pool.get(KeyValuePair.class.getName() + "[]");
CtMethod method = new CtMethod(pool.get(Object.class.getName()), "callMethod", parameters, reflectorImplClass);
StringBuilder methodBuilder = new StringBuilder();
methodBuilder.append("{");
methodBuilder.append("if (1==0) {} ");
for (SMethod sMethod : sService.getMethods()) {
methodBuilder.append(" else if ($2.equals(\"" + sMethod.getName() + "\")) {");
if (!sMethod.getReturnType().isVoid()) {
methodBuilder.append("return ");
}
methodBuilder.append("publicInterface." + sMethod.getName() + "(");
int i = 0;
for (SParameter sParameter : sMethod.getParameters()) {
methodBuilder.append("(" + sParameter.getType().toJavaCode() + ")$4[" + i + "].getValue()");
if (i < sMethod.getParameters().size() - 1) {
methodBuilder.append(", ");
}
i++;
}
methodBuilder.append(");");
methodBuilder.append("}");
}
methodBuilder.append("return null;");
methodBuilder.append("}");
method.setBody(methodBuilder.toString());
reflectorImplClass.addMethod(method);
pool.toClass(reflectorImplClass, GeneratedNeighbourClass.class, getClass().getClassLoader(), getClass().getProtectionDomain());
} catch (Exception e) {
LOGGER.error("", e);
}
}
use of javassist.CtClass in project hibernate-orm by hibernate.
the class PersistentAttributesEnhancer method extendedEnhancement.
// --- //
/**
* Replace access to fields of entities (for example, entity.field) with a call to the enhanced getter / setter
* (in this example, entity.$$_hibernate_read_field()). It's assumed that the target entity is enhanced as well.
*
* @param aCtClass Class to enhance (not an entity class).
*/
public void extendedEnhancement(CtClass aCtClass) {
final ConstPool constPool = aCtClass.getClassFile().getConstPool();
final ClassPool classPool = aCtClass.getClassPool();
for (Object oMethod : aCtClass.getClassFile().getMethods()) {
final MethodInfo methodInfo = (MethodInfo) oMethod;
final String methodName = methodInfo.getName();
// skip methods added by enhancement and abstract methods (methods without any code)
if (methodName.startsWith("$$_hibernate_") || methodInfo.getCodeAttribute() == null) {
continue;
}
try {
final CodeIterator itr = methodInfo.getCodeAttribute().iterator();
while (itr.hasNext()) {
int index = itr.next();
int op = itr.byteAt(index);
if (op != Opcode.PUTFIELD && op != Opcode.GETFIELD) {
continue;
}
String fieldName = constPool.getFieldrefName(itr.u16bitAt(index + 1));
String fieldClassName = constPool.getClassInfo(constPool.getFieldrefClass(itr.u16bitAt(index + 1)));
CtClass targetCtClass = classPool.getCtClass(fieldClassName);
if (!enhancementContext.isEntityClass(targetCtClass) && !enhancementContext.isCompositeClass(targetCtClass)) {
continue;
}
if (targetCtClass == aCtClass || !enhancementContext.isPersistentField(targetCtClass.getField(fieldName)) || PersistentAttributesHelper.hasAnnotation(targetCtClass, fieldName, Id.class) || "this$0".equals(fieldName)) {
continue;
}
log.debugf("Extended enhancement: Transforming access to field [%s.%s] from method [%s#%s]", fieldClassName, fieldName, aCtClass.getName(), methodName);
if (op == Opcode.GETFIELD) {
int fieldReaderMethodIndex = constPool.addMethodrefInfo(constPool.addClassInfo(fieldClassName), EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + fieldName, "()" + constPool.getFieldrefType(itr.u16bitAt(index + 1)));
itr.writeByte(Opcode.INVOKEVIRTUAL, index);
itr.write16bit(fieldReaderMethodIndex, index + 1);
} else {
int fieldWriterMethodIndex = constPool.addMethodrefInfo(constPool.addClassInfo(fieldClassName), EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + fieldName, "(" + constPool.getFieldrefType(itr.u16bitAt(index + 1)) + ")V");
itr.writeByte(Opcode.INVOKEVIRTUAL, index);
itr.write16bit(fieldWriterMethodIndex, index + 1);
}
}
methodInfo.getCodeAttribute().setAttribute(MapMaker.make(classPool, methodInfo));
} catch (BadBytecode bb) {
final String msg = String.format("Unable to perform extended enhancement in method [%s]", methodName);
throw new EnhancementException(msg, bb);
} catch (NotFoundException nfe) {
final String msg = String.format("Unable to perform extended enhancement in method [%s]", methodName);
throw new EnhancementException(msg, nfe);
}
}
}
Aggregations