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

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

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

Constructor.getAnnotation介绍

暂无

代码示例

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

public Annotation callConstructorGetAnnotation(Constructor<?> m, Class<? extends Annotation> annotClass) {
  return m.getAnnotation(annotClass);
}
public List<List<Annotation>> callConstructorGetParameterAnnotations(Constructor<?> m) {

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

@Nullable
  public static String[] evaluate(Constructor<?> candidate, int paramCount) {
    ConstructorProperties cp = candidate.getAnnotation(ConstructorProperties.class);
    if (cp != null) {
      String[] names = cp.value();
      if (names.length != paramCount) {
        throw new IllegalStateException("Constructor annotated with @ConstructorProperties but not " +
            "corresponding to actual number of parameters (" + paramCount + "): " + candidate);
      }
      return names;
    }
    else {
      return null;
    }
  }
}

代码示例来源:origin: MorphiaOrg/morphia

@SuppressWarnings("unchecked")
private boolean injectOnConstructor(final Class clazz) {
  final Constructor[] cs = clazz.getDeclaredConstructors();
  for (final Constructor constructor : cs) {
    if (constructor.getAnnotation(Inject.class) != null) {
      return true;
    }
  }
  return false;
}

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

private boolean shouldInject(final Class<?> clazz) {
  for(Constructor<?> constructor : clazz.getDeclaredConstructors()) {
   if (constructor.getAnnotation(Inject.class) != null) {
    return true;
   }
  }
  return false;
 }
}

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

/**
 * {@inheritDoc}
 */
public Instantiator replaceBy(Resolved instantiator) {
  Priority left = constructor.getAnnotation(Priority.class), right = instantiator.constructor.getAnnotation(Priority.class);
  int leftPriority = left == null ? Priority.DEFAULT : left.value(), rightPriority = right == null ? Priority.DEFAULT : right.value();
  if (leftPriority > rightPriority) {
    return this;
  } else if (leftPriority < rightPriority) {
    return instantiator;
  } else {
    throw new IllegalStateException("Ambiguous constructors " + constructor + " and " + instantiator.constructor);
  }
}

代码示例来源:origin: jenkinsci/configuration-as-code-plugin

@CheckForNull
public static Constructor getDataBoundConstructor(@Nonnull Class type) {
  for (Constructor c : type.getConstructors()) {
    if (c.getAnnotation(DataBoundConstructor.class) != null) return c;
  }
  return null;
}

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

@Override
public <A extends IAnnotation> A findAnnotation(Constructor<?> cons, Class<A> annotationClass) {
 final Class<? extends Annotation> a = m_annotationMap.get(annotationClass);
 if (a == null) {
  throw new IllegalArgumentException("Java @Annotation class for '"
    + annotationClass + "' not found.");
 }
 Annotation annotation = cons.getAnnotation(a);
 return findAnnotation(cons.getDeclaringClass(), annotation, annotationClass, null, cons, null,
   new Pair<>(annotation, cons));
}

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

@Nullable
  public static String[] evaluate(Constructor<?> candidate, int paramCount) {
    ConstructorProperties cp = candidate.getAnnotation(ConstructorProperties.class);
    if (cp != null) {
      String[] names = cp.value();
      if (names.length != paramCount) {
        throw new IllegalStateException("Constructor annotated with @ConstructorProperties but not " +
            "corresponding to actual number of parameters (" + paramCount + "): " + candidate);
      }
      return names;
    }
    else {
      return null;
    }
  }
}

代码示例来源: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: fabioCollini/DaggerMock

public void checkOverriddenInjectAnnotatedClass(List<Object> modules) {
  Set<String> errors = new HashSet<>();
  for (Map.Entry<ObjectId, Provider> entry : fields.entrySet()) {
    ObjectId objectId = entry.getKey();
    Constructor[] constructors = objectId.objectClass.getConstructors();
    for (Constructor constructor : constructors) {
      if (constructor.getAnnotation(Inject.class) != null && !existProvidesMethodInModule(objectId, modules)) {
        errors.add(objectId.objectClass.getName());
      }
    }
  }
  ErrorsFormatter.throwExceptionOnErrors(
      "Error while trying to override objects",
      errors,
      "You must define overridden objects using a @Provides annotated method instead of using @Inject annotation");
}

代码示例来源:origin: alibaba/fastjson

