Skip to content

Commit

Permalink
feat: create method field metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Jul 31, 2023
1 parent 61d0aa2 commit 85e0eac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.jnosql.communication.Value;
import org.eclipse.jnosql.mapping.AttributeConverter;

import java.lang.annotation.Annotation;
import java.util.Optional;

/**
Expand Down Expand Up @@ -105,4 +106,19 @@ public interface FieldMetadata {
<X, Y, T extends AttributeConverter<X, Y>> Optional<Class<? extends AttributeConverter<X, Y>>> converter();


/**
* Retrieves the value from the default method (usually named "value") of the provided annotation type.
*
* <p>This method allows you to obtain the value from the default method, typically named "value," in the
* specified annotation type. If the field in question has this annotation or if the method with the name
* "value" exists in the annotation, this method will return an {@link java.util.Optional} containing the
* value. Otherwise, it will return an empty {@link java.util.Optional}.</p>
*
* @param type the annotation
* @return an {@link java.util.Optional} containing the value from the default method if present,
* otherwise an empty {@link Optional#empty()}
* @param <T> the annotation type
* @see java.util.Optional
*/
<T extends Annotation> Optional<String> value(Class<T> type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
*/
package org.eclipse.jnosql.mapping.reflection;

import jakarta.nosql.NoSQLException;
import org.eclipse.jnosql.communication.Value;
import org.eclipse.jnosql.mapping.AttributeConverter;
import org.eclipse.jnosql.mapping.metadata.FieldMetadata;
import org.eclipse.jnosql.mapping.metadata.MappingType;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

Expand Down Expand Up @@ -100,6 +105,27 @@ public Object value(Value value) {
return value.get(field.getType());
}


@Override
public <T extends Annotation> Optional<String> value(Class<T> type){
Objects.requireNonNull(type, "type is required");
Optional<Method> method = Arrays.stream(type.getDeclaredMethods()).filter(m -> "value".equals(m.getName()))
.findFirst();
T annotation = this.field.getAnnotation(type);
if (method.isEmpty() && annotation == null) {
return Optional.empty();
}
return method.map(m -> {
try {
Object invoke = m.invoke(annotation);
return invoke.toString();
} catch (IllegalAccessException | InvocationTargetException e) {
throw new NoSQLException("There is an issue invoking the method: " + m + " using the annotation: "
+ type, e);
}
});
}

@Override
public String toString() {
return "AbstractFieldMetadata{" +
Expand Down

0 comments on commit 85e0eac

Please sign in to comment.