List of usage examples for java.lang.reflect Method isAnnotationPresent
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param leftSide/*from ww w.j av a 2 s . c o m*/ * @param rightSide * @param leftSideVarName * @param isMappedBy * @return */ @SuppressWarnings("cast") protected String getRightSideForManyToMany(final Class<?> leftSide, final Class<?> rightSide, final String leftSideVarName) { 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("getRightSideForManyToMany 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.ManyToMany.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")+"]"); } if (leftSide == retType) { javax.persistence.ManyToMany manyToMany = (javax.persistence.ManyToMany) method .getAnnotation(javax.persistence.ManyToMany.class); // Caller wasn't mappedBy so look for mapped By String othersideName = manyToMany.mappedBy(); if (StringUtils.isNotEmpty(othersideName) && leftSideVarName.equals(othersideName)) { return getFieldNameFromMethod(method); } } } return null; }
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param leftSide/* w w w . j av a 2s.co m*/ * @param rightSide * @param leftSideVarName * @return */ @SuppressWarnings("cast") protected String getRightSideForManyToOne(final Class<?> leftSide, final Class<?> rightSide, final String leftSideVarName) { 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("getRightSideForManyToOne 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.OneToMany.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")+"]"); } if (leftSide != retType) { continue; } javax.persistence.OneToMany oneToMany = (javax.persistence.OneToMany) method .getAnnotation(javax.persistence.OneToMany.class); String othersideName = oneToMany.mappedBy(); if (StringUtils.isNotEmpty(othersideName)) // This should never be null { //System.out.println("\nXXX["+othersideName+"]["+retType+"]"); //System.out.println("othersideName["+othersideName+"] leftSideVarName["+leftSideVarName+"]"); //System.out.println("["+leftSide.getSimpleName()+"]["+retType.getSimpleName()+"]"); if (othersideName.equals(leftSideVarName)) { return getFieldNameFromMethod(method); } } } return null; }
From source file:velo.ejb.impl.ConfBean.java
@Deprecated protected Method findIdMethod(Class clazz) { for (Class currClazz = clazz; !currClazz.equals(Object.class); currClazz = currClazz.getSuperclass()) { // Iterate over the fields and find the Id field for (Method m : currClazz.getDeclaredMethods()) { if (!(m.isAccessible())) m.setAccessible(true);/*from w w w . ja v a2 s. co m*/ if (m.isAnnotationPresent(Id.class)) { return m; } } } return null; }
From source file:com.jsmartframework.web.manager.BeanHandler.java
void executePreDestroy(Object bean) { for (Method method : HELPER.getPreDestroyMethods(bean.getClass())) { if (method.isAnnotationPresent(PreDestroy.class)) { try { method.invoke(bean, null); } catch (Exception ex) { ex.printStackTrace();// w w w. j av a 2s. com } return; } } }
From source file:com.jsmartframework.web.manager.BeanHandler.java
void executePostConstruct(Object bean) { for (Method method : HELPER.getPostConstructMethods(bean.getClass())) { if (method.isAnnotationPresent(PostConstruct.class)) { try { method.invoke(bean, null); } catch (Exception ex) { ex.printStackTrace();//from ww w. jav a 2s. co m } return; } } }
From source file:adalid.core.Project.java
/** * * @param clazz/*w w w . j a v a 2s.com*/ */ public void attachAddAttributesMethods(Class<?> clazz) { logger.debug(signature("attachAddAttributesMethods", clazz)); String name; boolean addAttributesMethod; int modifiers; Class<?> returnType; Class<?>[] parameterTypes; Method[] methods = clazz.getDeclaredMethods(); List<Method> list = Arrays.asList(methods); Comparator<Method> comparator = new ByMethodSequence(); ColUtils.sort(list, comparator); for (Method method : list) { name = method.getName(); addAttributesMethod = method.isAnnotationPresent(AddAttributesMethod.class); modifiers = method.getModifiers(); returnType = method.getReturnType(); parameterTypes = method.getParameterTypes(); if (addAttributesMethod && Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && void.class.equals(returnType) && parameterTypes.length == 1 && Artifact.class.isAssignableFrom(parameterTypes[0])) { logger.debug(signature(clazz.getSimpleName() + "." + name, parameterTypes[0])); _addAttributesMethods.add(method); } } }
From source file:edu.ku.brc.specify.dbsupport.SpecifyDeleteHelper.java
/** * @param parent/*from w ww .ja v a2s.c o m*/ * @param cls * @param sqlStr * @param delSqlStr * @param level */ protected StackItem getSubTables(final StackItem parent, final Class<?> cls, final int id, final String sqlStr, final String delSqlStr, final int level, final Hashtable<String, Boolean> inUseHashArg, final boolean checkIfIsShared/*, final int excludeId /*check if is required by anything other than this id*/) { if (classHash.contains(cls)) { return null; } classHash.add(cls); if (debug) { printLevel(level); System.out.println(cls.getSimpleName()); } DBTableInfo tblInfo = tblMgr.getByShortClassName(cls.getSimpleName()); StackItem child = parent.push(tblInfo, sqlStr, delSqlStr); Hashtable<String, Boolean> inUseHash = inUseHashArg == null && level == 1 ? new Hashtable<String, Boolean>() : inUseHashArg; for (Method method : cls.getMethods()) { String methodName = method.getName(); //System.out.println(methodName); // Skip if it is a not a getter if (!methodName.startsWith("get")) { continue; } // Skip if it is a not a ManyToOne if (method.isAnnotationPresent(javax.persistence.Transient.class)) { continue; } if (methodName.endsWith("TreeDef")) { if (doTrees) { String className = methodName.substring(3, methodName.length() - 7); String tableNameTD = className.toLowerCase() + "treedef"; String primaryKeyTD = className + "TreeDefID"; String itemTableNameTDI = className.toLowerCase() + "treedefitem"; String sql; String delSql; try { sql = "SELECT " + primaryKeyTD + " FROM " + tblInfo.getName() + " WHERE " + tblInfo.getIdColumnName() + " = " + id; if (debugUpdate) System.err.println(sql); Vector<Integer> ids = getIds(sql, level); if (ids != null && ids.size() > 0) { Class<?> treeClass = null; try { treeClass = Class.forName("edu.ku.brc.specify.datamodel." + className); } catch (Exception ex) { ex.printStackTrace(); edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance() .capture(SpecifyDeleteHelper.class, ex); } if (treeClass == cls) { classHash.remove(cls); return null; } if (treeClass == Taxon.class) { String tmpSql = "SELECT tc.TaxonCitationID FROM taxoncitation tc INNER JOIN taxon tx ON tc.TaxonID = tx.TaxonID INNER JOIN taxontreedef ttd ON tx.TaxonTreeDefID = ttd.TaxonTreeDefID = " + ids.get(0); delSql = "DELETE FROM taxoncitation WHERE TaxonCitationID = "; child.pushPPS(new StackItem(null, tmpSql, delSql, false, true)); } delSql = "DELETE FROM " + className.toLowerCase() + " WHERE " + primaryKeyTD + " = " + ids.get(0) + " ORDER BY AcceptedID DESC, ParentID DESC"; child.pushPPS(new StackItem(null, sql, delSql, false, false)); delSql = "DELETE FROM " + itemTableNameTDI + " WHERE " + primaryKeyTD + " = " + ids.get(0) + " ORDER BY RankID DESC"; child.pushPPS(new StackItem(null, sql, delSql, false, false)); delSql = "DELETE FROM " + tableNameTD + " WHERE " + primaryKeyTD + " = " + ids.get(0); child.pushPPS(new StackItem(null, sql, delSql, false, false)); } } catch (SQLException ex) { edu.ku.brc.af.core.UsageTracker.incrSQLUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(SpecifyDeleteHelper.class, ex); ex.printStackTrace(); } } continue; } String colName = null; boolean isOKToDel = false; boolean includeSubTable = false; if (method.isAnnotationPresent(javax.persistence.OneToMany.class)) { String nm = method.getName(); boolean isAttachment = nm.indexOf("Attachment") > -1; boolean doDel = false; javax.persistence.OneToMany oneToMany = (javax.persistence.OneToMany) method .getAnnotation(javax.persistence.OneToMany.class); for (CascadeType ct : oneToMany.cascade()) { if (ct == CascadeType.ALL || ct == CascadeType.REMOVE) { doDel = true; break; } } if (!isAttachment && !doDel && method.isAnnotationPresent(org.hibernate.annotations.Cascade.class)) { org.hibernate.annotations.Cascade cascade = (org.hibernate.annotations.Cascade) method .getAnnotation(org.hibernate.annotations.Cascade.class); for (org.hibernate.annotations.CascadeType ct : cascade.value()) { if (ct == org.hibernate.annotations.CascadeType.ALL || ct == org.hibernate.annotations.CascadeType.DELETE || ct == org.hibernate.annotations.CascadeType.REMOVE) { doDel = true; break; } } } isOKToDel = !doDel ? isOKToDel(method) : true; if (checkIfIsShared && cls.equals(Geography.class) && method.getName().equals("getLocalities")) //Hibernate!?. Looks like the Cascade annotation is wrong. { isOKToDel = false; } includeSubTable = !checkIfIsShared ? isOKToDel : !isOKToDel; colName = tblInfo.getIdColumnName(); } else if (!checkIfIsShared && method.isAnnotationPresent(javax.persistence.ManyToOne.class)) { boolean doDel = false; javax.persistence.ManyToOne oneToMany = (javax.persistence.ManyToOne) method .getAnnotation(javax.persistence.ManyToOne.class); for (CascadeType ct : oneToMany.cascade()) { if (ct == CascadeType.ALL || ct == CascadeType.REMOVE) { doDel = true; } } isOKToDel = !doDel ? isOKToDel(method) : true; includeSubTable = !checkIfIsShared ? isOKToDel : !isOKToDel; if (includeSubTable) { javax.persistence.JoinColumn joinCol = (javax.persistence.JoinColumn) method .getAnnotation(javax.persistence.JoinColumn.class); if (joinCol != null) { colName = joinCol.name(); } } } else if (method.isAnnotationPresent(javax.persistence.ManyToMany.class)) { javax.persistence.JoinTable joinTable = (javax.persistence.JoinTable) method .getAnnotation(javax.persistence.JoinTable.class); if (joinTable != null) { String joinTableName = joinTable.name(); String joinColName = null; for (JoinColumn jc : joinTable.joinColumns()) { joinColName = jc.name(); break; } DBRelationshipInfo relInfo = null; for (DBRelationshipInfo ri : tblInfo.getRelationships()) { if (ri.getJoinTable() != null && ri.getJoinTable().equals(joinTableName)) { relInfo = ri; break; } } System.out.println(joinColName); if (cls != Agent.class) { if (inUseHash != null) inUseHash.put(relInfo.getClassName(), true); String sql = "SELECT " + joinColName + " FROM " + joinTableName + " WHERE " + joinColName + " = "; String delSql = "DELETE FROM " + joinTableName + " WHERE " + joinColName + " = "; if (debug) { printLevel(level); System.out.println(sql); } DBTableInfo ti = tblMgr.getByShortClassName(relInfo.getDataClass().getSimpleName()); if (!checkIfIsShared) { child.push(ti, sql, delSql); } } else { //System.err.println(cls.getName()); } } } if (includeSubTable) { //System.out.println(method.getName()+" "+method.getReturnType().getSimpleName()); String relName = method.getName().substring(3); DBRelationshipInfo relInfo = tblInfo.getRelationshipByName(relName); if (relInfo != null) { DBTableInfo ti = tblMgr.getByClassName(relInfo.getClassName()); if (ti != null) { String sql; if (method.isAnnotationPresent(javax.persistence.OneToMany.class)) { String otherColName = colName; DBRelationshipInfo ri = ti.getRelationshipByName(relInfo.getOtherSide()); if (ri != null) { otherColName = ri.getColName(); } sql = "SELECT " + ti.getAbbrev() + "." + ti.getIdColumnName() + " FROM " + ti.getName() + " " + ti.getAbbrev() + " INNER JOIN " + tblInfo.getName() + " " + tblInfo.getAbbrev() + " ON " + ti.getAbbrev() + "." + otherColName + " = " + tblInfo.getAbbrev() + "." + tblInfo.getIdColumnName() + " WHERE " + tblInfo.getAbbrev() + "." + colName + " = "; } else { sql = "SELECT " + ti.getAbbrev() + "." + ti.getIdColumnName() + " FROM " + ti.getName() + " " + ti.getAbbrev() + " INNER JOIN " + tblInfo.getName() + " " + tblInfo.getAbbrev() + " ON " + ti.getAbbrev() + "." + ti.getIdColumnName() + " = " + tblInfo.getAbbrev() + "." + colName + " WHERE " + tblInfo.getAbbrev() + "." + tblInfo.getIdColumnName() + " = "; } String delSql = "DELETE FROM " + ti.getName() + " WHERE " + ti.getIdColumnName() + " = "; if (debug) { printLevel(level); System.out.println(sql); printLevel(level); System.out.println(delSql); } if (relInfo.getDataClass() != Agent.class) { if (inUseHash != null) inUseHash.put(relInfo.getClassName(), true); if (ti.getClassObj() != cls || (doTrees && !Treeable.class.isAssignableFrom(cls))) { if (!checkIfIsShared) { getSubTables(child, ti.getClassObj(), id, sql, delSql, level + 1, inUseHash, checkIfIsShared); } else { child.push(ti, sql, delSql); } } else if (debug) { System.err.println("Skipping " + ti.getClassObj().getSimpleName()); } } else { //System.err.println(relInfo.getDataClass().getName()); } } else { String shortClassName = relInfo.getDataClass().getSimpleName(); String sql = "SELECT " + shortClassName + "ID FROM " + shortClassName.toLowerCase() + " WHERE " + tblInfo.getClassObj().getSimpleName() + "ID = "; String delSql = "DELETE FROM " + shortClassName.toLowerCase() + " WHERE " + shortClassName + "ID = "; if (debug) { printLevel(level); System.out.println(sql); printLevel(level); System.out.println(delSql); } child.push(tblInfo, sql, delSql); // NOTE: the tblInfo is for the parent! } } } } for (DBTableInfo ti : tblMgr.getTables()) { if (ti != tblInfo) { for (DBRelationshipInfo ri : ti.getRelationships()) { /*System.out.println(ri.getName());*/ if (ri.getDataClass() != Agent.class) { boolean hashOK = inUseHash == null || inUseHash.get(ti.getClassName()) == null; if (ri.getDataClass() == tblInfo.getClassObj() && hashOK && StringUtils.isEmpty(ri.getOtherSide())) { String sql = "SELECT " + ti.getIdColumnName() + " FROM " + ti.getName() + " WHERE " + ri.getColName() + " = "; String delSql = "DELETE FROM " + ti.getName() + " WHERE " + ti.getIdColumnName() + " = "; if (debug) { printLevel(level); System.out.println("Missed " + ti.getClassName() + " for " + tblInfo.getClassObj()); printLevel(level); System.out.println(sql); } if (inUseHash != null) inUseHash.put(ti.getClassName(), true); //if (!checkIfIsShared) { getSubTables(child, ti.getClassObj(), id, sql, delSql, level + 1, inUseHash, checkIfIsShared); } } else if (ri.getDataClass() == tblInfo.getClassObj() && !hashOK && StringUtils.isEmpty(ri.getOtherSide())) { if (debug) System.out.println("Skipping " + ti.getClassObj().getSimpleName() + " for " + tblInfo.getClassObj().getSimpleName()); } } else { //System.err.println(ri.getDataClass().getName()); } } } } if (debug) System.out.println(); classHash.remove(cls); return child; }
From source file:com.evolveum.midpoint.repo.sql.query2.definition.ClassDefinitionParser.java
private JpaLinkDefinition parseMethod(Method method) { CollectionSpecification collectionSpecification; // non-null if return type is Set<X>, null if it's X Type returnedContentType; // X in return type, which is either X or Set<X> if (Set.class.isAssignableFrom(method.getReturnType())) { // e.g. Set<RObject> or Set<String> or Set<REmbeddedReference<RFocus>> Type returnType = method.getGenericReturnType(); if (!(returnType instanceof ParameterizedType)) { throw new IllegalStateException("Method " + method + " returns a non-parameterized collection"); }// w ww. j a v a2s . c o m returnedContentType = ((ParameterizedType) returnType).getActualTypeArguments()[0]; collectionSpecification = new CollectionSpecification(); } else { returnedContentType = method.getReturnType(); collectionSpecification = null; } ItemPath itemPath = getJaxbName(method); String jpaName = getJpaName(method); Class jpaClass = getClass(returnedContentType); // sanity check if (Set.class.isAssignableFrom(jpaClass)) { throw new IllegalStateException("Collection within collection is not supported: method=" + method); } JpaLinkDefinition<? extends JpaDataNodeDefinition> linkDefinition; Any any = method.getAnnotation(Any.class); if (any != null) { JpaAnyContainerDefinition targetDefinition = new JpaAnyContainerDefinition(jpaClass); QName jaxbNameForAny = new QName(any.jaxbNameNamespace(), any.jaxbNameLocalPart()); linkDefinition = new JpaLinkDefinition<>(jaxbNameForAny, jpaName, collectionSpecification, false, targetDefinition); } else if (ObjectReference.class.isAssignableFrom(jpaClass)) { boolean embedded = method.isAnnotationPresent(Embedded.class); // computing referenced entity type from returned content type like RObjectReference<RFocus> or REmbeddedReference<RRole> Class referencedJpaClass; if (returnedContentType instanceof ParameterizedType) { referencedJpaClass = getClass( ((ParameterizedType) returnedContentType).getActualTypeArguments()[0]); } else { referencedJpaClass = RObject.class; } JpaReferenceDefinition targetDefinition = new JpaReferenceDefinition(jpaClass, referencedJpaClass); linkDefinition = new JpaLinkDefinition<>(itemPath, jpaName, collectionSpecification, embedded, targetDefinition); } else if (isEntity(jpaClass)) { JpaEntityDefinition content = parseClass(jpaClass); boolean embedded = method.isAnnotationPresent(Embedded.class) || jpaClass.isAnnotationPresent(Embeddable.class); linkDefinition = new JpaLinkDefinition<JpaDataNodeDefinition>(itemPath, jpaName, collectionSpecification, embedded, content); } else { boolean lob = method.isAnnotationPresent(Lob.class); boolean enumerated = method.isAnnotationPresent(Enumerated.class); //todo implement also lookup for @Table indexes boolean indexed = method.isAnnotationPresent(Index.class); Class jaxbClass = getJaxbClass(method, jpaClass); if (method.isAnnotationPresent(IdQueryProperty.class)) { if (collectionSpecification != null) { throw new IllegalStateException( "ID property is not allowed to be multivalued; for method " + method); } itemPath = new ItemPath(new IdentifierPathSegment()); } else if (method.isAnnotationPresent(OwnerIdGetter.class)) { if (collectionSpecification != null) { throw new IllegalStateException( "Owner ID property is not allowed to be multivalued; for method " + method); } itemPath = new ItemPath(new ParentPathSegment(), new IdentifierPathSegment()); } JpaPropertyDefinition propertyDefinition = new JpaPropertyDefinition(jpaClass, jaxbClass, lob, enumerated, indexed); // Note that properties are considered to be embedded linkDefinition = new JpaLinkDefinition<JpaDataNodeDefinition>(itemPath, jpaName, collectionSpecification, true, propertyDefinition); } return linkDefinition; }
From source file:com.jsmartframework.web.manager.BeanHandler.java
private void initAnnotatedActionMethods(String className, Class<?> clazz) { for (Method method : HELPER.getBeanMethods(clazz)) { List<Arg> arguments = new ArrayList<>(); for (Annotation[] annotations : method.getParameterAnnotations()) { for (Annotation annotation : annotations) { if (annotation instanceof Arg) { arguments.add((Arg) annotation); }//from w ww . j av a 2s . c o m } } if (method.isAnnotationPresent(Function.class)) { AnnotatedFunction annotatedFunction = new AnnotatedFunction(method, className, arguments); // Keep track of functions per bean method beanMethodFunctions.put(String.format(BEAN_METHOD_NAME_FORMAT, className, method.getName()), annotatedFunction); List<String> urlPaths = HELPER.cleanPaths(annotatedFunction.getFunction().forPaths()); for (String urlPath : urlPaths) { List<AnnotatedFunction> pathFunctions = annotatedFunctions.get(urlPath); if (pathFunctions == null) { pathFunctions = new ArrayList<>(); annotatedFunctions.put(urlPath, pathFunctions); } pathFunctions.add(annotatedFunction); } } if (method.isAnnotationPresent(Action.class)) { AnnotatedAction annotatedAction = new AnnotatedAction(method, className, arguments); for (String id : annotatedAction.getAction().forIds()) { annotatedActions.put(id, annotatedAction); } } } }
From source file:com.hiperf.common.ui.server.storage.impl.PersistenceHelper.java
private static void processMethod(String className, PropertyDescriptor[] pds, Set<PropertyDescriptor> ids, Set<PropertyDescriptor> collections, Set<PropertyDescriptor> lazys, Set<PropertyDescriptor> eagers, Set<LinkFileInfo> linkedFiles, PropertyDescriptor pd) throws PersistenceException { Method readMethod = pd.getReadMethod(); if (readMethod != null) { if (readMethod.isAnnotationPresent(Id.class)) { ids.add(pd);//from ww w . ja v a 2 s . c om if (readMethod.isAnnotationPresent(GeneratedValue.class)) { generatedIdClasses.add(className); } } OneToMany oneToMany = readMethod.getAnnotation(OneToMany.class); if (oneToMany != null) { collections.add(pd); } else { ManyToMany manyToMany = readMethod.getAnnotation(ManyToMany.class); if (manyToMany != null) { collections.add(pd); } else if (INakedObject.class.isAssignableFrom(pd.getPropertyType())) { OneToOne oneToOne = readMethod.getAnnotation(OneToOne.class); if (oneToOne != null) { if (oneToOne.fetch().equals(FetchType.LAZY)) lazys.add(pd); else eagers.add(pd); } else { ManyToOne manyToOne = readMethod.getAnnotation(ManyToOne.class); if (manyToOne != null) { if (manyToOne.fetch().equals(FetchType.LAZY)) lazys.add(pd); else eagers.add(pd); } else { Basic basic = readMethod.getAnnotation(Basic.class); if (basic != null) { if (basic.fetch().equals(FetchType.LAZY)) lazys.add(pd); else eagers.add(pd); } } } } } //processLinkedFiles(pds, linkedFiles, pd); } }