Java Code Examples for java.lang.reflect.AccessibleObject#getAnnotation()

The following examples show how to use java.lang.reflect.AccessibleObject#getAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotQueryableException.java    From attic-polygene-java with Apache License 2.0 6 votes vote down vote up
/**
 * 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()
            )
        );
    }
}
 
Example 2
Source File: OWLSchemaPersistingManager.java    From anno4j with Apache License 2.0 6 votes vote down vote up
/**
 * 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>&lt;&gt;</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();
}
 
Example 3
Source File: AccessibleObjectTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: OWLSchemaPersistingManager.java    From anno4j with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Source File: EnumToStringConverter.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 6
Source File: EnumToStringConverter.java    From blade-tool with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 7
Source File: AbstractAssociationModel.java    From attic-polygene-java with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 8
Source File: CustomClassMapper.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
private static String annotatedName(AccessibleObject obj) {
  if (obj.isAnnotationPresent(PropertyName.class)) {
    PropertyName annotation = obj.getAnnotation(PropertyName.class);
    return annotation.value();
  }

  return null;
}
 
Example 9
Source File: OWLSchemaPersistingManager.java    From anno4j with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
        }
    }
}
 
Example 10
Source File: CustomClassMapper.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private static String annotatedName(AccessibleObject obj) {
  if (obj.isAnnotationPresent(PropertyName.class)) {
    PropertyName annotation = obj.getAnnotation(PropertyName.class);
    return annotation.value();
  }

  return null;
}
 
Example 11
Source File: MetamodelUtil.java    From javaee-lab with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 12
Source File: PropertyModel.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 13
Source File: ReflectionUtil.java    From uima-uimafit with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: MetamodelUtil.java    From javaee-lab with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: AccessibleProperty.java    From warpdb with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 16
Source File: MetamodelUtil.java    From javaee-lab with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 17
Source File: CallerSensitiveDetector.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
boolean isCallerSensitive(final AccessibleObject ao) {
    return ao.getAnnotation(CALLER_SENSITIVE_ANNOTATION_CLASS) != null;
}
 
Example 18
Source File: CallerSensitiveDetector.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
boolean isCallerSensitive(final AccessibleObject ao) {
    return ao.getAnnotation(CALLER_SENSITIVE_ANNOTATION_CLASS) != null;
}
 
Example 19
Source File: CallerSensitiveDetector.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
boolean isCallerSensitive(AccessibleObject ao) {
    return ao.getAnnotation(CALLER_SENSITIVE_ANNOTATION_CLASS) != null;
}
 
Example 20
Source File: CallerSensitiveDetector.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
boolean isCallerSensitive(final AccessibleObject ao) {
    return ao.getAnnotation(CALLER_SENSITIVE_ANNOTATION_CLASS) != null;
}