下面列出了java.lang.reflect.AccessibleObject#getAnnotation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
private static AccessibleObject getAnnotation(Class<?> clazz) {
Set<AccessibleObject> accessibleObjects = new HashSet<>();
// JsonValue METHOD, FIELD
Field[] fields = clazz.getDeclaredFields();
Collections.addAll(accessibleObjects, fields);
// methods
Method[] methods = clazz.getDeclaredMethods();
Collections.addAll(accessibleObjects, methods);
for (AccessibleObject accessibleObject : accessibleObjects) {
// 复用 jackson 的 JsonValue 注解
JsonValue jsonValue = accessibleObject.getAnnotation(JsonValue.class);
if (jsonValue != null && jsonValue.value()) {
accessibleObject.setAccessible(true);
return accessibleObject;
}
}
return null;
}
public AbstractAssociationModel( AccessibleObject accessor,
ValueConstraintsInstance valueConstraintsInstance,
ValueConstraintsInstance associationConstraintsInstance,
MetaInfo metaInfo )
{
Objects.requireNonNull( accessor );
Objects.requireNonNull( metaInfo );
this.metaInfo = metaInfo;
this.constraints = valueConstraintsInstance;
this.associationConstraints = associationConstraintsInstance;
this.accessor = accessor;
this.type = GenericAssociationInfo.associationTypeOf( accessor );
this.qualifiedName = QualifiedName.fromAccessor( accessor );
this.immutable = metaInfo.get( Immutable.class ) != null;
this.aggregated = metaInfo.get( Aggregated.class ) != null;
Queryable queryable = accessor.getAnnotation( Queryable.class );
this.queryable = queryable == null || queryable.value();
}
@Nullable
private static AccessibleObject getAnnotation(Class<?> clazz) {
Set<AccessibleObject> accessibleObjects = new HashSet<>();
// JsonValue METHOD, FIELD
Field[] fields = clazz.getDeclaredFields();
Collections.addAll(accessibleObjects, fields);
// methods
Method[] methods = clazz.getDeclaredMethods();
Collections.addAll(accessibleObjects, methods);
for (AccessibleObject accessibleObject : accessibleObjects) {
// 复用 jackson 的 JsonValue 注解
JsonValue jsonValue = accessibleObject.getAnnotation(JsonValue.class);
if (jsonValue != null && jsonValue.value()) {
accessibleObject.setAccessible(true);
return accessibleObject;
}
}
return null;
}
/**
* Verify that the provided accessor method has not been marked with a Queryable(false).
*
* @param accessor accessor method
*
* @throws NotQueryableException - If accessor method has been marked as not queryable
*/
public static void throwIfNotQueryable( final AccessibleObject accessor )
{
Queryable queryable = accessor.getAnnotation( Queryable.class );
if( queryable != null && !queryable.value() )
{
throw new NotQueryableException(
String.format(
"%1$s \"%2$s\" (%3$s) is not queryable as has been marked with @Queryable(false)",
Classes.RAW_CLASS.apply( GenericPropertyInfo.propertyTypeOf( accessor ) ).getSimpleName(),
( (Member) accessor ).getName(),
( (Member) accessor ).getDeclaringClass().getName()
)
);
}
}
/**
* Persists the information that a property is restricted to have all values from a specified set of classes.
* All properties with {@link AllValuesFrom} annotation are considered.
* @param annotatedObjects The {@link Iri} annotated objects that should be considered.
* @throws RepositoryException Thrown on error regarding the connected triplestore.
*/
private void persistAllValuesFrom(Collection<AccessibleObject> annotatedObjects) throws RepositoryException {
// Get those methods and fields that have the @AllValuesFrom annotation:
Collection<AccessibleObject> allValuesFromObjects = filterObjectsWithAnnotation(annotatedObjects, AllValuesFrom.class);
for (AccessibleObject object : allValuesFromObjects) {
// Get the IRIs of the classes defined in the annotation:
AllValuesFrom allValuesFrom = object.getAnnotation(AllValuesFrom.class);
Set<String> clazzIris = getIrisFromObjects(Arrays.asList(allValuesFrom.value()));
Set<OWLClazz> clazzes = new HashSet<>();
for (String clazzIri : clazzIris) {
clazzes.add(createClazzOnDemand(clazzIri));
}
// Add the classes to the restriction:
Restriction restriction = buildRestrictionForProperty(object, null);
restriction.setAllValuesFrom(clazzes);
}
}
public void test_getAnnotation() throws Exception{
AccessibleObject ao = SubTestClass.class.getMethod("annotatedMethod");
//test error case
boolean npeThrown = false;
try {
ao.getAnnotation(null);
fail("NPE expected");
} catch (NullPointerException e) {
npeThrown = true;
}
assertTrue("NPE expected", npeThrown);
//test inherited on method has no effect
InheritedRuntime ir = ao.getAnnotation(InheritedRuntime.class);
assertNull("Inherited Annotations should have no effect", ir);
//test ordinary runtime annotation
AnnotationRuntime0 rt0 = ao.getAnnotation(AnnotationRuntime0.class);
assertNotNull("AnnotationRuntime0 instance expected", rt0);
}
/**
* Constructs a SPARQL VALUES clause for successive binding of the given objects {@link Iri} mappings,
* to the SPARQL variable <code>binding</code>.
* The IRIs of the resources are enclosed in <code><></code> brackets.
* @param objects The values to successively bind.
* @param binding The name of the binding without a leading <code>"?"</code>.
* @return Returns a SPARQL VALUES clause with the given resources and binding.
*/
private String buildValuesClause(Collection<AccessibleObject> objects, String binding) {
StringBuilder clause = new StringBuilder("VALUES ?")
.append(binding)
.append(" {");
for (AccessibleObject object : objects) {
if(object.isAnnotationPresent(Iri.class)) {
Iri iri = object.getAnnotation(Iri.class);
clause.append(" <")
.append(iri.value())
.append("> ");
}
}
clause.append("}");
return clause.toString();
}
/**
* Retrieves cascade from metamodel attribute on a xToMany relation.
*
* @param attribute given singular attribute
* @return an empty collection if no jpa relation annotation can be found.
*/
public Collection<CascadeType> getCascades(SingularAttribute<?, ?> attribute) {
if (attribute.getJavaMember() instanceof AccessibleObject) {
AccessibleObject accessibleObject = (AccessibleObject) attribute.getJavaMember();
OneToOne oneToOne = accessibleObject.getAnnotation(OneToOne.class);
if (oneToOne != null) {
return newArrayList(oneToOne.cascade());
}
ManyToOne manyToOne = accessibleObject.getAnnotation(ManyToOne.class);
if (manyToOne != null) {
return newArrayList(manyToOne.cascade());
}
}
return newArrayList();
}
/**
* Retrieves cascade from metamodel attribute
*
* @param attribute given pluaral attribute
* @return an empty collection if no jpa relation annotation can be found.
*/
public Collection<CascadeType> getCascades(PluralAttribute<?, ?, ?> attribute) {
if (attribute.getJavaMember() instanceof AccessibleObject) {
AccessibleObject accessibleObject = (AccessibleObject) attribute.getJavaMember();
OneToMany oneToMany = accessibleObject.getAnnotation(OneToMany.class);
if (oneToMany != null) {
return newArrayList(oneToMany.cascade());
}
ManyToMany manyToMany = accessibleObject.getAnnotation(ManyToMany.class);
if (manyToMany != null) {
return newArrayList(manyToMany.cascade());
}
}
return newArrayList();
}
private static String annotatedName(AccessibleObject obj) {
if (obj.isAnnotationPresent(PropertyName.class)) {
PropertyName annotation = obj.getAnnotation(PropertyName.class);
return annotation.value();
}
return null;
}
/**
* Persists the information that a property is a subproperty of another to the default graph of the connected triplestore.
* All properties with {@link SubPropertyOf} annotation are considered.
* @param annotatedObjects The {@link Iri} annotated objects that should be considered.
* @throws RepositoryException Thrown on error regarding the connected triplestore.
* @throws UpdateExecutionException Thrown if an error occurred while executing the update.
*/
private void persistSubPropertyOf(Collection<AccessibleObject> annotatedObjects) throws RepositoryException, UpdateExecutionException {
// Get those methods and fields that have the @Transitive annotation:
Collection<AccessibleObject> subPropertyObjects = filterObjectsWithAnnotation(annotatedObjects, SubPropertyOf.class);
for (AccessibleObject object : subPropertyObjects) {
String iri = getIriFromObject(object);
SubPropertyOf subPropertyAnnotation = object.getAnnotation(SubPropertyOf.class);
StringBuilder query = new StringBuilder(QUERY_PREFIX)
.append(" INSERT DATA { ");
for (String superPropertyIri : subPropertyAnnotation.value()) {
query.append("<").append(iri).append("> ")
.append("<").append(RDFS.SUB_PROPERTY_OF).append("> ")
.append("<").append(superPropertyIri).append("> . ");
}
query.append("}");
// Prepare the update query and execute it:
try {
Update update = getConnection().prepareUpdate(query.toString());
update.execute();
} catch (MalformedQueryException e) {
throw new UpdateExecutionException();
}
}
}
private static String annotatedName(AccessibleObject obj) {
if (obj.isAnnotationPresent(PropertyName.class)) {
PropertyName annotation = obj.getAnnotation(PropertyName.class);
return annotation.value();
}
return null;
}
public PropertyModel( AccessibleObject accessor,
boolean immutable,
boolean useDefaults,
ValueConstraintsInstance constraints,
MetaInfo metaInfo,
Object initialValue
)
{
if( accessor instanceof Method )
{
Method m = (Method) accessor;
if( !m.getReturnType().equals( Property.class ) )
{
throw new InvalidPropertyTypeException( accessor );
}
}
this.immutable = immutable;
this.metaInfo = metaInfo;
this.accessor = accessor;
type = GenericPropertyInfo.propertyTypeOf( accessor );
checkTypeValidity( type );
qualifiedName = QualifiedName.fromAccessor( accessor );
initialValueProvider = new DefaultInitialValueProvider( useDefaults, initialValue );
this.constraints = constraints;
final Queryable queryable = accessor.getAnnotation( Queryable.class );
this.queryable = queryable == null || queryable.value();
}
/**
* Equivalent to {@link AccessibleObject#isAnnotationPresent(Class)} but handles uimaFIT legacy
* annotations.
*
* @param aObject
* the object to analyze
* @param aAnnotationClass
* the annotation to check for
* @return whether the annotation is present
*/
public static boolean isAnnotationPresent(AccessibleObject aObject,
Class<? extends Annotation> aAnnotationClass) {
// First check if the desired annotation is present
// UIMA-3853 workaround for IBM Java 8 beta 3
if (aObject.getAnnotation(aAnnotationClass) != null) {
return true;
}
// If not present, check if an equivalent legacy annotation is present
return LegacySupport.getInstance().isAnnotationPresent(aObject, aAnnotationClass);
}
public boolean isOrphanRemoval(PluralAttribute<?, ?, ?> attribute) {
if (attribute.getJavaMember() instanceof AccessibleObject) {
AccessibleObject accessibleObject = (AccessibleObject) attribute.getJavaMember();
OneToMany oneToMany = accessibleObject.getAnnotation(OneToMany.class);
if (oneToMany != null) {
return oneToMany.orphanRemoval();
}
}
return true;
}
private static String getColumnName(AccessibleObject ao, String defaultName) {
Column col = ao.getAnnotation(Column.class);
if (col == null || col.name().isEmpty()) {
return defaultName;
}
return col.name();
}
@Override
boolean isCallerSensitive(final AccessibleObject ao) {
return ao.getAnnotation(CALLER_SENSITIVE_ANNOTATION_CLASS) != null;
}
@Override
boolean isCallerSensitive(final AccessibleObject ao) {
return ao.getAnnotation(CALLER_SENSITIVE_ANNOTATION_CLASS) != null;
}
@Override
boolean isCallerSensitive(final AccessibleObject ao) {
return ao.getAnnotation(CALLER_SENSITIVE_ANNOTATION_CLASS) != null;
}
@Override
boolean isCallerSensitive(AccessibleObject ao) {
return ao.getAnnotation(CALLER_SENSITIVE_ANNOTATION_CLASS) != null;
}