Search in sources :

Example 61 with Annotation

use of java.lang.annotation.Annotation in project jodd by oblac.

the class ValidationContext method collectPropertyAnnotationChecks.

/**
	 * Process all annotations of provided properties.
	 */
protected void collectPropertyAnnotationChecks(List<Check> annChecks, PropertyDescriptor propertyDescriptor) {
    FieldDescriptor fd = propertyDescriptor.getFieldDescriptor();
    if (fd != null) {
        Annotation[] annotations = fd.getField().getAnnotations();
        collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
    }
    MethodDescriptor md = propertyDescriptor.getReadMethodDescriptor();
    if (md != null) {
        Annotation[] annotations = md.getMethod().getAnnotations();
        collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
    }
    md = propertyDescriptor.getWriteMethodDescriptor();
    if (md != null) {
        Annotation[] annotations = md.getMethod().getAnnotations();
        collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
    }
}
Also used : MethodDescriptor(jodd.introspector.MethodDescriptor) Annotation(java.lang.annotation.Annotation) FieldDescriptor(jodd.introspector.FieldDescriptor)

Example 62 with Annotation

use of java.lang.annotation.Annotation in project jodd by oblac.

the class ScopeDataResolver method inspectMethodParameterScopeData.

// ---------------------------------------------------------------- inspect method
/**
	 * Inspects all method parameters for scope type.
	 */
protected ScopeData inspectMethodParameterScopeData(String name, Class type, Annotation[] annotations, ScopeType scopeType) {
    ScopeData sd = new ScopeData();
    int count = 0;
    for (Annotation annotation : annotations) {
        if (annotation instanceof In) {
            ScopeData.In scopeDataIn = inspectIn((In) annotation, scopeType, name, type);
            if (scopeDataIn != null) {
                count++;
                sd.in = new ScopeData.In[] { scopeDataIn };
            }
        } else if (annotation instanceof Out) {
            ScopeData.Out scopeDataOut = inspectOut((Out) annotation, scopeType, name, type);
            if (scopeDataOut != null) {
                count++;
                sd.out = new ScopeData.Out[] { scopeDataOut };
            }
        } else if (annotation instanceof InOut) {
            ScopeData.In scopeDataIn = inspectIn((InOut) annotation, scopeType, name, type);
            if (scopeDataIn != null) {
                count++;
                sd.in = new ScopeData.In[] { scopeDataIn };
            }
            ScopeData.Out scopeDataOut = inspectOut((InOut) annotation, scopeType, name, type);
            if (scopeDataOut != null) {
                count++;
                sd.out = new ScopeData.Out[] { scopeDataOut };
            }
        }
    }
    if (count == 0) {
        return null;
    }
    return sd;
}
Also used : In(jodd.madvoc.meta.In) ScopeData(jodd.madvoc.ScopeData) Annotation(java.lang.annotation.Annotation) InOut(jodd.madvoc.meta.InOut) Out(jodd.madvoc.meta.Out) InOut(jodd.madvoc.meta.InOut)

Example 63 with Annotation

use of java.lang.annotation.Annotation in project jodd by oblac.

the class AnnotatedPropertyInterceptor method lookupAnnotatedProperties.

/**
	 * Lookups for annotated properties. Caches all annotated properties on the first
	 * action class scan. 
	 */
protected PropertyDescriptor[] lookupAnnotatedProperties(Class type) {
    PropertyDescriptor[] properties = annotatedProperties.get(type);
    if (properties != null) {
        return properties;
    }
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    PropertyDescriptor[] allProperties = cd.getAllPropertyDescriptors();
    List<PropertyDescriptor> list = new ArrayList<>();
    for (PropertyDescriptor propertyDescriptor : allProperties) {
        Annotation ann = null;
        if (propertyDescriptor.getFieldDescriptor() != null) {
            ann = propertyDescriptor.getFieldDescriptor().getField().getAnnotation(annotations);
        }
        if (ann == null && propertyDescriptor.getWriteMethodDescriptor() != null) {
            ann = propertyDescriptor.getWriteMethodDescriptor().getMethod().getAnnotation(annotations);
        }
        if (ann == null && propertyDescriptor.getReadMethodDescriptor() != null) {
            ann = propertyDescriptor.getReadMethodDescriptor().getMethod().getAnnotation(annotations);
        }
        if (ann != null) {
            list.add(propertyDescriptor);
        }
    }
    if (list.isEmpty()) {
        properties = EMPTY;
    } else {
        properties = list.toArray(new PropertyDescriptor[list.size()]);
    }
    annotatedProperties.put(type, properties);
    return properties;
}
Also used : PropertyDescriptor(jodd.introspector.PropertyDescriptor) ClassDescriptor(jodd.introspector.ClassDescriptor) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation)

Example 64 with Annotation

use of java.lang.annotation.Annotation in project jodd by oblac.

the class InvReplTest method testReplacement.

@Test
public void testReplacement() throws IllegalAccessException, InstantiationException, NoSuchMethodException, IOException {
    InvokeProxetta proxetta = initProxetta();
    String className = One.class.getCanonicalName();
    byte[] klazz = proxetta.builder(One.class).create();
    //FileUtil.writeBytes("/Users/igor/OneClone.class", klazz);
    FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
    //		PrintStream out = System.out;
    System.setOut(new PrintStream(fbaos));
    One one = (One) ClassLoaderUtil.defineClass((new StringBuilder()).append(className).append(JoddProxetta.invokeProxyClassNameSuffix).toString(), klazz).newInstance();
    // clone ctor calls super ctor,
    assertEquals("one ctor!one ctor!", fbaos.toString());
    fbaos.reset();
    one.example1();
    assertEquals("REPLACED VIRTUAL! jodd.proxetta.inv.Two * one!173>overriden sub", fbaos.toString());
    fbaos.reset();
    one.example2();
    assertEquals("REPLACED STATIC! one * jodd/proxetta/inv/Two * example2 * void example2() * jodd.proxetta.inv.One * jodd.proxetta.inv.One$$Clonetou!15013static: 4", fbaos.toString());
    fbaos.reset();
    one.example3();
    assertEquals("state = REPLACED ctor!", fbaos.toString());
    fbaos.reset();
    assertEquals("jodd.proxetta.inv.One$$Clonetou", one.getClass().getName());
    assertTrue(one instanceof Serializable);
    Annotation[] anns = one.getClass().getAnnotations();
    assertEquals(3, anns.length);
    Method ms = one.getClass().getMethod("example1");
    anns = ms.getAnnotations();
    assertEquals(1, anns.length);
}
Also used : PrintStream(java.io.PrintStream) Serializable(java.io.Serializable) FastByteArrayOutputStream(jodd.io.FastByteArrayOutputStream) Method(java.lang.reflect.Method) InvokeProxetta(jodd.proxetta.impl.InvokeProxetta) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 65 with Annotation

use of java.lang.annotation.Annotation in project openhab1-addons by openhab.

the class MetadataHandler method generate.

/**
     * Scans the class and generates metadata.
     */
public void generate(Class<?> clazz) throws IllegalAccessException {
    if (clazz == null) {
        return;
    }
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getType().getName().startsWith(PACKAGE_TO_SCAN) && !field.isEnumConstant()) {
            generate(field.getType());
        } else {
            for (Annotation annotation : field.getAnnotations()) {
                if (annotation.annotationType().equals(ProviderMappings.class)) {
                    ProviderMappings providerAnnotations = (ProviderMappings) annotation;
                    for (Provider provider : providerAnnotations.value()) {
                        Map<String, ProviderMappingInfo> mappings = providerMappings.get(provider.name());
                        if (mappings == null) {
                            mappings = new HashMap<String, ProviderMappingInfo>();
                            providerMappings.put(provider.name(), mappings);
                        }
                        Converter<?> converter = getConverter(field, provider.converter());
                        String target = clazz.getSimpleName().toLowerCase() + "." + field.getName();
                        ProviderMappingInfo pm = new ProviderMappingInfo(provider.property(), target, converter);
                        mappings.put(pm.getSource(), pm);
                        logger.trace("Added provider mapping {}: {}", provider.name(), pm);
                    }
                } else if (annotation.annotationType().equals(ForecastMappings.class)) {
                    ForecastMappings forecastsAnnotations = (ForecastMappings) annotation;
                    for (Forecast forecast : forecastsAnnotations.value()) {
                        List<String> forecastProperties = forecastMappings.get(forecast.provider());
                        if (forecastProperties == null) {
                            forecastProperties = new ArrayList<String>();
                            forecastMappings.put(forecast.provider(), forecastProperties);
                        }
                        forecastProperties.add(forecast.property());
                        logger.trace("Added forecast mapping {}: {}", forecast.provider(), forecast.property());
                    }
                }
            }
        }
    }
}
Also used : ProviderMappings(org.openhab.binding.weather.internal.annotation.ProviderMappings) ForecastMappings(org.openhab.binding.weather.internal.annotation.ForecastMappings) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) Provider(org.openhab.binding.weather.internal.annotation.Provider) Field(java.lang.reflect.Field) Forecast(org.openhab.binding.weather.internal.annotation.Forecast) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Annotation (java.lang.annotation.Annotation)707 Method (java.lang.reflect.Method)171 ArrayList (java.util.ArrayList)99 Field (java.lang.reflect.Field)76 Test (org.junit.Test)66 Type (java.lang.reflect.Type)64 HashMap (java.util.HashMap)54 HashSet (java.util.HashSet)52 Map (java.util.Map)35 ParameterizedType (java.lang.reflect.ParameterizedType)34 List (java.util.List)30 Set (java.util.Set)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)22 IOException (java.io.IOException)20 BindingAnnotation (com.google.inject.BindingAnnotation)17 AbstractModule (com.google.inject.AbstractModule)16 TypeElement (javax.lang.model.element.TypeElement)15 Injector (com.google.inject.Injector)14 MediaType (okhttp3.MediaType)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)13