java.lang.Class.getDeclaredAnnotation()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(299)

本文整理了Java中java.lang.Class.getDeclaredAnnotation()方法的一些代码示例,展示了Class.getDeclaredAnnotation()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getDeclaredAnnotation()方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getDeclaredAnnotation

Class.getDeclaredAnnotation介绍

[英]Returns the annotation if it exists.
[中]返回注释(如果存在)。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Determine whether the given {@code Class} is a Kotlin type
 * (with Kotlin metadata present on it).
 */
public static boolean isKotlinType(Class<?> clazz) {
  return (kotlinMetadata != null && clazz.getDeclaredAnnotation(kotlinMetadata) != null);
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Determine whether the given {@code Class} is a Kotlin type
 * (with Kotlin metadata present on it).
 */
public static boolean isKotlinType(Class<?> clazz) {
  return (kotlinMetadata != null && clazz.getDeclaredAnnotation(kotlinMetadata) != null);
}

代码示例来源:origin: swagger-api/swagger-core

public static io.swagger.v3.oas.annotations.media.Schema getSchemaDeclaredAnnotation(Class<?> cls) {
  if (cls == null) {
    return null;
  }
  io.swagger.v3.oas.annotations.media.Schema mp = null;
  io.swagger.v3.oas.annotations.media.ArraySchema as = cls.getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.ArraySchema.class);
  if (as != null) {
    mp = as.schema();
  } else {
    mp = cls.getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
  }
  return mp;
}

代码示例来源:origin: apache/ignite

/**
 * Gets declared annotation for a class.
 *
 * @param <T> Type of annotation to return.
 * @param cls Class to get annotation from.
 * @param annCls Annotation to get.
 * @return Instance of annotation, or {@code null} if not found.
 */
@Nullable public static <T extends Annotation> T getDeclaredAnnotation(Class<?> cls, Class<T> annCls) {
  if (cls == Object.class)
    return null;
  return cls.getDeclaredAnnotation(annCls);
}

代码示例来源:origin: robovm/robovm

/**
 * Returns true if the annotation exists.
 */
private boolean isDeclaredAnnotationPresent(Class<? extends Annotation> annotationClass) {
  if (annotationClass == null) {
    throw new NullPointerException("annotationClass == null");
  }
  return getDeclaredAnnotation(annotationClass) != null;
}

代码示例来源:origin: neo4j/neo4j

private String groupPrefix()
{
  return SslPolicyConfig.class.getDeclaredAnnotation( Group.class ).value();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Determine whether an annotation of the specified {@code annotationType}
 * is declared locally (i.e. <em>directly present</em>) on the supplied
 * {@code clazz}.
 * <p>The supplied {@link Class} may represent any type.
 * <p>Meta-annotations will <em>not</em> be searched.
 * <p>Note: This method does <strong>not</strong> determine if the annotation
 * is {@linkplain java.lang.annotation.Inherited inherited}. For greater
 * clarity regarding inherited annotations, consider using
 * {@link #isAnnotationInherited(Class, Class)} instead.
 * @param annotationType the annotation type to look for
 * @param clazz the class to check for the annotation on
 * @return {@code true} if an annotation of the specified {@code annotationType}
 * is <em>directly present</em>
 * @see java.lang.Class#getDeclaredAnnotations()
 * @see java.lang.Class#getDeclaredAnnotation(Class)
 * @see #isAnnotationInherited(Class, Class)
 */
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
  try {
    return (clazz.getDeclaredAnnotation(annotationType) != null);
  }
  catch (Throwable ex) {
    handleIntrospectionFailure(clazz, ex);
    return false;
  }
}

代码示例来源:origin: resilience4j/resilience4j

private RateLimiter getRateLimiterAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
  RateLimiter rateLimiter = null;
  Class<?> targetClass = proceedingJoinPoint.getTarget().getClass();
  if (targetClass.isAnnotationPresent(RateLimiter.class)) {
    rateLimiter = targetClass.getAnnotation(RateLimiter.class);
    if (rateLimiter == null) {
      rateLimiter = targetClass.getDeclaredAnnotation(RateLimiter.class);
    }
    if (rateLimiter == null) {
      logger.debug("TargetClass has no declared annotation 'RateLimiter'");
    }
  }
  return rateLimiter;
}

代码示例来源:origin: spring-projects/spring-framework

