java.lang.reflect.Constructor.isAnnotationPresent()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(146)

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

Constructor.isAnnotationPresent介绍

暂无

代码示例

代码示例来源:origin: com.google.inject/guice

/** Returns true if the inject annotation is on the constructor. */
private static boolean hasAtInject(Constructor cxtor) {
 return cxtor.isAnnotationPresent(Inject.class)
   || cxtor.isAnnotationPresent(javax.inject.Inject.class);
}

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

public boolean callConstructorIsAnnotationPresent(Constructor<?> m, Class<? extends Annotation> annotClass) {
  return m.isAnnotationPresent(annotClass);
}
public Annotation callConstructorGetAnnotation(Constructor<?> m, Class<? extends Annotation> annotClass) {

代码示例来源:origin: oracle/helidon

/**
 * Check if constructor can be used for deserialization.
 */
private static boolean checkConstructor(Constructor<?> constructor, boolean hasParams) {
  return Modifier.isPublic(constructor.getModifiers())
      && !constructor.isAnnotationPresent(Transient.class)
      && ((constructor.getParameterCount() > 0) == hasParams);
}

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

private void handleClass(Class<?> cl) {
  if (stopClasses.contains(cl) || cl.isAnnotationPresent(QueryExclude.class)) {
    return;
  } else if (cl.isAnnotationPresent(entityAnnotation)) {
    entityTypes.put(cl, null);
  } else if (cl.isAnnotationPresent(embeddableAnnotation)) {
    embeddableTypes.put(cl, null);
  } else if (cl.isAnnotationPresent(supertypeAnnotation)) {
    superTypes.put(cl, null);
  } else {
    for (Constructor<?> constructor : cl.getConstructors()) {
      if (constructor.isAnnotationPresent(QueryProjection.class)) {
        projectionTypes.put(cl, null);
        break;
      }
    }
  }
}

代码示例来源:origin: apache/incubator-gobblin

private boolean canUseConstructor(Constructor<?> constructor) {
 if (!Modifier.isPublic(constructor.getModifiers())) {
  return false;
 }
 if (!constructor.isAnnotationPresent(CliObjectSupport.class)) {
  return false;
 }
 for (Class<?> param : constructor.getParameterTypes()) {
  if (param != String.class) {
   return false;
  }
 }
 return constructor.getParameterTypes().length ==
   constructor.getAnnotation(CliObjectSupport.class).argumentNames().length;
}

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

private static <T> Constructor<T> findImplicitConstructorFor(Class<T> type) {
    @SuppressWarnings("unchecked")
    final Constructor<T>[] constructors = (Constructor<T>[]) type.getDeclaredConstructors();

    List<Constructor<T>> annotatedConstructors = Stream.of(constructors)
        .filter(c -> c.isAnnotationPresent(ConstructorProperties.class))
        .collect(Collectors.toList());

    if (annotatedConstructors.size() > 1) {
      throw new IllegalArgumentException(type + " may have at most one constructor annotated @ConstructorProperties");
    } else if (annotatedConstructors.size() == 1) {
      return annotatedConstructors.get(0);
    }

    if (constructors.length != 1) {
      throw new IllegalArgumentException(type + " must have exactly one constructor, or specify it with @JdbiConstructor or @ConstructorProperties");
    }
    return constructors[0];
  }
}

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

/**
 * Find an invokable constructor.  Prefer an {@link JdbiConstructor} annotated
 * one if present.  Throws if multiple or zero candidates are found.
 *
 * @param <T>  the type to inspect
 * @param type the type to inspect
 * @return the preferred constructor
 */
