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);
}
}
use of javassist.CtConstructor in project incubator-servicecomb-java-chassis by apache.
the class JavassistUtils method addEnumConstructor.
private static void addEnumConstructor(ClassPool classPool, CtClass ctClass) throws Exception {
String src = "super($1, $2);";
CtConstructor ctConstructor = new CtConstructor(classPool.get(new String[] { String.class.getName(), int.class.getName() }), ctClass);
ctConstructor.setBody(src);
ctClass.addConstructor(ctConstructor);
}
use of javassist.CtConstructor in project systemml by apache.
the class GenerateClassesForMLContext method createScriptClass.
/**
* Convert a script file to a Java class that extends the MLContext API's
* Script class.
*
* @param scriptFilePath
* the path to a script file
*/
public static void createScriptClass(String scriptFilePath) {
try {
String fullScriptClassName = BASE_DEST_PACKAGE + "." + scriptFilePathToFullClassNameNoBase(scriptFilePath);
System.out.println("Generating Class: " + fullScriptClassName);
ClassPool pool = ClassPool.getDefault();
CtClass ctNewScript = pool.makeClass(fullScriptClassName);
CtClass ctScript = pool.get(Script.class.getName());
ctNewScript.setSuperclass(ctScript);
CtConstructor ctCon = new CtConstructor(null, ctNewScript);
ctCon.setBody(scriptConstructorBody(scriptFilePath));
ctNewScript.addConstructor(ctCon);
addFunctionMethods(scriptFilePath, ctNewScript);
ctNewScript.writeFile(destination);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (CannotCompileException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
use of javassist.CtConstructor in project powermock by powermock.
the class JavaAssistTestClassTransformer 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 powermock by powermock.
the class ClassReplicaCreator method createInstanceReplica.
/**
* Create a class that is a replica of type {@code T}. To allow for
* partial mocking all calls to non-mocked methods will be delegated to the
* {@code delegator}.
*
* @param <T> The type of the replica class to be created.
* @param delegator The delegator object that will be invoked to allow for partial
* mocking.
* @return A replica class that can be used to duck-type an instance.
*/
@SuppressWarnings("unchecked")
public <T> Class<T> createInstanceReplica(T delegator) {
if (delegator == null) {
throw new IllegalArgumentException("delegator cannot be null");
}
final Class<T> clazz = (Class<T>) delegator.getClass();
ClassPool classpool = ClassPool.getDefault();
final String originalClassName = clazz.getName();
CtClass originalClassAsCtClass;
final CtClass newClass = classpool.makeClass(generateReplicaClassName(clazz));
try {
originalClassAsCtClass = classpool.get(originalClassName);
copyFields(originalClassAsCtClass, newClass);
addDelegatorField(delegator, newClass);
CtMethod[] declaredMethods = originalClassAsCtClass.getDeclaredMethods();
for (CtMethod ctMethod : declaredMethods) {
@SuppressWarnings("unused") final String code = getReplicaMethodDelegationCode(delegator.getClass(), ctMethod, POWERMOCK_INSTANCE_DELEGATOR_FIELD_NAME);
CtMethod make2 = CtNewMethod.copy(ctMethod, newClass, null);
newClass.addMethod(make2);
}
CtConstructor[] declaredConstructors = originalClassAsCtClass.getDeclaredConstructors();
for (CtConstructor ctConstructor : declaredConstructors) {
CtConstructor copy = CtNewConstructor.copy(ctConstructor, newClass, null);
newClass.addConstructor(copy);
}
return (Class<T>) newClass.toClass(this.getClass().getClassLoader(), this.getClass().getProtectionDomain());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations