Java Class.getAnnotation Examples

Java Class.getAnnotation - 30 examples found. These are the top rated real world Java examples of Class.getAnnotation extracted from open source projects. You can rate examples to help us improve the quality of examples.
 @SuppressWarnings({"unchecked"})
 private void addPerRequest(Class clazz) {
   if (clazz.getAnnotation(Provider.class) != null) {
     // per-request provider
     if (ContextResolver.class.isAssignableFrom(clazz)) {
       providers.addContextResolver(clazz);
     }
     if (ExceptionMapper.class.isAssignableFrom(clazz)) {
       providers.addExceptionMapper(clazz);
     }
     if (MessageBodyReader.class.isAssignableFrom(clazz)) {
       providers.addMessageBodyReader(clazz);
     }
     if (MessageBodyWriter.class.isAssignableFrom(clazz)) {
       providers.addMessageBodyWriter(clazz);
     }
   } else if (clazz.getAnnotation(Filter.class) != null) {
     // per-request filter
     if (MethodInvokerFilter.class.isAssignableFrom(clazz)) {
       providers.addMethodInvokerFilter(clazz);
     }
     if (RequestFilter.class.isAssignableFrom(clazz)) {
       providers.addRequestFilter(clazz);
     }
     if (ResponseFilter.class.isAssignableFrom(clazz)) {
       providers.addResponseFilter(clazz);
     }
   } else if (clazz.getAnnotation(Path.class) != null) {
     // per-request resource
     resources.addResource(clazz, null);
   }
 }
  @Override
  public void beforeTestMethod(TestContext testContext) throws Exception {

    checkInitialized();

    Method testMethod = getTestMethod(testContext);

    Expectations expectations = testMethod.getAnnotation(Expectations.class);
    Expectation expectation = testMethod.getAnnotation(Expectation.class);
    NoQueriesAllowed notAllowedQueries = testMethod.getAnnotation(NoQueriesAllowed.class);

    // If no annotations present, check the test class and its superclasses
    for (Class<?> testClass = testMethod.getDeclaringClass();
        null == expectations
            && null == expectation
            && null == notAllowedQueries
            && !Object.class.equals(testClass);
        testClass = testClass.getSuperclass()) {
      expectations = testClass.getAnnotation(Expectations.class);
      expectation = testClass.getAnnotation(Expectation.class);
      notAllowedQueries = testClass.getAnnotation(NoQueriesAllowed.class);
    }

    if (null != expectation && null != notAllowedQueries) {
      throw new IllegalArgumentException(
          "Cannot specify @Expectation and @NotAllowedQueries on one test method");
    } else if (null != expectations && null != notAllowedQueries) {
      throw new IllegalArgumentException(
          "Cannot specify @Expectations and @NotAllowedQueries on one test method");
    } else if (null != expectations || null != expectation) {

      List<Expectation> expectationList = new ArrayList<Expectation>();

      if (null != expectation) {
        expectationList.add(expectation);
      }

      if (null != expectations) {
        expectationList.addAll(Arrays.asList(expectations.value()));
      }

      for (Expectation expectation1 : expectationList) {
        if (expectation1.value() != -1) {
          if (expectation1.atMost() != -1 || expectation1.atLeast() != -1) {
            throw new IllegalArgumentException(
                "Cannot specify value parameter together with atLeast or atMost parameters");
          }
        }
      }

      setAttribute(testContext, SPY_ATTRIBUTE_NAME, Sniffer.expect(expectationList));

    } else if (null != notAllowedQueries) {
      setAttribute(
          testContext,
          SPY_ATTRIBUTE_NAME,
          Sniffer.expect(
              Collections.singletonList(NoQueriesAllowed.class.getAnnotation(Expectation.class))));
    }
  }
Example #3
0
 /** 获取表名 */
 public static <T> String getTableName(Class<T> clazz) {
   if (clazz.getAnnotation(Entity.class) != null && clazz.getAnnotation(Table.class) != null) {
     return clazz.getAnnotation(Table.class).name();
   } else {
     return clazz.getSimpleName().toLowerCase();
   }
 }
Example #4
0
	/**
	 * Create a new HandlerMethodResolver for the specified handler type.
	 * @param handlerType the handler class to introspect
	 */
	public HandlerMethodResolver(final Class<?> handlerType,final String baseurls[]) {
		this.typeLevelMapping = handlerType.getAnnotation(HandlerMapping.class);
		Method[] methods = handlerType.getMethods();
		for(Method method:methods)
		{
				
				if (HandlerUtils.isHandlerMethod(handlerType,method)) {
					//System.out.println(method);
					handlerMethods.add(new MethodInfo(ClassUtils.getMostSpecificMethod(method, handlerType),baseurls));
				}
//				else if (method.isAnnotationPresent(InitBinder.class)) {
//					initBinderMethods.add(ClassUtils.getMostSpecificMethod(method, handlerType));
//				}
				else if (method.isAnnotationPresent(ModelAttribute.class)) {
					modelAttributeMethods.add(ClassUtils.getMostSpecificMethod(method, handlerType));
				}
			}
		
		SessionAttributes sessionAttributes = handlerType.getAnnotation(SessionAttributes.class);
		this.sessionAttributesFound = (sessionAttributes != null);
		if (this.sessionAttributesFound) {
			this.sessionAttributeNames.addAll(Arrays.asList(sessionAttributes.value()));
			this.sessionAttributeTypes.addAll(Arrays.asList(sessionAttributes.types()));
		}
	}
  /**
   * Creates results from @Results and @Result annotations
   *
   * @param actionClass class to check for annotations
   * @param packageConfig packageConfig where the action will be located
   * @param defaultResultPath default result path
   * @param results map of results
   * @param resultsByExtension map of result types keyed by extension
   */
  protected void createResultsFromAnnotations(
      Class<?> actionClass,
      PackageConfig packageConfig,
      String defaultResultPath,
      Map<String, ResultConfig> results,
      Map<String, ResultTypeConfig> resultsByExtension) {
    Results resultsAnn = actionClass.getAnnotation(Results.class);
    if (resultsAnn != null) {
      createFromAnnotations(
          results,
          defaultResultPath,
          packageConfig,
          resultsAnn.value(),
          actionClass,
          resultsByExtension);
    }

    Result resultAnn = actionClass.getAnnotation(Result.class);
    if (resultAnn != null) {
      createFromAnnotations(
          results,
          defaultResultPath,
          packageConfig,
          new Result[] {resultAnn},
          actionClass,
          resultsByExtension);
    }
  }
  protected static boolean matchesSegment(Field field, String segmentTag) {
    Class<?> clz = field.getType();

    if (Collection.class.isAssignableFrom(clz)) {
      clz = getCollectionType(field);
    }

    if (clz.isAnnotationPresent(EDISegment.class)) {
      EDISegment es = clz.getAnnotation(EDISegment.class);
      if (StringUtils.equals(segmentTag, es.tag())) {
        return true;
      }
    } else if (clz.isAnnotationPresent(EDISegmentGroup.class)) {
      EDISegmentGroup esg = clz.getAnnotation(EDISegmentGroup.class);

      String ediSegmentGroupTag = esg.header();

      if (StringUtils.isNotBlank(ediSegmentGroupTag)) {
        if (StringUtils.equals(segmentTag, ediSegmentGroupTag)) {
          return true;
        }
      } else {
        // get the segement group's first field, and recurse.
        if (clz.getDeclaredFields().length > 0) {
          return matchesSegment(clz.getDeclaredFields()[0], segmentTag);
        }
      }
    }

    return false;
  }