use of javassist.CtConstructor in project knetbuilder by Rothamsted.
the class UniversalProxyTemplateBuilder method addCnstr.
private void addCnstr(CtClass ctCls, CtClass[] par, String body) {
try {
CtConstructor ctc = new CtConstructor(par, ctCls);
if (body != null) {
ctc.setBody(body);
}
ctCls.addConstructor(ctc);
} catch (Exception e) {
e.printStackTrace();
}
}
use of javassist.CtConstructor in project afterburner by stephanenicolas.
the class AfterBurner method insertConstructor.
/**
* Inserts java instructions into all constructors a given class.
* @param insertableConstructor contains all information about insertion.
* @throws CannotCompileException if the source contained in insertableMethod can't be compiled.
* @throws AfterBurnerImpossibleException if something else goes wrong, wraps other exceptions.
*/
public void insertConstructor(InsertableConstructor insertableConstructor) throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException {
// create or complete onViewCreated
List<CtConstructor> constructorList = extractExistingConstructors(insertableConstructor);
log.info("constructor : " + constructorList.toString());
if (!constructorList.isEmpty()) {
for (CtConstructor constructor : constructorList) {
constructor.insertBeforeBody(insertableConstructor.getConstructorBody(constructor.getParameterTypes()));
}
} else {
throw new AfterBurnerImpossibleException("No suitable constructor was found in class " + insertableConstructor.getClassToInsertInto().getName() + ". Add a constructor that is accepted by the InsertableConstructor. Don't use non static inner classes.");
}
}
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 javaparser by javaparser.
the class JavassistConstructorDeclarationTest method createValue.
@Override
public ResolvedConstructorDeclaration createValue() {
try {
TypeSolver typeSolver = new ReflectionTypeSolver();
CtClass clazz = ClassPool.getDefault().getCtClass("java.lang.StringBuilder");
CtConstructor constructor = clazz.getConstructors()[0];
return new JavassistConstructorDeclaration(constructor, typeSolver);
} catch (NotFoundException e) {
throw new RuntimeException("Unexpected error.", e);
}
}
use of javassist.CtConstructor in project motech by motech.
the class EntityBuilderImpl method injectDefaultConstructor.
private void injectDefaultConstructor(CtClass ctClass) {
CtConstructor[] constructors = ctClass.getDeclaredConstructors();
// No constructors? Nothing to do here - Java will inject default one
if (constructors.length == 0) {
return;
}
try {
for (CtConstructor constructor : constructors) {
int parameters = constructor.getParameterTypes().length;
int modifiers = constructor.getModifiers();
if (parameters == 0 && Modifier.isPublic(modifiers)) {
// Default constructor present? Nothing to do here.
return;
} else if (parameters == 0 && !Modifier.isPublic(modifiers)) {
// If there's a default private or protected constructor, we remove it to create a public one
ctClass.removeConstructor(constructor);
break;
}
}
} catch (NotFoundException e) {
LOGGER.error("Could not read constructor parameters for class {}.", ctClass.getName());
}
// We create and inject default constructor
try {
CtConstructor defaultConstructor = CtNewConstructor.make(new CtClass[] {}, new CtClass[] {}, ctClass);
ctClass.addConstructor(defaultConstructor);
} catch (CannotCompileException e) {
LOGGER.error("Could not create and insert default constructor for class {}.", ctClass.getName());
}
}
Aggregations