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

The following are Jave code examples for showing how to use getDeclaredAnnotation() 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: NyaaCore   File: ColumnStructure.java   View Source Code Vote up 6 votes
/**
 * Constructor for field based table columns
 */
public ColumnStructure(TableStructure table, Field dataField, DataColumn anno) {
    if (anno == null) throw new IllegalArgumentException();
    this.table = table;
    String name = anno.value();
    if ("".equals(name)) name = dataField.getName();
    this.name = name;
    this.isPrimary = dataField.getDeclaredAnnotation(PrimaryKey.class) != null;
    dataField.setAccessible(true);
    field = dataField;
    fieldType = field.getType();
    setter = null;
    getter = null;
    fieldParser = getParserMethod(dataField.getType());
    if (fieldParser == null) {
        accessMethod = ColumnAccessMethod.FIELD;
        columnType = ColumnType.getType(ColumnAccessMethod.FIELD, field.getType());
    } else {
        accessMethod = ColumnAccessMethod.FIELD_PARSE;
        columnType = ColumnType.getType(ColumnAccessMethod.FIELD_PARSE, field.getType());
    }
}
 
Example 2
Project: FApkSigner   File: Asn1BerParser.java   View Source Code Vote up 5 votes
private static List<AnnotatedField> getAnnotatedFields(Class<?> containerClass)
        throws Asn1DecodingException {
    Field[] declaredFields = containerClass.getDeclaredFields();
    List<AnnotatedField> result = new ArrayList<>(declaredFields.length);
    for (Field field : declaredFields) {
        Asn1Field annotation = field.getDeclaredAnnotation(Asn1Field.class);
        if (annotation == null) {
            continue;
        }
        if (Modifier.isStatic(field.getModifiers())) {
            throw new Asn1DecodingException(
                    Asn1Field.class.getName() + " used on a static field: "
                            + containerClass.getName() + "." + field.getName());
        }

        AnnotatedField annotatedField;
        try {
            annotatedField = new AnnotatedField(field, annotation);
        } catch (Asn1DecodingException e) {
            throw new Asn1DecodingException(
                    "Invalid ASN.1 annotation on "
                            + containerClass.getName() + "." + field.getName(),
                    e);
        }
        result.add(annotatedField);
    }
    return result;
}
 
Example 3
Project: FApkSigner   File: Asn1DerEncoder.java   View Source Code Vote up 5 votes
private static List<AnnotatedField> getAnnotatedFields(Object container)
        throws Asn1EncodingException {
    Class<?> containerClass = container.getClass();
    Field[] declaredFields = containerClass.getDeclaredFields();
    List<AnnotatedField> result = new ArrayList<>(declaredFields.length);
    for (Field field : declaredFields) {
        Asn1Field annotation = field.getDeclaredAnnotation(Asn1Field.class);
        if (annotation == null) {
            continue;
        }
        if (Modifier.isStatic(field.getModifiers())) {
            throw new Asn1EncodingException(
                    Asn1Field.class.getName() + " used on a static field: "
                            + containerClass.getName() + "." + field.getName());
        }

        AnnotatedField annotatedField;
        try {
            annotatedField = new AnnotatedField(container, field, annotation);
        } catch (Asn1EncodingException e) {
            throw new Asn1EncodingException(
                    "Invalid ASN.1 annotation on "
                            + containerClass.getName() + "." + field.getName(),
                    e);
        }
        result.add(annotatedField);
    }
    return result;
}
 
Example 4
Project: spring-boot-gae   File: FieldSearchFieldMetadata.java   View Source Code Vote up 5 votes
/**
 * Create a new instance.
 *
 * @param entityType        Entity class.
 * @param field             For this field.
 * @param indexTypeRegistry Determine the index type with this lookup.
 */
public FieldSearchFieldMetadata(Class<?> entityType, Field field, IndexTypeRegistry indexTypeRegistry) {
    this.entityType = entityType;
    this.field = field;
    this.indexName = NAME_CALCULATOR.apply(field);
    this.encodedName = NAME_ENCODER.apply(indexName);

    SearchIndex annotation = field.getDeclaredAnnotation(SearchIndex.class);
    if (annotation == null || annotation.type() == IndexType.AUTO) {
        this.indexType = indexTypeRegistry.apply(field.getGenericType());
    } else {
        this.indexType = annotation.type();
    }
}
 
