Search in sources :

Example 1 with Annotation

use of java.lang.annotation.Annotation in project elasticsearch by elastic.

the class InjectionPoint method forMember.

private List<Dependency<?>> forMember(Member member, TypeLiteral<?> type, Annotation[][] parameterAnnotations) {
    Errors errors = new Errors(member);
    Iterator<Annotation[]> annotationsIterator = Arrays.asList(parameterAnnotations).iterator();
    List<Dependency<?>> dependencies = new ArrayList<>();
    int index = 0;
    for (TypeLiteral<?> parameterType : type.getParameterTypes(member)) {
        try {
            Annotation[] paramAnnotations = annotationsIterator.next();
            Key<?> key = Annotations.getKey(parameterType, member, paramAnnotations, errors);
            dependencies.add(newDependency(key, Nullability.allowsNull(paramAnnotations), index));
            index++;
        } catch (ErrorsException e) {
            errors.merge(e.getErrors());
        }
    }
    errors.throwConfigurationExceptionIfErrorsExist();
    return Collections.unmodifiableList(dependencies);
}
Also used : Errors(org.elasticsearch.common.inject.internal.Errors) ArrayList(java.util.ArrayList) ErrorsException(org.elasticsearch.common.inject.internal.ErrorsException) Annotation(java.lang.annotation.Annotation)

Example 2 with Annotation

use of java.lang.annotation.Annotation in project cucumber-jvm by cucumber.

the class ParameterInfo method fromMethod.

public static List<ParameterInfo> fromMethod(Method method) {
    List<ParameterInfo> result = new ArrayList<ParameterInfo>();
    Type[] genericParameterTypes = method.getGenericParameterTypes();
    Annotation[][] annotations = method.getParameterAnnotations();
    for (int i = 0; i < genericParameterTypes.length; i++) {
        String format = null;
        String delimiter = DEFAULT_DELIMITER;
        boolean transposed = false;
        Transformer<?> transformer = null;
        for (Annotation annotation : annotations[i]) {
            if (annotation instanceof Format) {
                format = ((Format) annotation).value();
            } else if (isAnnotatedWith(annotation, Format.class)) {
                format = getAnnotationForAnnotation(annotation, Format.class).value();
            }
            if (annotation instanceof Delimiter) {
                delimiter = ((Delimiter) annotation).value();
            } else if (isAnnotatedWith(annotation, Delimiter.class)) {
                delimiter = getAnnotationForAnnotation(annotation, Delimiter.class).value();
            }
            if (annotation instanceof Transpose) {
                transposed = ((Transpose) annotation).value();
            }
            if (annotation instanceof Transform) {
                transformer = getTransformer(annotation);
            } else if (isAnnotatedWith(annotation, Transform.class)) {
                transformer = getTransformer(getAnnotationForAnnotation(annotation, Transform.class));
            }
        }
        result.add(new ParameterInfo(genericParameterTypes[i], format, delimiter, transposed, transformer));
    }
    return result;
}
Also used : Delimiter(cucumber.api.Delimiter) ArrayList(java.util.ArrayList) Transpose(cucumber.api.Transpose) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Format(cucumber.api.Format) Transform(cucumber.api.Transform)

Example 3 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 4 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 5 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)1784 Method (java.lang.reflect.Method)412 ArrayList (java.util.ArrayList)245 Field (java.lang.reflect.Field)172 Test (org.junit.Test)164 Type (java.lang.reflect.Type)149 HashMap (java.util.HashMap)146 HashSet (java.util.HashSet)129 Map (java.util.Map)95 List (java.util.List)92 IOException (java.io.IOException)71 Set (java.util.Set)71 ParameterizedType (java.lang.reflect.ParameterizedType)62 InvocationTargetException (java.lang.reflect.InvocationTargetException)53 Collection (java.util.Collection)52 LinkedHashMap (java.util.LinkedHashMap)36 Collectors (java.util.stream.Collectors)36 Constructor (java.lang.reflect.Constructor)33 LinkedHashSet (java.util.LinkedHashSet)32 Optional (java.util.Optional)32