Search in sources :

Example 21 with CtConstructor

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();
    }
}
Also used : CannotCompileException(javassist.CannotCompileException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CtConstructor(javassist.CtConstructor)

Example 22 with CtConstructor

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.");
    }
}
Also used : AfterBurnerImpossibleException(com.github.stephanenicolas.afterburner.exception.AfterBurnerImpossibleException) CtConstructor(javassist.CtConstructor)

Example 23 with 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;
}
Also used : CtClass(javassist.CtClass) ArrayList(java.util.ArrayList) CtConstructor(javassist.CtConstructor)

Example 24 with CtConstructor

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);
    }
}
Also used : CtClass(javassist.CtClass) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) NotFoundException(javassist.NotFoundException) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CtConstructor(javassist.CtConstructor)

Example 25 with CtConstructor

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());
    }
}
Also used : NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtConstructor(javassist.CtConstructor)

Aggregations

CtConstructor (javassist.CtConstructor)88 CtClass (javassist.CtClass)64 CtMethod (javassist.CtMethod)36 NotFoundException (javassist.NotFoundException)31 ClassPool (javassist.ClassPool)30 CtField (javassist.CtField)30 CannotCompileException (javassist.CannotCompileException)29 IOException (java.io.IOException)13 Method (java.lang.reflect.Method)9 ArrayList (java.util.ArrayList)7 FileNotFoundException (java.io.FileNotFoundException)6 CtNewMethod (javassist.CtNewMethod)6 StorageException (org.apache.skywalking.oap.server.core.storage.StorageException)6 StringWriter (java.io.StringWriter)5 ConstPool (javassist.bytecode.ConstPool)5 MethodInfo (javassist.bytecode.MethodInfo)5 HashMap (java.util.HashMap)4 Annotation (javassist.bytecode.annotation.Annotation)4 OALCompileException (org.apache.skywalking.oap.server.core.oal.rt.OALCompileException)4 ModuleStartException (org.apache.skywalking.oap.server.library.module.ModuleStartException)4