Example 5
Project: FoodCraft-Reloaded   File: PropertiedFoodLoader.java   View Source Code Vote up 5 votes
@Load
public void registerFoods() {
    for (Field field : FCRFoods.class.getFields()) {
        field.setAccessible(true);
        try {
            RegFood anno = field.getDeclaredAnnotation(RegFood.class);
            if (anno == null) {
                Object food = field.get(null);
                if (food instanceof ItemPorridge)
                    registerPorridge((ItemPorridge) food);
                else if (food instanceof ItemSoup)
                    registerSoup((ItemSoup) food);
                else if (food instanceof ItemNoodles)
                    registerNoodles((ItemNoodles) food);
                else if (food instanceof ItemLiqueur)
                    registerLiqueur((ItemLiqueur) food);
                continue;
            }

            ItemPFood item = (ItemPFood) field.get(null);
            if (ArrayUtils.isNotEmpty(anno.modifier()))
                item.setProperties(anno.modifier());
            if (anno.amount() == Integer.MIN_VALUE)
                item.calcHealAmount();
            else
                item.setHealAmount(anno.amount());
            ForgeRegistries.ITEMS.register(item.setRegistryName(FoodCraftReloaded.MODID, NameBuilder.buildRegistryName(anno.name())).setUnlocalizedName(NameBuilder.buildUnlocalizedName(anno.name())));
            Arrays.asList(anno.oreDict()).forEach(s -> OreDictionary.registerOre(s, item));
            OreDictionary.registerOre("listAllfoods", item);
        } catch (IllegalAccessException | NullPointerException e) {
            FoodCraftReloaded.getLogger().warn("Un-able to register food " + field.toGenericString(), e);
        }
    }
}
 
Example 6
Project: soundwave   File: ObjectAdapter.java   View Source Code Vote up 4 votes
public static <T1, T2> T2 getObject(T1 object, Class<T2> adaptedClass) {

    try {
      Map<String, Field> objectFieldsMap = getAllFields(object.getClass());

      T2 adaptedObject = (T2) ConstructorUtils.invokeConstructor(adaptedClass, null);
      List<String> target = getFields(adaptedClass);
      for (String field : target) {

        // get The field of the adapted object
        Field targetField = adaptedClass.getDeclaredField(field);
        targetField.setAccessible(true);

        if (targetField.isAnnotationPresent(NestedField.class)) {

          NestedField annotation = targetField.getDeclaredAnnotation(NestedField.class);
          String[] hierarchy = StringUtils.split(annotation.src(), ".");
          Field nestedField = objectFieldsMap.get(hierarchy[0]);
          nestedField.setAccessible(true);
          Object fieldValue = nestedField.get(object);

          for (int i = 1; i < hierarchy.length; i++) {
            nestedField = nestedField.getType().getDeclaredField(hierarchy[i]);
            nestedField.setAccessible(true);
            fieldValue = nestedField.get(fieldValue);
          }

          // Set the last level value from hierarchy
          targetField.set(adaptedObject, fieldValue);

        } else {

          // Non nested field process as normal
          Field sourceField;
          if (targetField.isAnnotationPresent(StringDate.class)) {

            // Process date fields
            sourceField = objectFieldsMap.get(field);
            sourceField.setAccessible(true);

            if (sourceField.get(object) != null) {

              // Value is not null
              DateTime time = new DateTime(sourceField.get(object), DateTimeZone.UTC);
              targetField.set(adaptedObject, time.toString());
            } else {

              targetField.set(adaptedObject, "");
            }


          } else if (targetField.isAnnotationPresent(IgnoreAdaptation.class)) {
            // Leave field as it is. no processing.
          } else {

            sourceField = objectFieldsMap.get(field);
            sourceField.setAccessible(true);

            targetField.set(adaptedObject, sourceField.get(object));
          }
        }
      }

      return adaptedObject;

    } catch (Exception e) {
      logger.error(ExceptionUtils.getRootCauseMessage(e));
      return null;
    }


  }
 
Example 7
Project: rdf2x   File: ReadmeUtils.java   View Source Code Vote up 4 votes
private static void printConfig(Class configClass) throws IllegalAccessException, InstantiationException {
    Field[] fields = configClass.getDeclaredFields();
    System.out.println();
    System.out.println("### " + configClass.getSimpleName());
    System.out.println();
    Object defaultConfig = configClass.newInstance();

    System.out.println("|Name|Default|Description|");
    System.out.println("|---|---|---|");
    try {
        for (Field field : fields) {
            field.setAccessible(true);
            StringBuilder sb = new StringBuilder();
            sb.append("|");
            Parameter param = field.getDeclaredAnnotation(Parameter.class);

            if (param != null) {
                String names = Stream.of(param.names())
                        .collect(Collectors.joining(", "));
                // name
                sb.append(names).append("|");

                // default
                sb.append(param.required() ? "**required**" : field.get(defaultConfig) + " ").append("|");

                // description
                sb.append(param.description()).append("|");

                System.out.println(sb.toString());
            }

            ParametersDelegate delegate = field.getDeclaredAnnotation(ParametersDelegate.class);

            if (delegate != null) {
                printConfig(field.getType());
            }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}