Search in sources :

Example 1 with PetiteException

use of jodd.petite.PetiteException in project jodd by oblac.

the class AutomagicPetiteConfigurator method onEntry.

/**
	 * Scans all classes and registers only those annotated with {@link jodd.petite.meta.PetiteBean}.
	 * Because of performance purposes, classes are not dynamically loaded; instead, their
	 * file content is examined. 
	 */
@Override
protected void onEntry(EntryData entryData) {
    String entryName = entryData.getName();
    InputStream inputStream = entryData.openInputStream();
    if (!isTypeSignatureInUse(inputStream, petiteBeanAnnotationBytes)) {
        return;
    }
    Class<?> beanClass;
    try {
        beanClass = loadClass(entryName);
    } catch (ClassNotFoundException cnfex) {
        throw new PetiteException("Unable to load class: " + cnfex, cnfex);
    }
    if (beanClass == null) {
        return;
    }
    PetiteBean petiteBean = beanClass.getAnnotation(PetiteBean.class);
    if (petiteBean == null) {
        return;
    }
    container.registerPetiteBean(beanClass, null, null, null, false);
}
Also used : PetiteBean(jodd.petite.meta.PetiteBean) InputStream(java.io.InputStream) PetiteException(jodd.petite.PetiteException)

Example 2 with PetiteException

use of jodd.petite.PetiteException in project jodd by oblac.

the class AutomagicPetiteConfigurator method configure.

/**
	 * Configures {@link jodd.petite.PetiteContainer} with specified class path.
	 * @see AutomagicPetiteConfigurator#configure(jodd.petite.PetiteContainer)
	 */
public void configure(PetiteContainer petiteContainer, File[] classpath) {
    this.container = petiteContainer;
    rulesEntries.smartMode();
    elapsed = System.currentTimeMillis();
    try {
        scanPaths(classpath);
    } catch (Exception ex) {
        throw new PetiteException("Scan classpath error", ex);
    }
    elapsed = System.currentTimeMillis() - elapsed;
    log.info("Petite configured in " + elapsed + " ms. Total beans: " + petiteContainer.getTotalBeans());
}
Also used : PetiteException(jodd.petite.PetiteException) PetiteException(jodd.petite.PetiteException)

Example 3 with PetiteException

use of jodd.petite.PetiteException in project jodd by oblac.

the class CtorResolver method resolve.

/**
	 * Resolves constructor injection point from type. Looks for single annotated constructor.
	 * If no annotated constructors found, the total number of constructors will be checked.
	 * If there is only one constructor, that one will be used as injection point. If more
	 * constructors exist, the default one will be used as injection point. Otherwise, exception
	 * is thrown.
	 */
public CtorInjectionPoint resolve(Class type, boolean useAnnotation) {
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    CtorDescriptor[] allCtors = cd.getAllCtorDescriptors();
    Constructor foundedCtor = null;
    Constructor defaultCtor = null;
    String refValues = null;
    for (CtorDescriptor ctorDescriptor : allCtors) {
        Constructor<?> ctor = ctorDescriptor.getConstructor();
        Class<?>[] paramTypes = ctor.getParameterTypes();
        if (paramTypes.length == 0) {
            // detects default ctors
            defaultCtor = ctor;
        }
        if (!useAnnotation) {
            continue;
        }
        PetiteInject ref = ctor.getAnnotation(PetiteInject.class);
        if (ref == null) {
            continue;
        }
        if (foundedCtor != null) {
            throw new PetiteException("Two or more constructors are annotated as injection points in bean: " + type.getName());
        }
        foundedCtor = ctor;
        refValues = ref.value().trim();
    }
    if (foundedCtor == null) {
        if (allCtors.length == 1) {
            foundedCtor = allCtors[0].getConstructor();
        } else {
            foundedCtor = defaultCtor;
        }
    }
    if (foundedCtor == null) {
        throw new PetiteException("No constructor (annotated, single or default) founded as injection point for: " + type.getName());
    }
    String[][] references = PetiteUtil.convertAnnValueToReferences(refValues);
    return injectionPointFactory.createCtorInjectionPoint(foundedCtor, references);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) Constructor(java.lang.reflect.Constructor) PetiteInject(jodd.petite.meta.PetiteInject) CtorDescriptor(jodd.introspector.CtorDescriptor) PetiteException(jodd.petite.PetiteException)

