Search in sources :

Example 1 with PropertyDetails

use of org.gradle.internal.reflect.PropertyDetails in project gradle by gradle.

the class AbstractClassGenerator method inspectType.

private void inspectType(Class<?> type, ClassMetaData classMetaData) {
    ClassDetails classDetails = ClassInspector.inspect(type);
    boolean hasGetServicesMethod = false;
    for (Method method : classDetails.getAllMethods()) {
        if (method.getAnnotation(Inject.class) != null) {
            if (!Modifier.isPublic(method.getModifiers()) && !Modifier.isProtected(method.getModifiers())) {
                throw new UnsupportedOperationException(String.format("Cannot attach @Inject to method %s.%s() as it is not public or protected.", method.getDeclaringClass().getSimpleName(), method.getName()));
            }
            if (Modifier.isStatic(method.getModifiers())) {
                throw new UnsupportedOperationException(String.format("Cannot attach @Inject to method %s.%s() as it is static.", method.getDeclaringClass().getSimpleName(), method.getName()));
            }
        }
        if ("getServices".equals(method.getName()) && (method.getParameterTypes().length == 0) && ServiceRegistry.class.equals(method.getReturnType())) {
            hasGetServicesMethod = true;
        }
    }
    for (PropertyDetails property : classDetails.getProperties()) {
        PropertyMetaData propertyMetaData = classMetaData.property(property.getName());
        for (Method method : property.getGetters()) {
            propertyMetaData.addGetter(method);
        }
        for (Method method : property.getSetters()) {
            propertyMetaData.addSetter(method);
        }
    }
    for (Method method : classDetails.getInstanceMethods()) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length == 1) {
            classMetaData.addCandidateSetMethod(method);
        }
        if (parameterTypes.length > 0 && parameterTypes[parameterTypes.length - 1].equals(Action.class)) {
            classMetaData.addActionMethod(method);
        } else if (parameterTypes.length > 0 && parameterTypes[parameterTypes.length - 1].equals(Closure.class)) {
            classMetaData.addClosureMethod(method);
        }
    }
    if (!hasGetServicesMethod) {
        for (PropertyMetaData metaData : classMetaData.properties.values()) {
            if (metaData.injector) {
                classMetaData.shouldImplementWithServiceRegistry = true;
            }
        }
    }
}
Also used : Inject(javax.inject.Inject) Action(org.gradle.api.Action) PropertyDetails(org.gradle.internal.reflect.PropertyDetails) Method(java.lang.reflect.Method) ServiceRegistry(org.gradle.internal.service.ServiceRegistry) ClassDetails(org.gradle.internal.reflect.ClassDetails)

Aggregations

Method (java.lang.reflect.Method)1 Inject (javax.inject.Inject)1 Action (org.gradle.api.Action)1 ClassDetails (org.gradle.internal.reflect.ClassDetails)1 PropertyDetails (org.gradle.internal.reflect.PropertyDetails)1 ServiceRegistry (org.gradle.internal.service.ServiceRegistry)1