com.github.spjoe.BeanPropertyAnnotationCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of annotation-cache Show documentation
Show all versions of annotation-cache Show documentation
A library which provides caching for annotation on getters of java bean properties.
It also creates byte code (via reflectAsm) to call getter and setter of the annotated java bean property
/*
Copyright 2016 Camillo Dell'mour
Licensed under the Apache License,Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.github.spjoe;
import com.github.spjoe.getter.*;
import com.github.spjoe.setter.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class BeanPropertyAnnotationCache {
private final Map, Optional> annotations = new ConcurrentHashMap();
private final Map, GetterCall > getters = new ConcurrentHashMap();
private final Map, SetterCall > setters = new ConcurrentHashMap();
private final Class annotationClass;
public BeanPropertyAnnotationCache(final Class annotationClass) {
this.annotationClass = annotationClass;
}
public Optional getAnnotation(final Class derivedType) {
return annotations.computeIfAbsent(derivedType, this::createOptionalAnnotation);
}
public Optional getAnnotation(final DERIVED instance) {
return getAnnotation((Class) instance.getClass());
}
public GetterResult get(final BASE instance) {
return (GetterResult) getters.computeIfAbsent((Class ) instance.getClass(), this::createGetter).get(instance);
}
public SetterResult set(final DERIVED instance, final PROPERTY_TYPE propertyValue ) {
return setters.computeIfAbsent((Class) instance.getClass(), this::createSetter).set(instance, propertyValue);
}
private SetterCall createSetter(final Class derivedType) {
return firstOptional(() -> getGetterWithAnnotation(derivedType, annotationClass).findAny().flatMap(getter -> setterOfGetter(getter, derivedType))
.map(MethodSetterCall ::new),
() -> getFieldsWithAnnotation(derivedType, annotationClass).findAny().map(this::createFieldSetter))
.orElseGet(NoSetterCall::new);
}
private GetterCall createGetter(final Class derivedType) {
return firstOptional(() -> getGetterWithAnnotation(derivedType, annotationClass).findAny().map(MethodGetterCall ::new),
() -> getFieldsWithAnnotation(derivedType, annotationClass).findAny().map(this::createFieldGetter))
.orElseGet(NoGetterCall::new);
}
private GetterCall createFieldGetter(final Field field) {
GetterCall retVal;
try {
retVal = new FieldGetterCall<>(field);
} catch (IllegalArgumentException e) {
retVal = new FieldReflectionGetterCall<>(field);
}
return retVal;
}
private SetterCall createFieldSetter(final Field field) {
SetterCall retVal;
try {
retVal = new FieldSetterCall<>(field);
} catch (IllegalArgumentException e) {
retVal = new FieldReflectionSetterCall<>(field);
}
return retVal;
}
private Optional createOptionalAnnotation(Class derivedType) {
return getGetterWithAnnotation(derivedType, annotationClass).findAny().map(m -> m.getAnnotation(annotationClass)).map(Optional::of)
.orElseGet(() -> getFieldsWithAnnotation(derivedType, annotationClass).findAny().map(f -> f.getAnnotation(annotationClass)));
}
private Optional setterOfGetter(final Method getter, final Class derivedClass) {
String setterName = convertToSetterName(getter.getName());
return getMethodWithName(derivedClass, setterName);
}
private static String convertToSetterName(final String getterName) {
int nameIndex = getterName.startsWith("is") ? 2 : 3;
return "set" + getterName.substring(nameIndex);
}
private static Optional getMethodWithName(final Class> clazz, final String name) {
return getAccessibleMethods(clazz).filter(m -> m.getName().equals(name)).findAny();
}
private static Stream getGetterWithAnnotation(final Class> clazz, final Class extends Annotation> annotationClass) {
return getGetterMethods(clazz).filter(m -> m.isAnnotationPresent(annotationClass));
}
private static Stream getFieldsWithAnnotation(final Class> clazz, final Class extends Annotation> annotationClass) {
return getAccessibleFields(clazz).filter(f -> f.isAnnotationPresent(annotationClass));
}
private static Stream getGetterMethods(final Class> clazz) {
return getAccessibleMethods(clazz).filter(m -> m.getName().startsWith("get") || m.getName().startsWith("is"));
}
private static Stream getAccessibleMethods(final Class> clazz) {
Iterable> iterable = () -> new SuperClassIterator(clazz);
Stream> classHierarchy = StreamSupport.stream(iterable.spliterator(), false);
return classHierarchy.flatMap(c -> Stream.of(c.getDeclaredMethods()));
}
private static Stream getAccessibleFields(final Class> clazz) {
Iterable> iterable = () -> new SuperClassIterator(clazz);
Stream> classHierarchy = StreamSupport.stream(iterable.spliterator(), false);
return classHierarchy.flatMap(c -> Stream.of(c.getDeclaredFields()));
}
private static class SuperClassIterator implements Iterator> {
private Class clazz;
public SuperClassIterator(final Class clazz) {
this.clazz = clazz;
}
@Override
public boolean hasNext() {
return clazz != null;
}
@Override
public Class next() {
Class retVal = clazz;
clazz = clazz.getSuperclass();
return retVal;
}
}
private static Optional firstOptional(final Supplier>... suppliers) {
return Arrays.asList(suppliers).stream().map(Supplier::get).flatMap(o -> o.map(Stream::of).orElseGet(Stream::empty)).findFirst();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy