use of javassist.CtConstructor in project java-chassis by ServiceComb.
the class JavassistUtils method addEnumConstructor.
private static void addEnumConstructor(CtClass ctClass) throws Exception {
String src = "super($1, $2);";
CtConstructor ctConstructor = new CtConstructor(POOL.get(new String[] { String.class.getName(), int.class.getName() }), ctClass);
ctConstructor.setBody(src);
ctClass.addConstructor(ctConstructor);
}
use of javassist.CtConstructor in project afterburner by stephanenicolas.
the class AfterBurner method extractExistingConstructors.
private List<CtConstructor> extractExistingConstructors(final InsertableConstructor insertableConstructor) throws NotFoundException, AfterBurnerImpossibleException {
List<CtConstructor> constructors = new ArrayList<CtConstructor>();
CtConstructor[] declaredConstructors = insertableConstructor.getClassToInsertInto().getDeclaredConstructors();
for (CtConstructor constructor : declaredConstructors) {
CtClass[] paramClasses = constructor.getParameterTypes();
if (insertableConstructor.acceptParameters(paramClasses)) {
constructors.add(constructor);
}
}
return constructors;
}
use of javassist.CtConstructor in project powermock by powermock.
the class TestClassTransformer method addConstructorNotification.
private void addConstructorNotification(final CtClass clazz) throws CannotCompileException {
final String notificationCode = GlobalNotificationBuildSupport.class.getName() + ".testInstanceCreated(this);";
final boolean asFinally = !hasSuperClass(clazz);
for (final CtConstructor constr : clazz.getDeclaredConstructors()) {
constr.insertAfter(notificationCode, asFinally);
}
}
use of javassist.CtConstructor in project BIMserver by opensourceBIM.
the class RealtimeReflectorFactoryBuilder method build1.
private void build1(Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
try {
CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Impl" + implementationCounter);
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, getClass().getClassLoader(), getClass().getProtectionDomain());
} catch (Exception e) {
LOGGER.error("", e);
}
}
use of javassist.CtConstructor in project BIMserver by opensourceBIM.
the class RealtimeReflectorFactoryBuilder method build2.
private void build2(Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
try {
CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Reflector" + implementationCounter);
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, getClass().getClassLoader(), getClass().getProtectionDomain());
} catch (Exception e) {
LOGGER.error("", e);
}
}
Aggregations