List of usage examples for java.lang Class getAnnotation
@SuppressWarnings("unchecked") public <A extends Annotation> A getAnnotation(Class<A> annotationClass)
From source file:cn.org.awcp.core.mybatis.mapper.EntityHelper.java
/** * ?/* w w w . j av a 2 s . c o m*/ * * @param entityClass */ public static synchronized void initEntityNameMap(Class<?> entityClass) { if (entityTableMap.get(entityClass) != null) { return; } // ?? EntityTable entityTable = null; if (entityClass.isAnnotationPresent(Table.class)) { Table table = entityClass.getAnnotation(Table.class); if (!table.name().equals("")) { entityTable = new EntityTable(); entityTable.setTable(table); } } if (entityTable == null) { entityTable = new EntityTable(); entityTable.name = camelhumpToUnderline(entityClass.getSimpleName()).toUpperCase(); } entityTableMap.put(entityClass, entityTable); // List<Field> fieldList = getAllField(entityClass, null); List<EntityColumn> columnList = new ArrayList<EntityColumn>(); List<EntityColumn> pkColumnList = new ArrayList<EntityColumn>(); List<EntityColumn> obColumnList = new ArrayList<EntityColumn>(); for (Field field : fieldList) { // if (field.isAnnotationPresent(Transient.class)) { continue; } EntityColumn entityColumn = new EntityColumn(); if (field.isAnnotationPresent(Id.class)) { entityColumn.setId(true); } if (field.isAnnotationPresent(OrderBy.class)) { OrderBy orderBy = field.getAnnotation(OrderBy.class); if (StringUtils.isNotBlank(orderBy.value()) && orderBy.value().equalsIgnoreCase("desc")) { entityColumn.setOrderBy(OrderByEnum.DESC); } else { entityColumn.setOrderBy(OrderByEnum.ASC); } } String columnName = null; if (field.isAnnotationPresent(Column.class)) { Column column = field.getAnnotation(Column.class); columnName = column.name(); } if (columnName == null || columnName.equals("")) { columnName = camelhumpToUnderline(field.getName()); } entityColumn.setProperty(field.getName()); entityColumn.setColumn(columnName.toUpperCase()); entityColumn.setJavaType(field.getType()); // - Oracle?MySqlUUID if (field.isAnnotationPresent(SequenceGenerator.class)) { SequenceGenerator sequenceGenerator = field.getAnnotation(SequenceGenerator.class); if (sequenceGenerator.sequenceName().equals("")) { throw new RuntimeException(entityClass + "" + field.getName() + "@SequenceGeneratorsequenceName!"); } entityColumn.setSequenceName(sequenceGenerator.sequenceName()); } else if (field.isAnnotationPresent(GeneratedValue.class)) { GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class); if (generatedValue.generator().equals("UUID")) { if (field.getType().equals(String.class)) { entityColumn.setUuid(true); } else { throw new RuntimeException(field.getName() + " - @GeneratedValue?UUID?String"); } } else if (generatedValue.generator().equals("JDBC")) { if (Number.class.isAssignableFrom(field.getType())) { entityColumn.setIdentity(true); entityColumn.setGenerator("JDBC"); } else { throw new RuntimeException(field.getName() + " - @GeneratedValue?UUID?String"); } } else { // ?generator??idsql,mysql=CALL // IDENTITY(),hsqldb=SELECT SCOPE_IDENTITY() // ??generator if (generatedValue.strategy() == GenerationType.IDENTITY) { // mysql entityColumn.setIdentity(true); if (!generatedValue.generator().equals("")) { String generator = null; MapperHelper.IdentityDialect identityDialect = MapperHelper.IdentityDialect .getDatabaseDialect(generatedValue.generator()); if (identityDialect != null) { generator = identityDialect.getIdentityRetrievalStatement(); } else { generator = generatedValue.generator(); } entityColumn.setGenerator(generator); } } else { throw new RuntimeException(field.getName() + " - @GeneratedValue?????:" + "\n1.?@GeneratedValue(generator=\"UUID\")" + "\n2.useGeneratedKeys@GeneratedValue(generator=\\\"JDBC\\\") " + "\n3.mysql?@GeneratedValue(strategy=GenerationType.IDENTITY[,generator=\"Mysql\"])"); } } } columnList.add(entityColumn); if (entityColumn.isId()) { pkColumnList.add(entityColumn); } if (entityColumn.getOrderBy() != null) { obColumnList.add(entityColumn); } } if (pkColumnList.size() == 0) { pkColumnList = columnList; } entityClassColumns.put(entityClass, columnList); entityClassPKColumns.put(entityClass, pkColumnList); entityOrderByColumns.put(entityClass, obColumnList); }
From source file:com.sitewhere.web.rest.documentation.RestDocumentationGenerator.java
/** * Parse information for a given controller. * //from w ww . j a v a 2 s .co m * @param controller * @param resourcesFolder * @return * @throws SiteWhereException */ protected static ParsedController parseController(Class<?> controller, File resourcesFolder) throws SiteWhereException { ParsedController parsed = new ParsedController(); Api api = controller.getAnnotation(Api.class); if (api == null) { throw new SiteWhereException("Swagger Api annotation missing on documented controller."); } parsed.setResource(api.value()); DocumentedController doc = controller.getAnnotation(DocumentedController.class); parsed.setName(doc.name()); parsed.setGlobal(doc.global()); System.out.println("Processing controller: " + parsed.getName() + " (" + parsed.getResource() + ")"); RequestMapping mapping = controller.getAnnotation(RequestMapping.class); if (mapping == null) { throw new SiteWhereException( "Spring RequestMapping annotation missing on documented controller: " + controller.getName()); } parsed.setBaseUri("/sitewhere/api" + mapping.value()[0]); // Verify controller markdown file. File markdownFile = new File(resourcesFolder, parsed.getResource() + ".md"); if (!markdownFile.exists()) { throw new SiteWhereException("Controller markdown file missing: " + markdownFile.getAbsolutePath()); } // Verify controller resources folder. File resources = new File(resourcesFolder, parsed.getResource()); if (!resources.exists()) { throw new SiteWhereException("Controller markdown folder missing: " + resources.getAbsolutePath()); } try { PegDownProcessor processor = new PegDownProcessor(); String markdown = readFile(markdownFile); parsed.setDescription(processor.markdownToHtml(markdown)); } catch (IOException e) { throw new SiteWhereException("Unable to read markdown from: " + markdownFile.getAbsolutePath(), e); } Method[] methods = controller.getMethods(); for (Method method : methods) { if (method.getAnnotation(Documented.class) != null) { ParsedMethod parsedMethod = parseMethod(parsed.getBaseUri(), method, resources); parsed.getMethods().add(parsedMethod); } } Collections.sort(parsed.getMethods(), new Comparator<ParsedMethod>() { @Override public int compare(ParsedMethod o1, ParsedMethod o2) { return o1.getSummary().compareTo(o2.getSummary()); } }); return parsed; }
From source file:org.paxml.util.ReflectUtils.java
/** * Find annotation from a class and all super classes and interfaces, stop * at the first encounter./*from w w w . j a v a 2 s. c om*/ * * @param <A> * the annotation type * @param clazz * the class * @param annotationClass * the annotation class * @return the annotation, or null if not from inhheritance tree. */ public static <A extends Annotation> A getAnnotation(Class<?> clazz, final Class<A> annotationClass) { return traverseInheritance(clazz, null, true, new IClassVisitor<A>() { public A onVisit(Class<?> clazz) { return clazz.getAnnotation(annotationClass); } }); }
From source file:com.nerve.commons.repository.utils.reflection.ReflectionUtils.java
/** * * ?annotationClass/* w ww . j av a2 s . co m*/ * * @param targetClass * Class * @param annotationClass * Class * * @return List */ public static <T extends Annotation> List<T> getAnnotations(Class targetClass, Class annotationClass) { Assert.notNull(targetClass, "targetClass?"); Assert.notNull(annotationClass, "annotationClass?"); List<T> result = new ArrayList<T>(); Annotation annotation = targetClass.getAnnotation(annotationClass); if (annotation != null) { result.add((T) annotation); } Constructor[] constructors = targetClass.getDeclaredConstructors(); // ? CollectionUtils.addAll(result, getAnnotations(constructors, annotationClass).iterator()); Field[] fields = targetClass.getDeclaredFields(); // ? CollectionUtils.addAll(result, getAnnotations(fields, annotationClass).iterator()); Method[] methods = targetClass.getDeclaredMethods(); // ? CollectionUtils.addAll(result, getAnnotations(methods, annotationClass).iterator()); for (Class<?> superClass = targetClass.getSuperclass(); superClass == null || superClass == Object.class; superClass = superClass.getSuperclass()) { List<T> temp = (List<T>) getAnnotations(superClass, annotationClass); if (CollectionUtils.isNotEmpty(temp)) { CollectionUtils.addAll(result, temp.iterator()); } } return result; }
From source file:me.xiaopan.android.gohttp.requestobject.RequestParser.java
/** * ?URL/*from w ww . j a va 2s .c o m*/ * @param context * @param requestClass */ public static String parseURLAnnotation(Context context, Class<? extends Request> requestClass) { URL annotation = requestClass.getAnnotation(URL.class); if (annotation == null) { return null; } String annotationValue = annotation.value(); if (annotationValue != null && !"".equals(annotationValue)) { return annotationValue; } else if (context != null && annotation.resId() > 0) { return context.getString(annotation.resId()); } else { return null; } }
From source file:me.xiaopan.android.gohttp.requestobject.RequestParser.java
/** * ??// www. j ava 2s .c o m * @param context * @param requestClass */ public static String parseHostAnnotation(Context context, Class<? extends Request> requestClass) { Host annotation = requestClass.getAnnotation(Host.class); if (annotation == null) { return null; } String annotationValue = annotation.value(); if (annotationValue != null && !"".equals(annotationValue)) { return annotationValue; } else if (context != null && annotation.resId() > 0) { return context.getString(annotation.resId()); } else { return null; } }
From source file:me.xiaopan.android.gohttp.requestobject.RequestParser.java
/** * ???/*from w w w. java2 s .c o m*/ * @param context * @param requestClass */ public static String parseNameAnnotation(Context context, Class<? extends Request> requestClass) { Name annotation = requestClass.getAnnotation(Name.class); if (annotation == null) { return null; } String annotationValue = annotation.value(); if (annotationValue != null && !"".equals(annotationValue)) { return annotationValue; } else if (context != null && annotation.resId() > 0) { return context.getString(annotation.resId()); } else { return null; } }
From source file:me.xiaopan.android.gohttp.requestobject.RequestParser.java
/** * ?//from w w w . j av a 2s .c om * @param context * @param requestClass */ public static String parsePathAnnotation(Context context, Class<? extends Request> requestClass) { Path annotation = requestClass.getAnnotation(Path.class); if (annotation == null) { return null; } String annotationValue = annotation.value(); if (annotationValue != null && !"".equals(annotationValue)) { return annotationValue; } else if (context != null && annotation.resId() > 0) { return context.getString(annotation.resId()); } else { return null; } }
From source file:com.ankang.report.config.ReportConfig.java
private static <K, V> void convertMap(Map<? extends K, ? extends V> m) { if (null == m) { logger.error("map is null"); return;//from w w w .j a v a 2 s . co m } synchronized (FILETERS) { for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) { String key = e.getKey().toString().toLowerCase(); Object value = e.getValue(); if (ReportConfigItem.FILETER.getConfigName().equalsIgnoreCase(key)) { Class<?> filterzz = null; Fileter fileter = null; String[] clazzs = null; int i = 0; try { if (value != null && value.toString().contains(",")) { clazzs = value.toString().trim().split(","); } else { clazzs = new String[] { value.toString() }; } for (; i < clazzs.length; i++) { filterzz = Class.forName(clazzs[i]); Activate activate = filterzz.getAnnotation(Activate.class); if (!FILETERS.containsValue((fileter = (Fileter) ReportCabinet.getBean(filterzz)))) { Integer order = activate != null && activate.order() >= Integer.MIN_VALUE && activate.order() <= Integer.MAX_VALUE ? activate.order() : Integer.MIN_VALUE; if (FILETERS.containsKey(order)) { // ?key??,?? if (order < (Integer.MAX_VALUE >> 1) + 1) { while (FILETERS.containsKey(order)) { order++; } } else if (order > (Integer.MAX_VALUE >> 1)) { while (FILETERS.containsKey(order)) { order--; } } } FILETERS.put(order, fileter); logger.debug("load fileter.[" + filterzz + "]"); continue; } } } catch (ClassNotFoundException c) { logger.error("fileter load fial:" + clazzs[i], c); throw new ReportException("fileter load fial.[%s]", clazzs[i]); } } else { containsKey(key, value, true); } } } }
From source file:net.sf.firemox.xml.XmlConfiguration.java
/** * @param mdbClass//from ww w. j av a 2 s. c o m * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException */ private static XmlToMDB getAnnotedBuilder(Class<?> mdbClass, String tagName, String packageName, String namespace) throws InstantiationException, IllegalAccessException, ClassNotFoundException { if (!mdbClass.isAnnotationPresent(XmlTestElement.class)) { error("Unsupported annoted " + namespace + " '" + tagName + "'"); return DummyBuilder.instance(); } XmlTestElement xmlElement = mdbClass.getAnnotation(XmlTestElement.class); XmlAnnoted annotedInstance = (XmlAnnoted) Class.forName(packageName + "." + namespace + ".XmlAnnoted") .newInstance(); annotedInstance.setXmlElement(xmlElement); annotedInstance.setAnnotedClass(mdbClass); return annotedInstance; }