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);
}
}
}
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);
}
}
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();
}
}
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);
}
}
}
}
}
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;
}
Aggregations