All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.dagere.kopeme.annotations.AnnotationDefaults Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package de.dagere.kopeme.annotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Class used to get an instance of an annotation type (e.g. {@link PerformanceTest}). Recommendation of a stackoverflow article to create annotation instances used to get the
 * default of an annotation.
 * 
 * @see http://stackoverflow.com/questions/266903/create-annotation-instance-with-defaults-in-java
 * @author dhaeb
 * 
 */
public class AnnotationDefaults implements InvocationHandler {

   @SuppressWarnings("unchecked")
   public static  A of(final Class annotation) {
      return (A) Proxy.newProxyInstance(annotation.getClassLoader(), new Class[] { annotation }, new AnnotationDefaults());
   }

   @Override
   public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
      return method.getDefaultValue();
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy