Java Code Examples for java.lang.reflect.Field.getAnnotationsByType()

The following are Jave code examples for showing how to use getAnnotationsByType() of the java.lang.reflect.Field class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: gaffer-doc   File: OperationExample.java   View Source Code Vote up 6 votes
protected void printRequiredFields() {
    print("## Required fields");
    boolean hasRequiredFields = false;
    for (final Field field : getClassForExample().getDeclaredFields()) {
        final Required[] annotations = field.getAnnotationsByType(Required.class);
        if (null != annotations && annotations.length > 0) {
            if (!hasRequiredFields) {
                hasRequiredFields = true;
                print("The following fields are required: ");
            }
            final String name = field.getName();
            print("- " + name);
        }
    }

    if (!hasRequiredFields) {
        print("No required fields");
    }

    print("\n");
}
 
Example 2
Project: L2J-Global   File: SystemMessageId.java   View Source Code Vote up 6 votes
private static void buildFastLookupTable()
{
	final Field[] fields = SystemMessageId.class.getDeclaredFields();
	
	for (Field field : fields)
	{
		final int mod = field.getModifiers();
		if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && field.getType().equals(SystemMessageId.class) && field.isAnnotationPresent(ClientString.class))
		{
			try
			{
				final ClientString annotation = field.getAnnotationsByType(ClientString.class)[0];
				final SystemMessageId smId = new SystemMessageId(annotation.id());
				smId.setName(annotation.message());
				smId.setParamCount(parseMessageParameters(field.getName()));
				field.set(null, smId);
				VALUES.put(smId.getId(), smId);
			}
			catch (Exception e)
			{
				LOGGER.log(Level.WARNING, "SystemMessageId: Failed field access for '" + field.getName() + "'", e);
			}
		}
	}
}
 
Example 3
Project: L2J-Global   File: NpcStringId.java   View Source Code Vote up 6 votes
private static void buildFastLookupTable()
{
	final Field[] fields = NpcStringId.class.getDeclaredFields();
	
	for (Field field : fields)
	{
		final int mod = field.getModifiers();
		if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && field.getType().equals(NpcStringId.class) && field.isAnnotationPresent(ClientString.class))
		{
			try
			{
				final ClientString annotation = field.getAnnotationsByType(ClientString.class)[0];
				final NpcStringId nsId = new NpcStringId(annotation.id());
				nsId.setName(annotation.message());
				nsId.setParamCount(parseMessageParameters(field.getName()));
				field.set(null, nsId);
				VALUES.put(nsId.getId(), nsId);
			}
			catch (Exception e)
			{
				_log.log(Level.WARNING, "NpcStringId: Failed field access for '" + field.getName() + "'", e);
			}
		}
	}
}
 
Example 4
Project: OperatieBRP   File: ALaagAfleidingsUtil.java   View Source Code Vote up 6 votes
private static IndicatieActueelEnGeldigSetter bepaalIndicatieActueelEnGeldigSetter(final Afleidbaar entiteit, final Field historieSetField)
        throws ReflectiveOperationException {
    final IndicatieActueelEnGeldig[] annotationsByType = historieSetField.getAnnotationsByType(IndicatieActueelEnGeldig.class);
    if (annotationsByType.length > 0) {
        final Field indicatieAgField = entiteit.getClass().getDeclaredField(annotationsByType[0].naam());
        indicatieAgField.setAccessible(true);
        return indicatieActueelEnGeldig -> {
            try {
                indicatieAgField.set(entiteit, indicatieActueelEnGeldig);
            } catch (final ReflectiveOperationException e) {
                throw new IllegalStateException("Er is iets mis gegaan tijdens afleiden A-laag (bepalen veld indicatie actueel en geldig)", e);
            }
        };
    }
    return null;
}
 
Example 5
Project: strongbox   File: GenericFile.java   View Source Code Vote up 6 votes
private void buildMappings() {
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        Attribute[] attributes = field.getAnnotationsByType(Attribute.class);
        PartitionKey[] partitionKey = field.getAnnotationsByType(PartitionKey.class);
        SortKey[] sortKey = field.getAnnotationsByType(SortKey.class);

        if (attributes.length > 0) {
            fieldNames.add(field.getName());
        }

        if (partitionKey.length > 0) {
            fieldNames.add(field.getName());
            padding.put(field.getName(), partitionKey[0].padding());
        }

        if (sortKey.length > 0) {
            fieldNames.add(field.getName());
        }
    }
}
 
Example 6
Project: easyMvvmFx   File: ViewLoaderReflectionUtils.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
static Object injectScopeIntoField(Field scopeField, Object viewModel, ContextImpl context)
        throws IllegalAccessException {
    
    Class<? extends Scope> scopeType = (Class<? extends Scope>) scopeField.getType();

    // FIXME

    final InjectScope[] annotations = scopeField.getAnnotationsByType(InjectScope.class);

    if (annotations.length != 1) {
        throw new RuntimeException("A field to inject a Scope into should have exactly one @InjectScope annotation "
                + "but the viewModel <" + viewModel + "> has a field that violates this rule.");
    }

    Object newScope = context.getScope(scopeType);

    if (newScope == null) {
        // TODO Modify Stacktrace to get the Injectionpoint of the Scope
        throw new IllegalStateException(
                "A scope was requested but no @ScopeProvider found in the hirarchy. Declare it like this: @ScopeProvider("
                        + scopeType.getName() + ")");
    }

    if (!newScope.getClass().equals(scopeType)) {
        throw new IllegalStateException("something went wrong...");
    }

    scopeField.set(viewModel, newScope);

    return newScope;
}
 
Example 7
Project: javafx-qiniu-tinypng-client   File: ViewLoaderReflectionUtils.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
static Object injectScopeIntoField(Field scopeField, Object viewModel, ContextImpl context)
        throws IllegalAccessException {
    
    Class<? extends Scope> scopeType = (Class<? extends Scope>) scopeField.getType();

    // FIXME

    final InjectScope[] annotations = scopeField.getAnnotationsByType(InjectScope.class);

    if (annotations.length != 1) {
        throw new RuntimeException("A field to inject a Scope into should have exactly one @InjectScope annotation "
                + "but the viewModel <" + viewModel + "> has a field that violates this rule.");
    }

    Object newScope = context.getScope(scopeType);

    if (newScope == null) {
        // TODO Modify Stacktrace to get the Injectionpoint of the Scope
        throw new IllegalStateException(
                "A scope was requested but no @ScopeProvider found in the hirarchy. Declare it like this: @ScopeProvider("
                        + scopeType.getName() + ")");
    }

    if (!newScope.getClass().equals(scopeType)) {
        throw new IllegalStateException("something went wrong...");
    }

    scopeField.set(viewModel, newScope);

    return newScope;
}
 
Example 8
Project: strongbox   File: GenericFile.java   View Source Code Vote up 5 votes
private Primary getPartitionKey(Entry entry) {
    Field[] fields = entry.getClass().getDeclaredFields();
    for (Field field : fields) {
        try {
            PartitionKey[] partitionKey = field.getAnnotationsByType(PartitionKey.class);
            if (partitionKey.length > 0) {
                return (Primary) field.get(entry);
            }
        } catch (IllegalAccessException e) {
            throw new FieldAccessException(field.getName(), entry.getClass().getName(), e);
        }
    }
    throw new NoFieldMatchingAnnotationException(PartitionKey.class.getName(), entry.getClass().getName());
}
 
Example 9
Project: strongbox   File: GenericFile.java   View Source Code Vote up 5 votes
private Secondary getSortKey(Entry entry) {
    Field[] fields = entry.getClass().getDeclaredFields();
    for (Field field : fields) {
        try {
            SortKey[] sortKey = field.getAnnotationsByType(SortKey.class);
            if (sortKey.length > 0) {
                return (Secondary) field.get(entry);
            }
        } catch (IllegalAccessException e) {
            throw new FieldAccessException(field.getName(), entry.getClass().getName(), e);
        }
    }
    throw new NoFieldMatchingAnnotationException(SortKey.class.getName(), entry.getClass().getName());
}
 
Example 10
Project: OperatieBRP   File: ALaagAfleidingsUtil.java   View Source Code Vote up 4 votes
private static boolean isVeldNullable(final Field rootEntiteitField) {
    final Column[] columnAnnotation = rootEntiteitField.getAnnotationsByType(Column.class);
    final JoinColumn[] joinColumnAnnotation = rootEntiteitField.getAnnotationsByType(JoinColumn.class);
    return columnAnnotation.length > 0 && columnAnnotation[0].nullable() || joinColumnAnnotation.length > 0 && joinColumnAnnotation[0].nullable();
}