JSONCreator annotation = constructor.getAnnotation(JSONCreator.class);
if (annotation != null) {
  if (creatorConstructor != null) {

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

@SuppressWarnings("MagicConstant")
private boolean isCompatible(final Constructor<?> constructor) {
  if (constructor.getAnnotation(Inject.class) != null) {
    // JSR-330 applicable
    return true;
  }
  final int paramSize = constructor.getParameterTypes().length;
  if (paramSize != 0 && resolverAnnotations.get().isEmpty()) {
    return false;
  }
  if (!Modifier.isPublic(constructor.getModifiers())) {
    // return true for a default constructor, return false otherwise.
    return paramSize == 0
        && (constructor.getDeclaringClass().getModifiers()
              & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE)) == constructor.getModifiers();
  }
  for (final Annotation[] paramAnnotations : constructor.getParameterAnnotations()) {
    boolean found = false;
    for (final Annotation paramAnnotation : paramAnnotations) {
      if (resolverAnnotations.get().contains(paramAnnotation.annotationType())) {
        found = true;
        break;
      }
    }
    if (!found) {
      return false;
    }
  }
  return true;
}

代码示例来源:origin: cbeust/testng

@Override
public <A extends IAnnotation> A findAnnotation(Constructor<?> cons, Class<A> annotationClass) {
 final Class<? extends Annotation> a = m_annotationMap.get(annotationClass);
 if (a == null) {
  throw new IllegalArgumentException(
    "Java @Annotation class for '" + annotationClass + "' not found.");
 }
 Annotation annotation = cons.getAnnotation(a);
 return findAnnotation(
   cons.getDeclaringClass(),
   annotation,
   annotationClass,
   null,
   cons,
   null,
   new Pair<>(annotation, cons),
   null);
}

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

ConstructorProperties cp = ctor.getAnnotation(ConstructorProperties.class);
String[] paramNames = (cp != null ? cp.value() : parameterNameDiscoverer.getParameterNames(ctor));
Assert.state(paramNames != null, () -> "Cannot resolve parameter names for constructor " + ctor);

代码示例来源:origin: cbeust/testng

@DataProvider(name = "dp")
public static Object[][] createData(Constructor c) {
 Assert.assertEquals(c.getDeclaringClass(), ConstructorSample.class);
 Assert.assertNotNull(c.getAnnotation(Factory.class));
 Assert.assertEquals(c.getParameterTypes().length, 1);
 Assert.assertEquals(c.getParameterTypes()[0], String.class);
 return new Object[][] {{"Cedric"}, {"Alois"}};
}

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

@Test
public void javaLangAnnotationTypeViaFindMergedAnnotation() throws Exception {
  Constructor<?> deprecatedCtor = Date.class.getConstructor(String.class);
  assertEquals(deprecatedCtor.getAnnotation(Deprecated.class), findMergedAnnotation(deprecatedCtor, Deprecated.class));
  assertEquals(Date.class.getAnnotation(Deprecated.class), findMergedAnnotation(Date.class, Deprecated.class));
}

代码示例来源:origin: cbeust/testng

@DataProvider(name = "dp1")
public static Object[][] createData1(ConstructorOrMethod cOrM) {
 Assert.assertEquals(cOrM.getDeclaringClass(), ConstructorOrMethodSample.class);
 Assert.assertNull(cOrM.getMethod());
 Assert.assertNotNull(cOrM.getConstructor());
 Constructor c = cOrM.getConstructor();
 Assert.assertNotNull(c.getAnnotation(Factory.class));
 Assert.assertEquals(c.getParameterTypes().length, 1);
 Assert.assertEquals(c.getParameterTypes()[0], String.class);
 return new Object[][] {{"0"}, {"1"}};
}

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

@Test
 public void constructorPass() throws Exception {
  check(ImmutableValForPass.class.getConstructor(int.class).getAnnotation(B1.class)).notNull();
  check(ImmutableValForPass.class.getConstructor(int.class).getAnnotation(C1.class)).notNull();
 }
}

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

ConstructorProperties cp = ctor.getAnnotation(ConstructorProperties.class);
String[] paramNames = (cp != null ? cp.value() : parameterNameDiscoverer.getParameterNames(ctor));
Assert.state(paramNames != null, () -> "Cannot resolve parameter names for constructor " + ctor);

代码示例来源:origin: cbeust/testng

@DataProvider(name = "cookie-master")
 public static Object[][] cookies(ConstructorOrMethod method) {
  TestInfo value;
  if (method.getConstructor() != null) {
   value = (TestInfo) method.getConstructor().getAnnotation(TestInfo.class);
  } else {
   value = method.getMethod().getAnnotation(TestInfo.class);
  }
  String name = value.name();
  if ("glutton".equalsIgnoreCase(name)) {
   return new Object[][] {{"oreo", 200}};
  }
  if ("nibbler".equalsIgnoreCase(name)) {
   return new Object[][] {{"marie-gold", 10}};
  }
  return new Object[][] {
   {"oreo", 200},
   {"marie-gold", 10}
  };
 }
}

相关文章

微信公众号

最新文章

更多