diff options
Diffstat (limited to 'src/lombok/core/SpiLoadUtil.java')
-rw-r--r-- | src/lombok/core/SpiLoadUtil.java | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/lombok/core/SpiLoadUtil.java b/src/lombok/core/SpiLoadUtil.java new file mode 100644 index 00000000..bf4bddf4 --- /dev/null +++ b/src/lombok/core/SpiLoadUtil.java @@ -0,0 +1,37 @@ +package lombok.core; + +import java.lang.annotation.Annotation; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +public class SpiLoadUtil { + private SpiLoadUtil() {} + + @SuppressWarnings("unchecked") + public static Class<? extends Annotation> findAnnotationClass(Class<?> c, Class<?> base) { + if ( c == Object.class || c == null ) return null; + for ( Type iface : c.getGenericInterfaces() ) { + if ( iface instanceof ParameterizedType ) { + ParameterizedType p = (ParameterizedType)iface; + if ( !base.equals(p.getRawType()) ) continue; + Type target = p.getActualTypeArguments()[0]; + if ( target instanceof Class<?> ) { + if ( Annotation.class.isAssignableFrom((Class<?>) target) ) { + return (Class<? extends Annotation>) target; + } + } + + throw new ClassCastException("Not an annotation type: " + target); + } + } + + Class<? extends Annotation> potential = findAnnotationClass(c.getSuperclass(), base); + if ( potential != null ) return potential; + for ( Class<?> iface : c.getInterfaces() ) { + potential = findAnnotationClass(iface, base); + if ( potential != null ) return potential; + } + + return null; + } +} |