public static <T> Constructor<T> findConstructorFor(Class<T> type) {
  @SuppressWarnings("unchecked")
  final Constructor<T>[] constructors = (Constructor<T>[]) type.getDeclaredConstructors();
  List<Constructor<T>> annotatedConstructors = Stream.of(constructors)
      .filter(c -> c.isAnnotationPresent(JdbiConstructor.class))
      .collect(Collectors.toList());
  if (annotatedConstructors.size() > 1) {
    throw new IllegalArgumentException(type + " may have at most one constructor annotated @JdbiConstructor");
  } else if (annotatedConstructors.size() == 1) {
    return annotatedConstructors.get(0);
  }
  return findImplicitConstructorFor(type);
}

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

/**
 * Find an invokable instance factory, such as a constructor or a static factory method.
 * Prefer an {@link JdbiConstructor} annotated constructor or static factory method if
 * one is present. Throws if multiple or zero candidates are found.
 *
 * @param <T>  the type to inspect
 * @param type the type to inspect
 * @return the preferred constructor or static factory method
 */
static <T> InstanceFactory<T> findFactoryFor(Class<T> type) {
  @SuppressWarnings("unchecked")
  final Constructor<T>[] constructors = (Constructor<T>[]) type.getDeclaredConstructors();
  List<Constructor<T>> explicitConstructors = Stream.of(constructors)
    .filter(constructor -> constructor.isAnnotationPresent(JdbiConstructor.class))
    .collect(Collectors.toList());
  List<Method> explicitFactoryMethods = Stream.of(type.getDeclaredMethods())
    .filter(method -> method.isAnnotationPresent(JdbiConstructor.class))
    .collect(Collectors.toList());
  if (explicitConstructors.size() + explicitFactoryMethods.size() > 1) {
    throw new IllegalArgumentException(type + " may have at most one constructor or static factory method annotated @JdbiConstructor");
  }
  if (explicitConstructors.size() == 1) {
    return new ConstructorInstanceFactory<>(explicitConstructors.get(0));
  }
  if (explicitFactoryMethods.size() == 1) {
    return new StaticMethodInstanceFactory<>(type, explicitFactoryMethods.get(0));
  }
  return new ConstructorInstanceFactory<>(findImplicitConstructorFor(type));
}

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

