List of usage examples for java.lang.reflect Field isAnnotationPresent
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
From source file:net.ceos.project.poi.annotated.annotation.XlsFreeElementTest.java
/** * Test initialization of the decorator attribute with specific value. */// w ww. java 2 s .c om @Test public void checkDecoratorAttribute() { Class<XMenFactory.ProfessorX> oC = XMenFactory.ProfessorX.class; List<Field> fL = Arrays.asList(oC.getDeclaredFields()); for (Field f : fL) { // Process @XlsFreeElement if (f.isAnnotationPresent(XlsFreeElement.class)) { XlsFreeElement xlsFreeElement = (XlsFreeElement) f.getAnnotation(XlsFreeElement.class); if (f.getName().equals("integerFreeAttribute") && StringUtils.isNotBlank(xlsFreeElement.decorator())) { assertEquals(xlsFreeElement.decorator(), "myDecorator"); } } } }
From source file:net.ceos.project.poi.annotated.annotation.XlsFreeElementTest.java
/** * Test initialization of the formatMask attribute with specific value. *///from w w w . j a v a 2s .co m @Test public void checkFormatMaskAttribute() { Class<XMenFactory.ProfessorX> oC = XMenFactory.ProfessorX.class; List<Field> fL = Arrays.asList(oC.getDeclaredFields()); for (Field f : fL) { // Process @XlsFreeElement if (f.isAnnotationPresent(XlsFreeElement.class)) { XlsFreeElement xlsFreeElement = (XlsFreeElement) f.getAnnotation(XlsFreeElement.class); if (f.getName().equals("integerFreeAttribute") && StringUtils.isNotBlank(xlsFreeElement.formatMask())) { assertEquals(xlsFreeElement.formatMask(), "0.0"); } } } }
From source file:net.ceos.project.poi.annotated.annotation.XlsFreeElementTest.java
/** * Test initialization of the transformMask attribute with specific value. */// www.j a v a 2s.c o m @Test public void checkTransformMaskAttribute() { Class<XMenFactory.ProfessorX> oC = XMenFactory.ProfessorX.class; List<Field> fL = Arrays.asList(oC.getDeclaredFields()); for (Field f : fL) { // Process @XlsFreeElement if (f.isAnnotationPresent(XlsFreeElement.class)) { XlsFreeElement xlsFreeElement = (XlsFreeElement) f.getAnnotation(XlsFreeElement.class); if (f.getName().equals("doubleFreeAttribute") && StringUtils.isNotBlank(xlsFreeElement.transformMask())) { assertEquals(xlsFreeElement.transformMask(), "0.00"); } } } }
From source file:jofc2.OFC.java
/** * ??/*from www . j a v a 2 s . co m*/ * @param c */ private void doAlias(Class<?> c) { /** * */ if (c.isAnnotationPresent(Alias.class)) { converter.alias(c.getAnnotation(Alias.class).value(), c); } /** * ?? */ for (Field f : c.getDeclaredFields()) { if (f.isAnnotationPresent(Alias.class)) { if (f.getAnnotation(Alias.class).value().equals("")) { System.out.println(f.getName()); } converter.aliasField(f.getAnnotation(Alias.class).value(), c, f.getName()); } } }
From source file:edu.mayo.cts2.framework.webapp.service.AbstractServiceAwareBean.java
/** * Load services.//w w w . j a v a 2 s . co m */ protected void loadServices() { for (Field field : this.getClass().getDeclaredFields()) { if (field.isAnnotationPresent(Cts2Service.class)) { @SuppressWarnings("unchecked") final Class<? extends Cts2Profile> clazz = (Class<? extends Cts2Profile>) field.getType(); ProxyFactory factory = new ProxyFactory(); Cts2Profile service = (Cts2Profile) factory.createInvokerProxy(new Invoker() { @Override public Object invoke(Object o, Method method, Object[] arguments) throws Throwable { ServiceProvider retrievedServiceProvider = serviceProviderFactory.getServiceProvider(); Cts2Profile retrievedService; if (retrievedServiceProvider == null) { throw new UnsupportedOperationException("This service is not implemented."); } else { retrievedService = retrievedServiceProvider.getService(clazz); if (retrievedService == null) { throw new UnsupportedOperationException("This service is not implemented."); } } try { return method.invoke(retrievedService, arguments); } catch (InvocationTargetException e) { throw e.getCause(); } } }, new Class<?>[] { clazz }); if (service == null) { service = proxyNullService(clazz); } field.setAccessible(true); try { field.set(this, service); } catch (Exception e) { throw new IllegalStateException(e); } log.info("Setting service: " + field.getType() + " on: " + this.getClass().getName()); } } }
From source file:com.higgses.griffin.annotation.app.GinInjector.java
/** * ??/*from w w w .java 2s. c o m*/ * * @param activity */ public void injectResource(Object activity) { Field[] fields = activity.getClass().getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { if (field.isAnnotationPresent(GinInjectResource.class)) { injectResource(activity, field); } } } }
From source file:de.hackerspacebremen.format.JSONFormatter.java
@Override public String format(final T entity, final String... levels) throws FormatException { final JSONObject json = new JSONObject(); boolean allAttributes = (levels == null || levels.length == 0); final List<String> levelList; if (levels == null || levels.length == 0) { levelList = new ArrayList<String>(); } else {//from w ww. j av a 2 s . c o m levelList = Arrays.asList(levels); } if (entity.getClass().isAnnotationPresent(Entity.class)) { final Field[] fields = entity.getClass().getDeclaredFields(); for (final Field field : fields) { if (field.isAnnotationPresent(FormatPart.class) && (allAttributes || levelList.contains(field.getAnnotation(FormatPart.class).level()))) { try { final Method method = entity.getClass() .getMethod(this.getGetter(field.getName(), field.getType()), (Class<?>[]) null); final Object methodResult = method.invoke(entity); if (methodResult != null) { final String type = field.getType().toString(); if (type.equals("class com.google.appengine.api.datastore.Key")) { json.put(field.getAnnotation(FormatPart.class).key(), KeyFactory.keyToString((Key) methodResult)); } else if (type.equals("class com.google.appengine.api.datastore.Text")) { json.put(field.getAnnotation(FormatPart.class).key(), ((Text) methodResult).getValue()); } else { json.put(field.getAnnotation(FormatPart.class).key(), methodResult.toString()); } } } catch (IllegalAccessException e) { logger.warning("IllegalAccessException occured: " + e.getMessage()); throw new FormatException(); } catch (NoSuchMethodException e) { logger.warning("NoSuchMethodException occured: " + e.getMessage()); throw new FormatException(); } catch (SecurityException e) { logger.warning("SecurityException occured: " + e.getMessage()); throw new FormatException(); } catch (IllegalArgumentException e) { logger.warning("IllegalArgumentException occured: " + e.getMessage()); throw new FormatException(); } catch (InvocationTargetException e) { logger.warning("InvocationTargetException occured: " + e.getMessage()); throw new FormatException(); } catch (JSONException e) { logger.warning("JSONException occured: " + e.getMessage()); throw new FormatException(); } } } } return json.toString(); }
From source file:es.caib.sgtsic.ejb3.AbstractFacade.java
public boolean borrable(Object id) { T item = this.find(id); log.debug("Entramos a borrable " + item); if (item == null) { return false; }/*from ww w . jav a2s . com*/ log.debug("Entramos a borrable con no false " + item); for (Field f : entityClass.getDeclaredFields()) { boolean hasToManyAnnotations = (f.isAnnotationPresent(OneToMany.class)) || (f.isAnnotationPresent(ManyToMany.class)); if (hasToManyAnnotations) { Type type = f.getGenericType(); ParameterizedType pt = (ParameterizedType) type; List<Type> arguments = Arrays.asList(pt.getActualTypeArguments()); Class childEntityClass = null; for (Type argtype : arguments) { childEntityClass = (Class) argtype; break; } if (childEntityClass == null) continue; if (this.childrenCount(id, childEntityClass, entityClass) > 0) { log.debug("Cuenta positiva"); return false; } } } log.debug("Cuenta 0"); return true; }
From source file:com.haulmont.chile.jpa.loader.JPAAnnotationsLoader.java
@Override protected Range.Cardinality getCardinality(Field field) { if (field.isAnnotationPresent(Column.class)) { return Range.Cardinality.NONE; } else if (field.isAnnotationPresent(OneToOne.class)) { return Range.Cardinality.ONE_TO_ONE; } else if (field.isAnnotationPresent(OneToMany.class)) { return Range.Cardinality.ONE_TO_MANY; } else if (field.isAnnotationPresent(ManyToOne.class)) { return Range.Cardinality.MANY_TO_ONE; } else if (field.isAnnotationPresent(ManyToMany.class)) { return Range.Cardinality.MANY_TO_MANY; } else if (field.isAnnotationPresent(Embedded.class)) { return Range.Cardinality.ONE_TO_ONE; } else {// w w w .ja v a 2 s .c o m return super.getCardinality(field); } }
From source file:de.hackerspacebremen.format.JSONFormatter.java
@Override public T reformat(final String object, final Class<T> entityClass) throws FormatException { T result = null;/*from w w w . j a va 2 s.co m*/ final Map<String, Field> fieldMap = new HashMap<String, Field>(); if (entityClass.isAnnotationPresent(Entity.class)) { final Field[] fields = entityClass.getDeclaredFields(); for (final Field field : fields) { if (field.isAnnotationPresent(FormatPart.class)) { fieldMap.put(field.getAnnotation(FormatPart.class).key(), field); } } try { final JSONObject json = new JSONObject(object); result = entityClass.newInstance(); for (final String key : fieldMap.keySet()) { if (json.has(key)) { final Field field = fieldMap.get(key); final Method method = entityClass.getMethod(this.getSetter(field.getName()), new Class<?>[] { field.getType() }); final String type = field.getType().toString(); if (type.equals("class com.google.appengine.api.datastore.Key")) { method.invoke(result, KeyFactory.stringToKey(json.getString(key))); } else if (type.equals("class com.google.appengine.api.datastore.Text")) { method.invoke(result, new Text(json.getString(key))); } else if (type.equals("boolean")) { method.invoke(result, json.getBoolean(key)); } else if (type.equals("long")) { method.invoke(result, json.getLong(key)); } else if (type.equals("int")) { method.invoke(result, json.getInt(key)); } else { method.invoke(result, json.get(key)); } } } } catch (JSONException e) { logger.warning("JSONException occured: " + e.getMessage()); throw new FormatException(); } catch (NoSuchMethodException e) { logger.warning("NoSuchMethodException occured: " + e.getMessage()); throw new FormatException(); } catch (SecurityException e) { logger.warning("SecurityException occured: " + e.getMessage()); throw new FormatException(); } catch (IllegalAccessException e) { logger.warning("IllegalAccessException occured: " + e.getMessage()); throw new FormatException(); } catch (IllegalArgumentException e) { logger.warning("IllegalArgumentException occured: " + e.getMessage()); throw new FormatException(); } catch (InvocationTargetException e) { logger.warning("InvocationTargetException occured: " + e.getMessage()); throw new FormatException(); } catch (InstantiationException e) { logger.warning("InstantiationException occured: " + e.getMessage()); throw new FormatException(); } } return result; }