@Nullable
private static Class<?> resolveExplicitTestContextBootstrapper(Class<?> testClass) {
  Set<BootstrapWith> annotations = AnnotatedElementUtils.findAllMergedAnnotations(testClass, BootstrapWith.class);
  if (annotations.isEmpty()) {
    return null;
  }
  if (annotations.size() == 1) {
    return annotations.iterator().next().value();
  }
  // Allow directly-present annotation to override annotations that are meta-present.
  BootstrapWith bootstrapWith = testClass.getDeclaredAnnotation(BootstrapWith.class);
  if (bootstrapWith != null) {
    return bootstrapWith.value();
  }
  throw new IllegalStateException(String.format(
      "Configuration error: found multiple declarations of @BootstrapWith for test class [%s]: %s",
      testClass.getName(), annotations));
}

代码示例来源:origin: robovm/robovm

@Override public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
  if (annotationType == null) {
    throw new NullPointerException("annotationType == null");
  }
  A annotation = getDeclaredAnnotation(annotationType);
  if (annotation != null) {
    return annotation;
  }
  if (annotationType.isAnnotationPresent(Inherited.class)) {
    for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
      annotation = sup.getDeclaredAnnotation(annotationType);
      if (annotation != null) {
        return annotation;
      }
    }
  }
  return null;
}

代码示例来源:origin: swagger-api/swagger-core

public static io.swagger.v3.oas.annotations.media.Schema getSchemaDeclaredAnnotation(Annotated a) {
  if (a == null) {
    return null;
  }
  io.swagger.v3.oas.annotations.media.ArraySchema arraySchema = a.getRawType().getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.ArraySchema.class);
  if (arraySchema != null) {
    return arraySchema.schema();
  } else {
    return a.getRawType().getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
  }
}

代码示例来源:origin: resilience4j/resilience4j

private CircuitBreaker getBackendMonitoredAnnotation(ProceedingJoinPoint proceedingJoinPoint) {
  if (logger.isDebugEnabled()) {
    logger.debug("circuitBreaker parameter is null");
  }
  CircuitBreaker circuitBreaker = null;
  Class<?> targetClass = proceedingJoinPoint.getTarget().getClass();
  if (targetClass.isAnnotationPresent(CircuitBreaker.class)) {
    circuitBreaker = targetClass.getAnnotation(CircuitBreaker.class);
    if (circuitBreaker == null) {
      if (logger.isDebugEnabled()) {
        logger.debug("TargetClass has no annotation 'CircuitBreaker'");
      }
      circuitBreaker = targetClass.getDeclaredAnnotation(CircuitBreaker.class);
      if (circuitBreaker == null) {
        if (logger.isDebugEnabled()) {
          logger.debug("TargetClass has no declared annotation 'CircuitBreaker'");
        }
      }
    }
  }
  return circuitBreaker;
}

代码示例来源:origin: spring-projects/spring-framework

