Search in sources :

Example 51 with Annotation

use of java.lang.annotation.Annotation in project junit4 by junit-team.

the class TestClass method addToAnnotationLists.

protected static <T extends FrameworkMember<T>> void addToAnnotationLists(T member, Map<Class<? extends Annotation>, List<T>> map) {
    for (Annotation each : member.getAnnotations()) {
        Class<? extends Annotation> type = each.annotationType();
        List<T> members = getAnnotatedMembers(map, type, true);
        T memberToAdd = member.handlePossibleBridgeMethod(members);
        if (memberToAdd == null) {
            return;
        }
        if (runsTopToBottom(type)) {
            members.add(0, memberToAdd);
        } else {
            members.add(memberToAdd);
        }
    }
}
Also used : Annotation(java.lang.annotation.Annotation)

Example 52 with Annotation

use of java.lang.annotation.Annotation in project pinot by linkedin.

the class PinotRestletApplication method attachRoutesForClass.

protected void attachRoutesForClass(Router router, Class<? extends ServerResource> clazz) {
    TreeSet<String> pathsOrderedByLength = new TreeSet<String>(ComparatorUtils.chainedComparator(new Comparator<String>() {

        @Override
        public int compare(String left, String right) {
            int leftLength = left.length();
            int rightLength = right.length();
            return leftLength < rightLength ? -1 : (leftLength == rightLength ? 0 : 1);
        }
    }, ComparatorUtils.NATURAL_COMPARATOR));
    for (Method method : clazz.getDeclaredMethods()) {
        Annotation annotationInstance = method.getAnnotation(Paths.class);
        if (annotationInstance != null) {
            pathsOrderedByLength.addAll(Arrays.asList(((Paths) annotationInstance).value()));
        }
    }
    for (String routePath : pathsOrderedByLength) {
        LOGGER.info("Attaching route {} -> {}", routePath, clazz.getSimpleName());
        attachRoute(router, routePath, clazz);
    }
}
Also used : TreeSet(java.util.TreeSet) Method(java.lang.reflect.Method) Paths(com.linkedin.pinot.common.restlet.swagger.Paths) Annotation(java.lang.annotation.Annotation) Comparator(java.util.Comparator)

Example 53 with Annotation

use of java.lang.annotation.Annotation in project pinot by linkedin.

the class SwaggerResource method schemaForType.

private JSONObject schemaForType(Class<?> type) {
    try {
        JSONObject schema = new JSONObject();
        schema.put("type", "object");
        schema.put("title", type.getSimpleName());
        Example example = type.getAnnotation(Example.class);
        if (example != null) {
            schema.put("example", new JSONObject(example.value()));
        }
        for (Constructor<?> constructor : type.getConstructors()) {
            if (constructor.isAnnotationPresent(JsonCreator.class)) {
                JSONObject properties = new JSONObject();
                JSONArray required = new JSONArray();
                Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
                Class<?>[] parameterTypes = constructor.getParameterTypes();
                for (int i = 0; i < parameterTypes.length; i++) {
                    Class<?> parameterType = parameterTypes[i];
                    Annotation[] annotations = parameterAnnotations[i];
                    if (annotations.length != 0) {
                        for (Annotation annotation : annotations) {
                            if (annotation instanceof JsonProperty) {
                                JsonProperty jsonPropertyAnnotation = (JsonProperty) annotation;
                                JSONObject parameter = new JSONObject();
                                properties.put(jsonPropertyAnnotation.value(), parameter);
                                if (parameterType.equals(String.class)) {
                                    parameter.put("type", "string");
                                } else if (parameterType.equals(Boolean.class) || parameterType.equals(Boolean.TYPE)) {
                                    parameter.put("type", "boolean");
                                } else if (parameterType.equals(Integer.class) || parameterType.equals(Integer.TYPE)) {
                                    parameter.put("type", "integer");
                                } else if (parameterType.equals(Long.class) || parameterType.equals(Long.TYPE)) {
                                    // Long maps to integer type in http://swagger.io/specification/#dataTypeFormat
                                    parameter.put("type", "integer");
                                } else if (parameterType.equals(Float.class) || parameterType.equals(Float.TYPE)) {
                                    parameter.put("type", "boolean");
                                } else if (parameterType.equals(Double.class) || parameterType.equals(Double.TYPE)) {
                                    parameter.put("type", "double");
                                } else if (parameterType.equals(Byte.class) || parameterType.equals(Byte.TYPE)) {
                                    // Byte maps to string type in http://swagger.io/specification/#dataTypeFormat
                                    parameter.put("type", "string");
                                } else {
                                    parameter.put("type", "string");
                                }
                                if (jsonPropertyAnnotation.required()) {
                                    required.put(jsonPropertyAnnotation.value());
                                }
                            }
                        }
                    }
                }
                if (required.length() != 0) {
                    schema.put("required", required);
                }
                schema.put("properties", properties);
                break;
            }
        }
        return schema;
    } catch (Exception e) {
        return new JSONObject();
    }
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) JSONArray(org.json.JSONArray) Annotation(java.lang.annotation.Annotation) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 54 with Annotation

use of java.lang.annotation.Annotation in project mockito by mockito.

the class IndependentAnnotationEngine method process.

@Override
public void process(Class<?> clazz, Object testInstance) {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        boolean alreadyAssigned = false;
        for (Annotation annotation : field.getAnnotations()) {
            Object mock = createMockFor(annotation, field);
            if (mock != null) {
                throwIfAlreadyAssigned(field, alreadyAssigned);
                alreadyAssigned = true;
                try {
                    setField(testInstance, field, mock);
                } catch (Exception e) {
                    throw new MockitoException("Problems setting field " + field.getName() + " annotated with " + annotation, e);
                }
            }
        }
    }
}
Also used : FieldSetter.setField(org.mockito.internal.util.reflection.FieldSetter.setField) Field(java.lang.reflect.Field) MockitoException(org.mockito.exceptions.base.MockitoException) Annotation(java.lang.annotation.Annotation) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 55 with Annotation

use of java.lang.annotation.Annotation in project morphia by mongodb.

the class MappedClass method callLifecycleMethods.

/**
     * Call the lifecycle methods
     *
     * @param event  the lifecycle annotation
     * @param entity the entity to process
     * @param dbObj  the dbObject to use
     * @param mapper  the Mapper to use
     * @return dbObj
     */
@SuppressWarnings({ "WMI", "unchecked" })
public DBObject callLifecycleMethods(final Class<? extends Annotation> event, final Object entity, final DBObject dbObj, final Mapper mapper) {
    final List<ClassMethodPair> methodPairs = getLifecycleMethods((Class<Annotation>) event);
    DBObject retDbObj = dbObj;
    try {
        Object tempObj;
        if (methodPairs != null) {
            final HashMap<Class<?>, Object> toCall = new HashMap<Class<?>, Object>((int) (methodPairs.size() * 1.3));
            for (final ClassMethodPair cm : methodPairs) {
                toCall.put(cm.clazz, null);
            }
            for (final Class<?> c : toCall.keySet()) {
                if (c != null) {
                    toCall.put(c, getOrCreateInstance(c, mapper));
                }
            }
            for (final ClassMethodPair cm : methodPairs) {
                final Method method = cm.method;
                final Object inst = toCall.get(cm.clazz);
                method.setAccessible(true);
                if (LOG.isDebugEnabled()) {
                    LOG.debug(format("Calling lifecycle method(@%s %s) on %s", event.getSimpleName(), method, inst));
                }
                if (inst == null) {
                    if (method.getParameterTypes().length == 0) {
                        tempObj = method.invoke(entity);
                    } else {
                        tempObj = method.invoke(entity, retDbObj);
                    }
                } else if (method.getParameterTypes().length == 0) {
                    tempObj = method.invoke(inst);
                } else if (method.getParameterTypes().length == 1) {
                    tempObj = method.invoke(inst, entity);
                } else {
                    tempObj = method.invoke(inst, entity, retDbObj);
                }
                if (tempObj != null) {
                    retDbObj = (DBObject) tempObj;
                }
            }
        }
        callGlobalInterceptors(event, entity, dbObj, mapper);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
    return retDbObj;
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) DBObject(com.mongodb.DBObject) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBObject(com.mongodb.DBObject)

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