本文整理匯總了Java中java.lang.reflect.AnnotatedElement類的典型用法代碼示例。如果您正苦於以下問題:Java AnnotatedElement類的具體用法?Java AnnotatedElement怎麽用?Java AnnotatedElement使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AnnotatedElement類屬於java.lang.reflect包,在下文中一共展示了AnnotatedElement類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getName
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
/**
* Get display name of specified element from name parameter of {@link Editable} annotation.
* If the annotation is not defined or name parameter is not available in the annotation, the
* element name itself will be transferred to non-camel case and returned.
*
* @param element
* annotated element to get name from
* @return
* display name of the element
*/
public static String getName(AnnotatedElement element) {
Editable editable = element.getAnnotation(Editable.class);
if (editable != null && editable.name().trim().length() != 0)
return editable.name();
else if (element instanceof Class)
return WordUtils.uncamel(((Class<?>)element).getSimpleName());
else if (element instanceof Field)
return WordUtils.uncamel(WordUtils.capitalize(((Field)element).getName()));
else if (element instanceof Method)
return StringUtils.substringAfter(WordUtils.uncamel(((Method)element).getName()), " ");
else if (element instanceof Package)
return ((Package)element).getName();
else
throw new GeneralException("Invalid element type: " + element.getClass().getName());
}
示例2: addInjectorsForMembers
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
private static <M extends Member & AnnotatedElement> void addInjectorsForMembers(
TypeLiteral<?> typeLiteral, Factory<M> factory, boolean statics,
Collection<InjectionPoint> injectionPoints, Errors errors) {
for (M member : factory.getMembers(getRawType(typeLiteral.getType()))) {
if (isStatic(member) != statics) {
continue;
}
Inject inject = member.getAnnotation(Inject.class);
if (inject == null) {
continue;
}
try {
injectionPoints.add(factory.create(typeLiteral, member, errors));
} catch (ConfigurationException ignorable) {
if (!inject.optional()) {
errors.merge(ignorable.getErrorMessages());
}
}
}
}
示例3: getNameFromMemberAnnotation
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
private static String getNameFromMemberAnnotation(final AnnotatedElement field) {
final String result;
if (field.isAnnotationPresent(Attribute.class)) {
result = Utils.name(field.getAnnotation(Attribute.class).name(), field);
} else if (field.isAnnotationPresent(Element.class)) {
result = Utils.name(field.getAnnotation(Element.class).name(), field);
} else if (field.isAnnotationPresent(ElementList.class)) {
result = Utils.name(field.getAnnotation(ElementList.class).name(), field);
} else if (field.isAnnotationPresent(ElementMap.class)) {
result = Utils.name(field.getAnnotation(ElementMap.class).name(), field);
} else if (field.isAnnotationPresent(Text.class)) {
result = "**text**";
} else {
result = null;
}
return result;
}
示例4: getTesterAnnotations
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
/**
* Find all the tester annotations declared on a tester class or method.
* @param classOrMethod a class or method whose tester annotations to find
* @return an iterable sequence of tester annotations on the class
*/
public static Iterable<Annotation> getTesterAnnotations(AnnotatedElement classOrMethod) {
synchronized (annotationCache) {
List<Annotation> annotations = annotationCache.get(classOrMethod);
if (annotations == null) {
annotations = new ArrayList<Annotation>();
for (Annotation a : classOrMethod.getDeclaredAnnotations()) {
if (a.annotationType().isAnnotationPresent(TesterAnnotation.class)) {
annotations.add(a);
}
}
annotations = Collections.unmodifiableList(annotations);
annotationCache.put(classOrMethod, annotations);
}
return annotations;
}
}
示例5: getSourceId
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
private SourceId getSourceId(AnnotatedElement element) {
SourceId sourceId = null;
Optional<Annotation> sourceAnnotation = Stream.of(element.getAnnotations())
.filter(a -> a.annotationType().isAnnotationPresent(RegisteredSourceIdProducer.class)).findFirst();
if (sourceAnnotation.isPresent()) {
RegisteredSourceIdProducer sourceIdProviderAnnotation = sourceAnnotation.get().annotationType()
.getAnnotation(RegisteredSourceIdProducer.class);
try {
SourceIdProducer sourceIdProducer = sourceIdProviderAnnotation.value().newInstance();
sourceId = sourceIdProducer.get(element, sourceAnnotation.get());
} catch (Exception e) {
throw new QueryProxyException("Problem with sourceId aquiring: can't find suitable constructor " + e,
e);
}
}
return sourceId;
}
示例6: getReferenceColumnNamesMapForReferenceAttribute
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
private Map<String, String> getReferenceColumnNamesMapForReferenceAttribute(SingularAttribute<?, ?> attribute, ManagedType<?> targetEntity) {
List<String> idAttributeNames = targetEntity.getSingularAttributes().stream()
.filter(this::isIdAttribute)
.map(this::getSingularAttributeColumnName)
.collect(Collectors.toList());
JoinColumns joinColumnsAnnotation =
((AnnotatedElement) attribute.getJavaMember()).getAnnotation(JoinColumns.class);
JoinColumn joinColumnAnnotation =
((AnnotatedElement) attribute.getJavaMember()).getAnnotation(JoinColumn.class);
JoinColumn[] joinColumns = joinColumnsAnnotation != null ? joinColumnsAnnotation.value() :
joinColumnAnnotation != null ? new JoinColumn[]{joinColumnAnnotation} : null;
Map<String, String> referenceColumnNamesMap;
if (joinColumns != null) {
referenceColumnNamesMap = Arrays.stream(joinColumns)
.collect(Collectors.toMap(JoinColumn::name, joinColumn ->
joinColumn.referencedColumnName().length() > 0 ? joinColumn.referencedColumnName() :
idAttributeNames.get(0)));
} else {
referenceColumnNamesMap = idAttributeNames.stream()
.collect(Collectors.toMap(idAttributeName -> attribute.getName().toUpperCase() + "_"
+ idAttributeName, idAttributeName -> idAttributeName));
}
return referenceColumnNamesMap;
}
示例7: EjbRefElement
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
public EjbRefElement(Member member, PropertyDescriptor pd) {
super(member, pd);
AnnotatedElement ae = (AnnotatedElement) member;
EJB resource = ae.getAnnotation(EJB.class);
String resourceBeanName = resource.beanName();
String resourceName = resource.name();
this.isDefaultName = !StringUtils.hasLength(resourceName);
if (this.isDefaultName) {
resourceName = this.member.getName();
if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) {
resourceName = Introspector.decapitalize(resourceName.substring(3));
}
}
Class<?> resourceType = resource.beanInterface();
if (resourceType != null && !Object.class.equals(resourceType)) {
checkResourceType(resourceType);
}
else {
// No resource type specified... check field/method.
resourceType = getResourceType();
}
this.beanName = resourceBeanName;
this.name = resourceName;
this.lookupType = resourceType;
this.mappedName = resource.mappedName();
}
示例8: getAnnotationValue
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
/**
* Utility method for calling an arbitrary method in an annotation.
*
* @param element the element that was annotated, either a class or method
* @param annotation the class of the annotation we're interested in
* @param methodName the name of the method in the annotation we wish
* to call.
* @param defaultValue the value to return if the annotation doesn't
* exist, or we couldn't invoke the method for some reason.
* @return the result of calling the annotation method, or the default.
*/
protected static Object getAnnotationValue(
AnnotatedElement element, Class<? extends Annotation> annotation,
String methodName, Object defaultValue) {
Object ret = defaultValue;
try {
Method m = annotation.getMethod(methodName);
Annotation a = element.getAnnotation(annotation);
ret = m.invoke(a);
} catch (NoSuchMethodException e) {
assert false;
} catch (IllegalAccessException e) {
assert false;
} catch (InvocationTargetException e) {
assert false;
} catch (NullPointerException e) {
assert false;
}
return ret;
}
示例9: findAnnotations
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
/**
* Find the annotations of given <code>annotationType</code> on given element and stores them in given
* <code>accumulator</code>.
* @param accumulator Accumulator
* @param element Annotated element
* @param annotationType Annotation type to lookup
* @param repeatableContainerType Optional repeteable annotation type
*/
private static <A extends Annotation> void findAnnotations(List<A> accumulator, AnnotatedElement element,
Class<A> annotationType, Class<? extends Annotation> repeatableContainerType) {
// direct lookup
A[] as = element.getAnnotationsByType(annotationType);
if (as.length > 0) {
for (A a : as) {
accumulator.add(a);
}
}
// check meta-annotations
Annotation[] all = element.getAnnotations();
if (all.length > 0) {
for (Annotation annotation : all) {
if (!isInJavaLangAnnotationPackage(annotation) && !annotation.annotationType().equals(annotationType)
&& (repeatableContainerType == null
|| !annotation.annotationType().equals(repeatableContainerType))) {
findAnnotations(accumulator, annotation.annotationType(), annotationType, repeatableContainerType);
}
}
}
}
示例10: Annotations
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
public Annotations(AnnotatedElement... elements) {
for (AnnotatedElement element : elements) {
for (Annotation annotation : element.getAnnotations()) {
annotations.put(annotation.annotationType(), annotation);
}
}
}
示例11: getAnnotatedElement
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
public static AnnotatedElement getAnnotatedElement(Class<?> beanClass, String propertyName, Class<?> propertyClass) {
Field field = getFieldOrNull(beanClass, propertyName);
Method method = getGetterOrNull(beanClass, propertyName, propertyClass);
if (field == null || field.getAnnotations().length == 0) {
return (method != null && method.getAnnotations().length > 0) ? method : method;
} else if (method == null || method.getAnnotations().length == 0) {
return field;
} else {
//return new Annotations(field, method);
return null;
}
}
示例12: resolveAnnotation
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
private Tracked resolveAnnotation(InvocationContext context) {
Function<AnnotatedElement, Tracked> extractor = c -> c.getAnnotation(Tracked.class);
Method method = context.getMethod();
Tracked tracked = extractor.apply(method);
return tracked != null ? tracked : extractor.apply(method.getDeclaringClass());
}
開發者ID:PacktPublishing,項目名稱:Architecting-Modern-Java-EE-Applications,代碼行數:8,代碼來源:TrackingInterceptor.java
示例13: getAnnotations
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
/**
* Get all the annotations of given <code>annotationType</code> present in given <code>element</code>, including any
* meta-annotation and supporting repeatable annotations.
* @param <A> Annotation type
* @param element Annotated element to inspect (not null)
* @param annotationType Annotation type to lookup
* @return List of detected annotation of given <code>annotationType</code>, an empty List if none found
*/
public static <A extends Annotation> List<A> getAnnotations(AnnotatedElement element, Class<A> annotationType) {
ObjectUtils.argumentNotNull(element, "AnnotatedElement must be not null");
ObjectUtils.argumentNotNull(annotationType, "Annotation type must be not null");
Class<? extends Annotation> repeatableContainerType = null;
if (annotationType.isAnnotationPresent(Repeatable.class)) {
repeatableContainerType = annotationType.getAnnotation(Repeatable.class).value();
}
List<A> annotations = new LinkedList<>();
findAnnotations(annotations, element, annotationType, repeatableContainerType);
return annotations;
}
示例14: createFor
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
@Override
Object createFor(AnnotatedElement annotatedElement, Class<?> targetType, Fairy fairy) {
IntegerWith config = findAnnotation(annotatedElement, IntegerWith.class).orElse(null);
int min = minValue(config);
int max = maxValue(config);
BaseProducer producer = fairy.baseProducer();
return producer.randomBetween(min, max);
}
示例15: getConverters
import java.lang.reflect.AnnotatedElement; //導入依賴的package包/類
private List<QueryConverter> getConverters(AnnotatedElement element) {
List<QueryConverter> annotatedConverters = new ArrayList<>(2);
List<Annotation> converterAnnotations = Stream.of(element.getAnnotations())
.filter(a -> a.annotationType().isAnnotationPresent(Converter.class)
|| converters.containsKey(a.annotationType()))
.collect(Collectors.toList());
if (!converterAnnotations.isEmpty()) {
for (Annotation converterAnnotation : converterAnnotations) {
QueryConverterFactory factory;
if (converters.containsKey(converterAnnotation.annotationType())) {
factory = converters.get(converterAnnotation.annotationType());
} else {
Converter converter = converterAnnotation.annotationType().getAnnotation(Converter.class);
Class<? extends QueryConverterFactory> converterFactoryClass = converter.value();
if (Converter.DEFAULT.class.isAssignableFrom(converterFactoryClass)) {
throw new QueryProxyException("Factory is not registered for annotation "
+ converterAnnotation.annotationType().getName() + "! "
+ "Set static converter using @Converter annotation value or use Queries.Builder.converter() method to register factory instance!");
}
try {
factory = converterFactoryClass.newInstance();
} catch (Exception e) {
throw new QueryProxyException(
"Problem instantiating query converter factory class with no argument constructor " + e,
e);
}
}
annotatedConverters.add(factory.get(converterAnnotation));
}
}
return annotatedConverters;
}