private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
  try {
    A annotation = clazz.getDeclaredAnnotation(annotationType);
    if (annotation != null) {
      return annotation;

代码示例来源:origin: org.springframework/spring-core

/**
 * Determine whether an annotation of the specified {@code annotationType}
 * is declared locally (i.e. <em>directly present</em>) on the supplied
 * {@code clazz}.
 * <p>The supplied {@link Class} may represent any type.
 * <p>Meta-annotations will <em>not</em> be searched.
 * <p>Note: This method does <strong>not</strong> determine if the annotation
 * is {@linkplain java.lang.annotation.Inherited inherited}. For greater
 * clarity regarding inherited annotations, consider using
 * {@link #isAnnotationInherited(Class, Class)} instead.
 * @param annotationType the annotation type to look for
 * @param clazz the class to check for the annotation on
 * @return {@code true} if an annotation of the specified {@code annotationType}
 * is <em>directly present</em>
 * @see java.lang.Class#getDeclaredAnnotations()
 * @see java.lang.Class#getDeclaredAnnotation(Class)
 * @see #isAnnotationInherited(Class, Class)
 */
public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> annotationType, Class<?> clazz) {
  try {
    return (clazz.getDeclaredAnnotation(annotationType) != null);
  }
  catch (Throwable ex) {
    handleIntrospectionFailure(clazz, ex);
    return false;
  }
}

代码示例来源:origin: org.springframework/spring-core

private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
  try {
    A annotation = clazz.getDeclaredAnnotation(annotationType);
    if (annotation != null) {
      return annotation;

代码示例来源:origin: kiegroup/jbpm

public static void validate(Class<? extends WorkItemHandler> handlerClass,
                WorkItem workItem) throws Exception {

    Wid handlerWidAnnotation = handlerClass.getDeclaredAnnotation(Wid.class);
    if (workItem == null || handlerWidAnnotation == null || handlerWidAnnotation.parameters() == null || handlerWidAnnotation.parameters().length < 1) {
      // nothing to validate
      return;
    }

    for (WidParameter handlerWidParameter : handlerWidAnnotation.parameters()) {
      if (handlerWidParameter.name() != null && handlerWidParameter.required()) {
        if (workItem.getParameter(handlerWidParameter.name()) == null) {
          logger.error("Workitem declares following required parameter which does not exist: " + handlerWidParameter.name());
          throw new IllegalArgumentException("Workitem declares following required parameter which does not exist: " + handlerWidParameter.name());
        }
      }
    }
  }
}

代码示例来源:origin: JpressProjects/jpress

WechatAddonConfig wechatAddonConfig = addonClass.getDeclaredAnnotation(WechatAddonConfig.class);
if (wechatAddonConfig == null) {
  continue;

代码示例来源:origin: swagger-api/swagger-core

protected Discriminator resolveDiscriminator(JavaType type, ModelConverterContext context) {
  io.swagger.v3.oas.annotations.media.Schema declaredSchemaAnnotation = AnnotationsUtils.getSchemaDeclaredAnnotation(type.getRawClass());
  String disc = (declaredSchemaAnnotation == null) ? "" : declaredSchemaAnnotation.discriminatorProperty();
  if (disc.isEmpty()) {
    // longer method would involve AnnotationIntrospector.findTypeResolver(...) but:
    JsonTypeInfo typeInfo = type.getRawClass().getDeclaredAnnotation(JsonTypeInfo.class);
    if (typeInfo != null) {
      disc = typeInfo.property();
    }
  }
  if (!disc.isEmpty()) {
    Discriminator discriminator = new Discriminator()
        .propertyName(disc);
    if (declaredSchemaAnnotation != null) {
      DiscriminatorMapping mappings[] = declaredSchemaAnnotation.discriminatorMapping();
      if (mappings != null && mappings.length > 0) {
        for (DiscriminatorMapping mapping : mappings) {
          if (!mapping.value().isEmpty() && !mapping.schema().equals(Void.class)) {
            discriminator.mapping(mapping.value(), constructRef(context.resolve(new AnnotatedType().type(mapping.schema())).getName()));
          }
        }
      }
    }
    return discriminator;
  }
  return null;
}

代码示例来源:origin: swagger-api/swagger-core

public static Annotation mergeSchemaAnnotations(
    Annotation[] ctxAnnotations, JavaType type) {
  io.swagger.v3.oas.annotations.media.Schema tS = type.getRawClass().getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
  if (!hasSchemaAnnotation(tS)) {
    tS = null;
  io.swagger.v3.oas.annotations.media.ArraySchema tA = type.getRawClass().getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.ArraySchema.class);
  if (!hasArrayAnnotation(tA)) {
    tA = null;

代码示例来源:origin: mulesoft/mule

/**
 * Finds the first class in a class hierarchy that is annotated with {@link ArtifactClassLoaderRunnerConfig}.
 * <p/>
 * Annotated class is searched by levels, in a Breadth-first search (BFS) way. Starting from the class received as a parameter,
 * first the class is checked and return if annotated. Otherwise interface directly implemented by the class will be added to
 * review and finally its super class.
 * <p/>
 * Implemented interfaces have priority over class hierarchy as base classes can be imported from other modules with different
 * runner configurations.
 *
 * @param testClass class of the test begin executed by the test runner.
 * @return the first class found in the hierarchy that is annotated, null if no class in the hierarchy is annotated.
 */
public static Class findConfiguredClass(Class<?> testClass) {
 Deque<Class> classesToReview = new LinkedList<>();
 classesToReview.push(testClass);
 while (!classesToReview.isEmpty()) {
  Class currentClass = classesToReview.pop();
  if (currentClass.getDeclaredAnnotation(ArtifactClassLoaderRunnerConfig.class) != null) {
   LOGGER.info("Reading test runner configuration for test '{}' from '{}'", testClass.getName(), currentClass.getName());
   return currentClass;
  }
  addAll(classesToReview, currentClass.getInterfaces());
  if (currentClass.getSuperclass() != null && currentClass.getSuperclass() != Object.class) {
   classesToReview.add(currentClass.getSuperclass());
  }
 }
 return null;
}

相关文章

Class类方法