List of usage examples for java.lang.reflect Method isAnnotationPresent
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
From source file:mil.army.usace.data.dataquery.rdbms.RdbmsDataQuery.java
private <T> List<T> commandToRecords(Class objClass, String command, HashMap<Method, String> fieldMapping, Object[] params) {/*from w ww . j a va2 s . c om*/ PreparedStatement st = null; ResultSet rs = null; try { ArrayList<T> records = new ArrayList(); st = conn.prepareStatement(command); setParams(st, params); rs = st.executeQuery(); while (rs.next()) { T newObj = (T) objClass.newInstance(); for (Method method : fieldMapping.keySet()) { try { Object val = converter.convertType(rs.getObject((String) fieldMapping.get(method)), method); if (val != null) method.invoke(newObj, val); } catch (Exception ex) { if (!method.isAnnotationPresent(Optional.class)) { throw new DataQueryException(command, "Unable to map method:" + method.getName(), ex); } } } records.add(newObj); } return records; } catch (Exception ex) { if (ex instanceof DataQueryException) throw (DataQueryException) ex; else throw new DataQueryException(command, null, ex); } finally { closeOnFinally(rs, st); } }
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param className/* w w w . ja v a 2s .co m*/ * @param tableList */ @SuppressWarnings("unchecked") protected void processCascade(final String className, final List<Table> tableList) { //System.out.println(className); try { Class<?> classObj = Class.forName(packageName + "." + className); //Table table = null; //String tableName = null; if (classObj.isAnnotationPresent(javax.persistence.Table.class)) { for (Method method : classObj.getMethods()) { String methodName = method.getName(); if (!methodName.startsWith("get") || method.isAnnotationPresent(javax.persistence.Transient.class)) { continue; } if (method.isAnnotationPresent(javax.persistence.ManyToOne.class)) { if (method.isAnnotationPresent(org.hibernate.annotations.Cascade.class)) { System.out.println("Missing Cascade[" + method.getName() + "]"); missing++; removeCascadeRule(classObj, method); } } } } } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DatamodelGenerator.class, ex); ex.printStackTrace(); } }
From source file:freemarker.ext.dump.BaseDumpDirective.java
private Map<String, Object> getObjectDump(TemplateHashModelEx model, Object object) throws TemplateModelException { Map<String, Object> map = new HashMap<String, Object>(); map.put(Key.TYPE.toString(), object.getClass().getName()); if (object instanceof java.lang.reflect.Method) return map; // Compile the collections of properties and methods available to the template SortedMap<String, Object> properties = new TreeMap<String, Object>(); SortedMap<String, Object> methods = new TreeMap<String, Object>(); // keys() gets only values visible to template based on the BeansWrapper used. // Note: if the BeansWrapper exposure level > BeansWrapper.EXPOSE_PROPERTIES_ONLY, // keys() returns both method and property name for any available method with no // parameters: e.g., both name and getName(). We are going to eliminate the latter. TemplateCollectionModel keys = model.keys(); TemplateModelIterator iModel = keys.iterator(); // Create a Set from keys so we can use the Set API. Set<String> keySet = new HashSet<String>(); while (iModel.hasNext()) { String key = iModel.next().toString(); keySet.add(key);/*from www . j a va2s . c om*/ } if (keySet.size() > 0) { Class<?> cls = object.getClass(); Method[] classMethods = cls.getMethods(); // Iterate through the methods rather than the keys, so that we can remove // some keys based on reflection on the methods. We also want to remove duplicates // like name/getName - we'll keep only the first form. for (Method method : classMethods) { if ("declaringClass".equals(method.getName())) continue; // Eliminate methods declared on Object // and other unusual places that can cause problems. Class<?> c = method.getDeclaringClass(); if (c == null || c.equals(java.lang.Object.class) || c.equals(java.lang.reflect.Constructor.class) || c.equals(java.lang.reflect.Field.class)) continue; if (c.getPackage().getName().startsWith("sun.") || c.getPackage().getName().startsWith("java.lang") || c.getPackage().getName().startsWith("java.security")) continue; // Eliminate deprecated methods if (method.isAnnotationPresent(Deprecated.class)) { continue; } // Include only methods included in keys(). This factors in visibility // defined by the model's BeansWrapper. String methodName = method.getName(); Matcher matcher = PROPERTY_NAME_PATTERN.matcher(methodName); // If the method name starts with "get" or "is", check if it's available // as a property if (matcher.find()) { String propertyName = getPropertyName(methodName); // The method is available as a property if (keySet.contains(propertyName)) { try { TemplateModel value = model.get(propertyName); properties.put(propertyName, getDump(value)); } catch (Throwable th) { log.error("problem dumping " + propertyName + " on " + object.getClass().getName() + " declared in " + c.getName(), th); } continue; } } // Else look for the entire methodName in the key set, to include // those that are exposed as methods rather than properties. if (keySet.contains(methodName)) { String methodDisplayName = getMethodDisplayName(method); // If no arguments, invoke the method to get the result if (methodDisplayName.endsWith("()")) { SimpleMethodModel methodModel = (SimpleMethodModel) model.get(methodName); try { Object result = methodModel.exec(null); ObjectWrapper wrapper = getWrapper(model); TemplateModel wrappedResult = wrapper.wrap(result); methods.put(methodDisplayName, getDump(wrappedResult)); } catch (Exception e) { log.error(e, e); } // Else display method name, parameter types, and return type } else { String returnTypeName = getReturnTypeName(method); Map<String, String> methodValue = new HashMap<String, String>(); if (!returnTypeName.equals("void")) { methodValue.put(Key.TYPE.toString(), returnTypeName); } methods.put(methodDisplayName, methodValue); } } } } Map<String, Object> objectValue = new HashMap<String, Object>(2); objectValue.put(Key.PROPERTIES.toString(), properties); objectValue.put(Key.METHODS.toString(), methods); map.put(Key.VALUE.toString(), objectValue); return map; }
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param className/*from w w w. j a v a2s. c o m*/ * @param tableList */ @SuppressWarnings("unchecked") protected void processCascadeAddCascade(final String className, final List<Table> tableList) { //System.out.println(className); try { Class<?> classObj = Class.forName(packageName + "." + className); Table table = null; String tableName = null; if (classObj.isAnnotationPresent(javax.persistence.Table.class)) { Vector<TableIndex> indexes = new Vector<TableIndex>(); javax.persistence.Table tableAnno = (javax.persistence.Table) classObj .getAnnotation(javax.persistence.Table.class); tableName = tableAnno.name(); org.hibernate.annotations.Table hiberTableAnno = (org.hibernate.annotations.Table) classObj .getAnnotation(org.hibernate.annotations.Table.class); if (hiberTableAnno != null) { for (Index index : hiberTableAnno.indexes()) { indexes.add(new TableIndex(index.name(), index.columnNames())); } } table = createTable(packageName + "." + className, tableName); table.setIndexes(indexes); tableList.add(table); } if (table != null) { for (Method method : classObj.getMethods()) { String methodName = method.getName(); if (!methodName.startsWith("get") || method.isAnnotationPresent(javax.persistence.Transient.class)) { continue; } //String thisSideName = getFieldNameFromMethod(method); if (method.isAnnotationPresent(javax.persistence.ManyToOne.class)) { if (!method.isAnnotationPresent(org.hibernate.annotations.Cascade.class)) { System.out.println("Missing Cascade[" + method.getName() + "]"); missing++; //addCascadeRule(classObj, method, " @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.MERGE, org.hibernate.annotations.CascadeType.LOCK })"); } } else if (method.isAnnotationPresent(javax.persistence.ManyToMany.class)) { if (!method.isAnnotationPresent(org.hibernate.annotations.Cascade.class)) { System.out.println("Missing Cascade[" + method.getName() + "]"); missing++; //addCascadeRule(classObj, method, " @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.MERGE, org.hibernate.annotations.CascadeType.LOCK })"); } } else if (method.isAnnotationPresent(javax.persistence.OneToMany.class)) { if (!method.isAnnotationPresent(org.hibernate.annotations.Cascade.class)) { System.out.println("Missing Cascade[" + method.getName() + "]"); missing++; addCascadeRule(classObj, method, " @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })"); } } else if (method.isAnnotationPresent(javax.persistence.OneToOne.class)) { if (!method.isAnnotationPresent(org.hibernate.annotations.Cascade.class)) { //System.out.println("Missing Cascade["+method.getName()+"]"); missing++; } } } // This updates each field as to whether it is an index table.updateIndexFields(); } } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DatamodelGenerator.class, ex); ex.printStackTrace(); } }
From source file:org.vaadin.testbenchsauce.BaseTestBenchTestCase.java
private void updateTestDescriptionWithRallyLinks(ITestResult testResult) { ITestNGMethod method = testResult.getMethod(); String extraDescriptionDetail = null; if (method instanceof BaseTestMethod) { BaseTestMethod baseTestMethod = (BaseTestMethod) method; extraDescriptionDetail = "Rally test case(s): "; Method baseTestMethodMethod = baseTestMethod.getMethod(); if (baseTestMethodMethod.isAnnotationPresent(RallyTestCase.class)) { String[] rallyTestCaseIds = baseTestMethodMethod.getAnnotation(RallyTestCase.class).value(); if (rallyTestCaseIds != null) { for (String rallyTestCaseId : rallyTestCaseIds) { if (rallyTestCaseId != null && !rallyTestCaseId.isEmpty()) { extraDescriptionDetail += "<a target='_blank' href=\"https://rally1.rallydev.com/#/search?keywords=" + rallyTestCaseId + "\">" + rallyTestCaseId + "</a> "; }/*from w w w.ja v a 2s .co m*/ } } if (extraDescriptionDetail != null) { String description = method.getDescription(); baseTestMethod.setDescription(description != null ? description + " - " + extraDescriptionDetail : extraDescriptionDetail); } } } }
From source file:org.vaadin.testbenchsauce.BaseTestBenchTestCase.java
private void updateRally(ITestResult testResult) { //check to see if the test method has the RallyTestCase annoation. Method method = testResult.getMethod().getConstructorOrMethod().getMethod(); if (method.isAnnotationPresent(RallyTestCase.class)) { String[] rallyTestCaseIds = method.getAnnotation(RallyTestCase.class).value(); if (rallyTestCaseIds != null) { for (String rallyTestCaseId : rallyTestCaseIds) { if (rallyTestCaseId != null && !rallyTestCaseId.isEmpty()) { try { updateRallyTestCase(rallyTestCaseId, testResult); } catch (URISyntaxException e) { printRallyServerError(e.getMessage()); } catch (IOException e) { printRallyServerError(e.getMessage()); }/*ww w.ja va 2 s . co m*/ } } } } }
From source file:org.projectforge.core.BaseDao.java
/** * Get all declared hibernate search fields. These fields are defined over annotations in the database object class. The names are the * property names or, if defined the name declared in the annotation of a field. <br/> * The user can search in these fields explicit by typing e. g. authors:beck (<field>:<searchString>) * @return//from w w w . j a va2 s . c o m */ public synchronized String[] getSearchFields() { if (searchFields != null) { return searchFields; } final Field[] fields = BeanHelper.getAllDeclaredFields(clazz); final Set<String> fieldNames = new TreeSet<String>(); for (final Field field : fields) { if (field.isAnnotationPresent(org.hibernate.search.annotations.Field.class) == true) { // @Field(index = Index.TOKENIZED), final org.hibernate.search.annotations.Field annotation = field .getAnnotation(org.hibernate.search.annotations.Field.class); fieldNames.add(getSearchName(field.getName(), annotation)); } else if (field.isAnnotationPresent(org.hibernate.search.annotations.Fields.class) == true) { // @Fields( { // @Field(index = Index.TOKENIZED), // @Field(name = "name_forsort", index = Index.UN_TOKENIZED) // } ) final org.hibernate.search.annotations.Fields annFields = field .getAnnotation(org.hibernate.search.annotations.Fields.class); for (final org.hibernate.search.annotations.Field annotation : annFields.value()) { fieldNames.add(getSearchName(field.getName(), annotation)); } } else if (field.isAnnotationPresent(Id.class) == true) { fieldNames.add(field.getName()); } else if (field.isAnnotationPresent(DocumentId.class) == true) { fieldNames.add(field.getName()); } } final Method[] methods = clazz.getMethods(); for (final Method method : methods) { if (method.isAnnotationPresent(org.hibernate.search.annotations.Field.class) == true) { final org.hibernate.search.annotations.Field annotation = method .getAnnotation(org.hibernate.search.annotations.Field.class); fieldNames.add(getSearchName(method.getName(), annotation)); } else if (method.isAnnotationPresent(DocumentId.class) == true) { final String prop = BeanHelper.determinePropertyName(method); fieldNames.add(prop); } } if (getAdditionalSearchFields() != null) { for (final String str : getAdditionalSearchFields()) { fieldNames.add(str); } } searchFields = new String[fieldNames.size()]; fieldNames.toArray(searchFields); log.info("Search fields for '" + clazz + "': " + ArrayUtils.toString(searchFields)); return searchFields; }
From source file:velo.ejb.impl.ConfBean.java
private Method getMethodAnnotatedWithUniqueColumnForSync(Class clazz) { for (Method currMethod : clazz.getMethods()) { if (currMethod.isAnnotationPresent(UniqueColumnForSync.class)) { return currMethod; }//from w w w . ja v a2 s . c om } return null; }
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param leftSide// w ww . j a va2 s . c om * @param rightSide * @param mappedByName * @return */ protected String getRightSideForOneToMany(final Class<?> leftSide, final Class<?> rightSide, final String mappedByName) { if (StringUtils.isEmpty(mappedByName)) { throw new RuntimeException("Couldn't find otherside method name missing for [" + rightSide.getSimpleName() + "] mappedByName[" + mappedByName + "]"); } for (Method method : rightSide.getMethods()) { String methodName = method.getName(); // Skip if it is a not a getter if (!methodName.startsWith("get")) { continue; } //System.out.println("Left Class["+leftSide.getSimpleName()+"] Right["+rightSide.getSimpleName()+"] Right Side Method ["+methodName+"] Ret["+method.getReturnType().getSimpleName()+"]"); // Skip if it is a not a ManyToOne if (!method.isAnnotationPresent(javax.persistence.ManyToOne.class)) { continue; } Class<?> retType = method.getReturnType(); boolean isSet = Collection.class.isAssignableFrom(retType); if (isSet) { Class<?> rt = getSetsClassType(rightSide, methodName); if (rt == null) { continue; // probably because of an interface } retType = rt; //System.out.println("Set["+(retType != null ? retType.getSimpleName() : "NULL")+"]"); } // Skip if the Return Types don't match if (leftSide != retType) { continue; } return getFieldNameFromMethod(method); } return null; }
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param leftSide/*from w ww.jav a 2 s. c o m*/ * @param rightSide * @param mappedByName * @return */ @SuppressWarnings("cast") protected String getRightSideForOneToOne(final Class<?> leftSide, final Class<?> rightSide, final String leftSideVarName, @SuppressWarnings("unused") final String mappedName, final boolean isMappedBy) { for (Method method : rightSide.getMethods()) { String methodName = method.getName(); // Skip if it is a not a getter if (!methodName.startsWith("get")) { continue; } //System.out.println("Left Class["+leftSide.getSimpleName()+"] Right["+rightSide.getSimpleName()+"] Right Side Method ["+methodName+"] Ret["+method.getReturnType().getSimpleName()+"]"); // Skip if it is a not a OneToOne if (!method.isAnnotationPresent(javax.persistence.OneToOne.class)) { continue; } Class<?> retType = method.getReturnType(); boolean isSet = Collection.class.isAssignableFrom(retType); if (isSet) { Class<?> rt = getSetsClassType(rightSide, methodName); if (rt == null) { continue; // probably because of an interface } retType = rt; //System.out.println("Set["+(retType != null ? retType.getSimpleName() : "NULL")+"]"); } String othersideName = ""; if (isMappedBy) { othersideName = getFieldNameFromMethod(method); } else { javax.persistence.OneToOne oneToOne = (javax.persistence.OneToOne) method .getAnnotation(javax.persistence.OneToOne.class); // Caller wasn't mappedBy so look for mapped By othersideName = oneToOne.mappedBy(); } if (StringUtils.isNotEmpty(othersideName) && leftSideVarName.equals(othersideName)) { return getFieldNameFromMethod(method); } } return null; }