List of usage examples for java.lang.reflect Method getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:com.ms.commons.summer.web.handler.ComponentMethodHandlerAdapter.java
private Method getInvokeMethod(Object handle, String methodName) { try {// ww w .j av a 2 s .c o m Method[] methods = handle.getClass().getMethods(); Method invokeMethod = null; for (Method method : methods) { if (method.getName().equals(methodName) && method.getAnnotation(ControllerAction.class) != null) { invokeMethod = method; } } if (invokeMethod == null) { return null; } return invokeMethod; } catch (SecurityException e) { return null; } }
From source file:com.cuubez.visualizer.resource.ResourceMetaDataScanner.java
private boolean scanHttpMethod(SubResource subResource, Method method) { GET get = method.getAnnotation(GET.class); if (get != null) { subResource.setHttpMethod(HttpMethod.GET); return true; }//from w w w. j a va 2 s . co m POST post = method.getAnnotation(POST.class); if (post != null) { subResource.setHttpMethod(HttpMethod.POST); return true; } PUT put = method.getAnnotation(PUT.class); if (put != null) { subResource.setHttpMethod(HttpMethod.PUT); return true; } DELETE delete = method.getAnnotation(DELETE.class); if (delete != null) { subResource.setHttpMethod(HttpMethod.DELETE); return true; } HEAD head = method.getAnnotation(HEAD.class); if (head != null) { subResource.setHttpMethod(HttpMethod.HEAD); return true; } OPTIONS options = method.getAnnotation(OPTIONS.class); if (options != null) { subResource.setHttpMethod(HttpMethod.OPTIONS); return true; } return false; }
From source file:com.evolveum.midpoint.repo.sql.query.definition.ClassDefinitionParser.java
private QName getJaxbName(Method method) { QName name = new QName(SchemaConstantsGenerated.NS_COMMON, getPropertyName(method.getName())); if (method.isAnnotationPresent(JaxbName.class)) { JaxbName jaxbName = method.getAnnotation(JaxbName.class); name = new QName(jaxbName.namespace(), jaxbName.localPart()); }//from w ww . j ava2 s. com return name; }
From source file:com.nginious.http.serialize.JsonDeserializerCreator.java
/** * Creates a JSON deserializer for the specified bean class unless a deserializer has already * been created. Created deserializers are cached and returned on subsequent calls to this method. * //from w w w . j a va 2 s. c om * @param <T> class type for bean * @param beanClazz bean class for which a deserializer should be created * @return the created deserializer * @throws SerializerFactoryException if unable to create deserializer or class is not a bean */ @SuppressWarnings("unchecked") protected <T> JsonDeserializer<T> create(Class<T> beanClazz) throws SerializerFactoryException { JsonDeserializer<T> deserializer = (JsonDeserializer<T>) deserializers.get(beanClazz); if (deserializer != null) { return deserializer; } try { synchronized (this) { deserializer = (JsonDeserializer<T>) deserializers.get(beanClazz); if (deserializer != null) { return deserializer; } checkDeserializability(beanClazz, "json"); String intBeanClazzName = Serialization.createInternalClassName(beanClazz); Method[] methods = beanClazz.getMethods(); String intDeserializerClazzName = new StringBuffer(intBeanClazzName).append("JsonDeserializer") .toString(); // Create class ClassWriter writer = new ClassWriter(0); String signature = Serialization .createClassSignature("com/nginious/http/serialize/JsonDeserializer", intBeanClazzName); writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intDeserializerClazzName, signature, "com/nginious/http/serialize/JsonDeserializer", null); // Create constructor Serialization.createConstructor(writer, "com/nginious/http/serialize/JsonDeserializer"); // Create deserialize method MethodVisitor visitor = createDeserializeMethod(writer, intBeanClazzName); for (Method method : methods) { Serializable info = method.getAnnotation(Serializable.class); boolean canDeserialize = info == null || (info != null && info.deserialize() && info.types().indexOf("json") > -1); if (canDeserialize && method.getName().startsWith("set") && method.getReturnType().equals(void.class) && method.getParameterTypes().length == 1) { Class<?>[] parameterTypes = method.getParameterTypes(); Class<?> parameterType = parameterTypes[0]; if (parameterType.isArray()) { Class<?> arrayType = parameterType.getComponentType(); if (arrayType.equals(boolean.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeBooleanArray", "[Z", "[Z", intBeanClazzName, method.getName()); } else if (arrayType.equals(double.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDoubleArray", "[D", "[D", intBeanClazzName, method.getName()); } else if (arrayType.equals(float.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeFloatArray", "[F", "[F", intBeanClazzName, method.getName()); } else if (arrayType.equals(int.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeIntArray", "[I", "[I", intBeanClazzName, method.getName()); } else if (arrayType.equals(long.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeLongArray", "[J", "[J", intBeanClazzName, method.getName()); } else if (arrayType.equals(short.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeShortArray", "[S", "[S", intBeanClazzName, method.getName()); } else if (arrayType.equals(String.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeStringArray", "[Ljava/lang/String;", "[Ljava/lang/String;", intBeanClazzName, method.getName()); } } else if (parameterType.isPrimitive()) { if (parameterType.equals(boolean.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeBoolean", "Z", "Z", intBeanClazzName, method.getName()); } else if (parameterType.equals(double.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDouble", "D", "D", intBeanClazzName, method.getName()); } else if (parameterType.equals(float.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeFloat", "F", "F", intBeanClazzName, method.getName()); } else if (parameterType.equals(int.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeInt", "I", "I", intBeanClazzName, method.getName()); } else if (parameterType.equals(long.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeLong", "J", "J", intBeanClazzName, method.getName()); } else if (parameterType.equals(short.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeShort", "S", "S", intBeanClazzName, method.getName()); } } else if (parameterType.equals(Calendar.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeCalendar", "Ljava/util/Calendar;", "Ljava/util/Calendar;", intBeanClazzName, method.getName()); } else if (parameterType.equals(Date.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDate", "Ljava/util/Date;", "Ljava/util/Date;", intBeanClazzName, method.getName()); } else if (parameterType.equals(String.class)) { createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeString", "Ljava/lang/String;", "Ljava/lang/String;", intBeanClazzName, method.getName()); } } } visitor.visitVarInsn(Opcodes.ALOAD, 3); visitor.visitInsn(Opcodes.ARETURN); visitor.visitMaxs(5, 4); visitor.visitEnd(); writer.visitEnd(); byte[] clazzBytes = writer.toByteArray(); ClassLoader controllerLoader = null; if (classLoader.hasLoaded(beanClazz)) { controllerLoader = beanClazz.getClassLoader(); } else { controllerLoader = this.classLoader; } Class<?> clazz = Serialization.loadClass(controllerLoader, intDeserializerClazzName.replace('/', '.'), clazzBytes); deserializer = (JsonDeserializer<T>) clazz.newInstance(); deserializers.put(beanClazz, deserializer); return deserializer; } } catch (IllegalAccessException e) { throw new SerializerFactoryException(e); } catch (InstantiationException e) { throw new SerializerFactoryException(e); } }
From source file:com.fun.rrs.common.excel.ExportExcel.java
/** * //from w w w . ja v a2 s . c o m * @param title ? * @param cls annotation.ExportField? * @param type 1:?2? * @param groups */ public ExportExcel(String title, Class<?> cls, int type, int... groups) { // Get annotation field Field[] fs = cls.getDeclaredFields(); for (Field f : fs) { ExcelField ef = f.getAnnotation(ExcelField.class); if (ef != null && (ef.type() == 0 || ef.type() == type)) { if (groups != null && groups.length > 0) { boolean inGroup = false; for (int g : groups) { if (inGroup) { break; } for (int efg : ef.groups()) { if (g == efg) { inGroup = true; annotationList.add(new Object[] { ef, f }); break; } } } } else { annotationList.add(new Object[] { ef, f }); } } } // Get annotation method Method[] ms = cls.getDeclaredMethods(); for (Method m : ms) { ExcelField ef = m.getAnnotation(ExcelField.class); if (ef != null && (ef.type() == 0 || ef.type() == type)) { if (groups != null && groups.length > 0) { boolean inGroup = false; for (int g : groups) { if (inGroup) { break; } for (int efg : ef.groups()) { if (g == efg) { inGroup = true; annotationList.add(new Object[] { ef, m }); break; } } } } else { annotationList.add(new Object[] { ef, m }); } } } // Field sorting Collections.sort(annotationList, new Comparator<Object[]>() { public int compare(Object[] o1, Object[] o2) { return new Integer(((ExcelField) o1[0]).sort()).compareTo(new Integer(((ExcelField) o2[0]).sort())); }; }); // Initialize List<String> headerList = new ArrayList<String>(); for (Object[] os : annotationList) { String t = ((ExcelField) os[0]).title(); // if (type == 1) { String[] ss = StringUtils.split(t, "**", 2); if (ss.length == 2) { t = ss[0]; } } headerList.add(t); } initialize(title, headerList); }
From source file:com.msopentech.odatajclient.proxy.api.impl.EntitySetInvocationHandler.java
private LinkedHashMap<String, Object> getCompoundKey(final Object key) { final Set<CompoundKeyElementWrapper> elements = new TreeSet<CompoundKeyElementWrapper>(); for (Method method : key.getClass().getMethods()) { final Annotation annotation = method.getAnnotation(CompoundKeyElement.class); if (annotation instanceof CompoundKeyElement) { elements.add(new CompoundKeyElementWrapper(((CompoundKeyElement) annotation).name(), method, ((CompoundKeyElement) annotation).position())); }/* ww w. j a v a 2s .c o m*/ } final LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>(); for (CompoundKeyElementWrapper element : elements) { try { map.put(element.getName(), element.getMethod().invoke(key)); } catch (Exception e) { LOG.warn("Error retrieving compound key element '{}' value", element.getName(), e); } } return map; }
From source file:org.slc.sli.dashboard.manager.component.impl.CustomizationAssemblyFactoryImpl.java
private final void findEntityReferencesForType(Map<String, InvokableSet> entityReferenceToManagerMethodMap, Class<?> type, Object instance) { Manager.EntityMapping entityMapping; for (Method m : type.getDeclaredMethods()) { entityMapping = m.getAnnotation(Manager.EntityMapping.class); if (entityMapping != null) { if (entityReferenceToManagerMethodMap.containsKey(entityMapping.value())) { throw new DashboardException( "Duplicate entity mapping references found for " + entityMapping.value() + ". Fix!!!"); }/* w w w .ja v a 2 s .c o m*/ if (!Arrays.equals(ENTITY_REFERENCE_METHOD_EXPECTED_SIGNATURE, m.getParameterTypes())) { throw new DashboardException("Wrong signature for the method for " + entityMapping.value() + ". Expected is " + Arrays.asList(ENTITY_REFERENCE_METHOD_EXPECTED_SIGNATURE) + "!!!"); } entityReferenceToManagerMethodMap.put(entityMapping.value(), new InvokableSet(instance, m)); } } }
From source file:com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl.java
/** * @inheritDoc//www . j ava 2 s . c o m */ public <T> String export(String template, T fixedFormatRecord) { StringBuffer result = new StringBuffer(template); Record record = getAndAssertRecordAnnotation(fixedFormatRecord.getClass()); Class fixedFormatRecordClass = fixedFormatRecord.getClass(); HashMap<Integer, String> foundData = new HashMap<Integer, String>(); // hashmap containing offset and data to write Method[] allMethods = fixedFormatRecordClass.getMethods(); for (Method method : allMethods) { Field fieldAnnotation = method.getAnnotation(Field.class); Fields fieldsAnnotation = method.getAnnotation(Fields.class); if (fieldAnnotation != null) { String exportedData = exportDataAccordingFieldAnnotation(fixedFormatRecord, method, fieldAnnotation); foundData.put(fieldAnnotation.offset(), exportedData); } else if (fieldsAnnotation != null) { Field[] fields = fieldsAnnotation.value(); for (Field field : fields) { String exportedData = exportDataAccordingFieldAnnotation(fixedFormatRecord, method, field); foundData.put(field.offset(), exportedData); } } } Set<Integer> sortedoffsets = foundData.keySet(); for (Integer offset : sortedoffsets) { String data = foundData.get(offset); appendData(result, record.paddingChar(), offset, data); } if (record.length() != -1) { //pad with paddingchar while (result.length() < record.length()) { result.append(record.paddingChar()); } } return result.toString(); }
From source file:com.cuubez.visualizer.resource.ResourceMetaDataScanner.java
private boolean scanHttpCodes(SubResource subResource, Method method) { com.cuubez.visualizer.annotation.HttpCode httpCode = method .getAnnotation(com.cuubez.visualizer.annotation.HttpCode.class); if (httpCode != null) { subResource.setHttpCodeMetaDataList(CuubezUtil.generateHttpCodeMetaData(httpCode.value())); return true; }//from w w w .j av a2s .c o m return false; }
From source file:com.ryantenney.metrics.spring.GaugeAnnotationBeanPostProcessor.java
@Override public Object postProcessAfterInitialization(final Object bean, String beanName) { final Class<?> targetClass = AopUtils.getTargetClass(bean); ReflectionUtils.doWithFields(targetClass, new FieldCallback() { @Override/*from w w w .j a va 2s . c o m*/ public void doWith(final Field field) throws IllegalAccessException { ReflectionUtils.makeAccessible(field); final Gauge annotation = field.getAnnotation(Gauge.class); final String metricName = Util.forGauge(targetClass, field, annotation); metrics.register(metricName, new com.codahale.metrics.Gauge<Object>() { @Override public Object getValue() { Object value = ReflectionUtils.getField(field, bean); if (value instanceof com.codahale.metrics.Gauge) { value = ((com.codahale.metrics.Gauge<?>) value).getValue(); } return value; } }); LOG.debug("Created gauge {} for field {}.{}", metricName, targetClass.getCanonicalName(), field.getName()); } }, FILTER); ReflectionUtils.doWithMethods(targetClass, new MethodCallback() { @Override public void doWith(final Method method) throws IllegalAccessException { if (method.getParameterTypes().length > 0) { throw new IllegalStateException( "Method " + method.getName() + " is annotated with @Gauge but requires parameters."); } final Gauge annotation = method.getAnnotation(Gauge.class); final String metricName = Util.forGauge(targetClass, method, annotation); metrics.register(metricName, new com.codahale.metrics.Gauge<Object>() { @Override public Object getValue() { return ReflectionUtils.invokeMethod(method, bean); } }); LOG.debug("Created gauge {} for method {}.{}", metricName, targetClass.getCanonicalName(), method.getName()); } }, FILTER); return bean; }