Example 4 with PetiteException

use of jodd.petite.PetiteException in project jodd by oblac.

the class DestroyMethodResolver method resolve.

public DestroyMethodPoint[] resolve(Object bean) {
    Class<?> type = bean.getClass();
    // lookup methods
    List<DestroyMethodPoint> list = new ArrayList<>();
    ClassDescriptor cd = new ClassDescriptor(type, false, false, false, null);
    MethodDescriptor[] allMethods = cd.getAllMethodDescriptors();
    for (MethodDescriptor methodDescriptor : allMethods) {
        Method method = methodDescriptor.getMethod();
        PetiteDestroyMethod petiteDestroyMethod = method.getAnnotation(PetiteDestroyMethod.class);
        if (petiteDestroyMethod == null) {
            continue;
        }
        if (method.getParameterTypes().length > 0) {
            throw new PetiteException("Arguments are not allowed for Petite destroy method: " + type.getName() + '#' + method.getName());
        }
        list.add(new DestroyMethodPoint(method));
    }
    DestroyMethodPoint[] methods;
    if (list.isEmpty()) {
        methods = DestroyMethodPoint.EMPTY;
    } else {
        methods = list.toArray(new DestroyMethodPoint[list.size()]);
    }
    return methods;
}
Also used : DestroyMethodPoint(jodd.petite.DestroyMethodPoint) ClassDescriptor(jodd.introspector.ClassDescriptor) PetiteDestroyMethod(jodd.petite.meta.PetiteDestroyMethod) ArrayList(java.util.ArrayList) PetiteDestroyMethod(jodd.petite.meta.PetiteDestroyMethod) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor) PetiteException(jodd.petite.PetiteException)

Example 5 with PetiteException

use of jodd.petite.PetiteException in project jodd by oblac.

the class InitMethodResolver method resolve.

public InitMethodPoint[] resolve(Object bean) {
    Class<?> type = bean.getClass();
    // lookup methods
    List<InitMethodPoint> list = new ArrayList<>();
    ClassDescriptor cd = new ClassDescriptor(type, false, false, false, null);
    MethodDescriptor[] allMethods = cd.getAllMethodDescriptors();
    for (MethodDescriptor methodDescriptor : allMethods) {
        Method method = methodDescriptor.getMethod();
        PetiteInitMethod petiteInitMethod = method.getAnnotation(PetiteInitMethod.class);
        if (petiteInitMethod == null) {
            continue;
        }
        if (method.getParameterTypes().length > 0) {
            throw new PetiteException("Arguments are not allowed for Petite init method: " + type.getName() + '#' + method.getName());
        }
        int order = petiteInitMethod.order();
        list.add(new InitMethodPoint(method, order, petiteInitMethod.invoke()));
    }
    InitMethodPoint[] methods;
    if (list.isEmpty()) {
        methods = InitMethodPoint.EMPTY;
    } else {
        Collections.sort(list);
        methods = list.toArray(new InitMethodPoint[list.size()]);
    }
    return methods;
}
Also used : InitMethodPoint(jodd.petite.InitMethodPoint) PetiteInitMethod(jodd.petite.meta.PetiteInitMethod) ClassDescriptor(jodd.introspector.ClassDescriptor) ArrayList(java.util.ArrayList) PetiteInitMethod(jodd.petite.meta.PetiteInitMethod) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor) PetiteException(jodd.petite.PetiteException) InitMethodPoint(jodd.petite.InitMethodPoint)

Aggregations

PetiteException (jodd.petite.PetiteException)5 ClassDescriptor (jodd.introspector.ClassDescriptor)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 MethodDescriptor (jodd.introspector.MethodDescriptor)2 InputStream (java.io.InputStream)1 Constructor (java.lang.reflect.Constructor)1 CtorDescriptor (jodd.introspector.CtorDescriptor)1 DestroyMethodPoint (jodd.petite.DestroyMethodPoint)1 InitMethodPoint (jodd.petite.InitMethodPoint)1 PetiteBean (jodd.petite.meta.PetiteBean)1 PetiteDestroyMethod (jodd.petite.meta.PetiteDestroyMethod)1 PetiteInitMethod (jodd.petite.meta.PetiteInitMethod)1 PetiteInject (jodd.petite.meta.PetiteInject)1