import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Method; import java.lang.reflect.Modifier; /** * Annotation to simplify output hints and errors for scripts. Hints and errors * are directly set in code. * * @author Dr. Matthias Wegner * @since 2014-11-01 * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ScriptDocumentation { /** * Description of annotation * * @return text of description */ public String desc() default ""; /** * Parameters of parameters * * @return Array of parameters. Used Syntax should be: parameter - Some text */ public String[] params(); /** * Static Helper class to create a String representation of the annotation * * @author Dr. Matthias Wegner * */ public class ServiceDisplay { /** * Returns a string of the annotation of given class and method * * @param clazz * Class which holds the annotation * @param methodName * method which should be used * @return String of Service annotation */ public static String show(Class clazz, String methodName) { for (Method method : clazz.getMethods()) { if (Modifier.isPublic(method.getModifiers()) && method.getName().equals(methodName)) { ScriptDocumentation serviceDescription = method.getAnnotation(ScriptDocumentation.class); if (serviceDescription != null) { StringBuilder sb = new StringBuilder(); sb.append(serviceDescription.desc() + ": "); String[] params = serviceDescription.params(); for (int i = 0; i < params.length; i++) { sb.append(params[i]); if ((i + 1) != params.length) { sb.append(", "); } } return sb.toString(); } } } return ""; } /** * String of all annotations in one class * * @param clazz * Class to show annotations * @return String of all annotations */ public static String createString(Class clazz) { StringBuilder sb = new StringBuilder(); sb.append("

Methods of " + clazz.getName() + "

"); for (Method method : clazz.getMethods()) { if (Modifier.isPublic(method.getModifiers())) { ScriptDocumentation serviceDescription = method.getAnnotation(ScriptDocumentation.class); if (serviceDescription != null) { sb.append("*" + method.getName() + " ("); Class[] parameters = method.getParameterTypes(); for (int i = 0; i < parameters.length; i++) { sb.append(parameters[i].getName()); if ((i + 1) != parameters.length) { sb.append(", "); } } sb.append(")*
"); sb.append("~~" + serviceDescription.desc() + "~~
"); sb.append(""); sb.append("~~returns~~ " + method.getGenericReturnType() + "
"); sb.append("----"); sb.append("
"); } } } return sb.toString(); } } }