Constructor<?> constructor = null;
for (Constructor<?> c : implementation.getConstructors()) {
  if (c.isAnnotationPresent(Inject.class)) {
    constructor = c;
    break;

代码示例来源:origin: square/dagger

if (!constructor.isAnnotationPresent(Inject.class)) {
 continue;

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

private void addConstructors(Class<?> cl, EntityType type) {
  for (Constructor<?> constructor : cl.getConstructors()) {
    if (constructor.isAnnotationPresent(QueryProjection.class)) {
      List<Parameter> parameters = Lists.newArrayList();
      for (int i = 0; i < constructor.getParameterTypes().length; i++) {
        Type parameterType = typeFactory.get(
            constructor.getParameterTypes()[i],
            constructor.getGenericParameterTypes()[i]);
        for (Annotation annotation : constructor.getParameterAnnotations()[i]) {
          if (annotation.annotationType().equals(QueryType.class)) {
            QueryType queryType = (QueryType) annotation;
            parameterType = parameterType.as(TypeCategory.valueOf(queryType.value().name()));
          }
        }
        parameters.add(new Parameter("param" + i, parameterType));
      }
      type.addConstructor(new com.mysema.codegen.model.Constructor(parameters));
    }
  }
}

代码示例来源:origin: jooby-project/jooby

} else {
 for (Constructor<?> c : cons) {
  if (c.isAnnotationPresent(Inject.class)) {
   if (inject != null) {
    throw new IllegalStateException(

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

if (ctor.isAnnotationPresent(Inject.class)) {
 injectCtors.add((Constructor<T>) ctor);
} else {

代码示例来源:origin: camunda/camunda-bpm-platform

private Constructor<?> findAnnotatedConstructor(final Constructor<?>[] constructors) {
 for (final Constructor<?> constructor : constructors) {
  if (constructor.isAnnotationPresent(AutomapConstructor.class)) {
   return constructor;
  }
 }
 return null;
}

代码示例来源:origin: org.mybatis/mybatis

private Constructor<?> findDefaultConstructor(final Constructor<?>[] constructors) {
 if (constructors.length == 1) return constructors[0];
 for (final Constructor<?> constructor : constructors) {
  if (constructor.isAnnotationPresent(AutomapConstructor.class)) {
   return constructor;
  }
 }
 return null;
}

代码示例来源:origin: spotify/heroic

private void verifyJson(final Class<?> cls) {
  final List<Constructor<?>> constructors = Arrays
    .stream(cls.getConstructors())
    .filter(c -> c.isAnnotationPresent(ConstructorProperties.class) ||
      c.isAnnotationPresent(JsonCreator.class))
    .collect(Collectors.toList());
  final List<Method> jsonCreators = Arrays
    .stream(cls.getMethods())
    .filter(m -> ((m.getModifiers() & Modifier.STATIC) != 0) &&
      m.isAnnotationPresent(JsonCreator.class))
    .collect(Collectors.toList());
  if (constructors.size() + jsonCreators.size() != 1) {
    throw new AssertionError(
      "Constructors (" + constructors + ") and @JsonCreator methods (" +
        jsonCreators + ") conflicting for JSON de-serialization");
  }
}

代码示例来源:origin: spotify/heroic

private Optional<Object> lookupClass(final Class<?> cls, final boolean secondary) {
  return Arrays
    .stream(cls.getConstructors())
    .filter(c -> c.isAnnotationPresent(ConstructorProperties.class))
    .findAny()
    .map(c -> {
      final String[] names = c.getAnnotation(ConstructorProperties.class).value();
      final Type[] types = c.getGenericParameterTypes();
      if (names.length != types.length) {
        throw new IllegalArgumentException(
          "@ConstructorProperties number of arguments do not match number of " +
            "parameters: " + c);
      }
      final Object[] arguments = new Object[types.length];
      for (int i = 0; i < types.length; i++) {
        arguments[i] = lookup(types[i], secondary, names[i]);
      }
      try {
        return c.newInstance(arguments);
      } catch (final ReflectiveOperationException e) {
        throw Throwables.propagate(e);
      }
    });
}

代码示例来源:origin: spotify/heroic

public void verify() {
  final ValueSuppliers suppliers = new ValueSuppliers(valueSuppliers);
  final FakeValueProvider valueProvider = new FakeValueProvider(suppliers);
  final Constructor<?> constructor = Arrays
    .stream(cls.getConstructors())
    .filter(c -> c.isAnnotationPresent(ConstructorProperties.class))
    .findAny()
    .orElseThrow(() -> new IllegalArgumentException(
      "No constructor annotated with @ConstructorProperties"));
  final List<FieldSpec> specs = buildFieldSpecs(constructor, valueProvider);
  if (checkGetters) {
    verifyGetters(constructor, specs);
  }
  verifyEquals(constructor, specs);
  verifyToString(constructor, specs);
  if (checkJson) {
    verifyJson(cls);
  }
}

代码示例来源:origin: org.apache.juneau/juneau-marshall

/**
 * Returns <jk>true</jk> if the specified constructor doesn't have the {@link Deprecated @Deprecated} annotation on it.
 *
 * @param c The constructor.
 * @return <jk>true</jk> if the specified constructor doesn't have the {@link Deprecated @Deprecated} annotation on it.
 */
public static boolean isNotDeprecated(Constructor<?> c) {
  return ! c.isAnnotationPresent(Deprecated.class);
}

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

public static boolean isAnnotationPresent(Constructor<?> clazz, Class<? extends Annotation> anType) {
  if (AnnotationDataStore.isConstructorDataRecorded(clazz)) {
    return AnnotationDataStore.isConstructorAnnotationPresent(clazz, anType);
  }
  return clazz.isAnnotationPresent(anType);
}

相关文章

微信公众号

最新文章

更多