Search in sources :

Example 36 with Annotation

use of java.lang.annotation.Annotation in project Denizen-For-Bukkit by DenizenScript.

the class CommandManager method registerMethods.

// Register the methods of a class.
private void registerMethods(Class<?> clazz, Method parent, Object obj) {
    for (Method method : clazz.getMethods()) {
        if (!method.isAnnotationPresent(Command.class)) {
            continue;
        }
        // We want to be able invoke with an instance
        if (!Modifier.isStatic(method.getModifiers())) {
            // Can't register this command if we don't have an instance
            if (obj == null) {
                continue;
            }
            instances.put(method, obj);
        }
        Command cmd = method.getAnnotation(Command.class);
        // Cache the aliases too
        for (String alias : cmd.aliases()) {
            for (String modifier : cmd.modifiers()) {
                commands.put(alias + " " + modifier, method);
            }
            if (!commands.containsKey(alias + " help")) {
                commands.put(alias + " help", null);
            }
        }
        List<Annotation> annotations = new ArrayList<Annotation>();
        for (Annotation annotation : method.getDeclaringClass().getAnnotations()) {
            Class<? extends Annotation> annotationClass = annotation.annotationType();
            if (annotationProcessors.containsKey(annotationClass)) {
                annotations.add(annotation);
            }
        }
        for (Annotation annotation : method.getAnnotations()) {
            Class<? extends Annotation> annotationClass = annotation.annotationType();
            if (!annotationProcessors.containsKey(annotationClass)) {
                continue;
            }
            Iterator<Annotation> itr = annotations.iterator();
            while (itr.hasNext()) {
                Annotation previous = itr.next();
                if (previous.annotationType() == annotationClass) {
                    itr.remove();
                }
            }
            annotations.add(annotation);
        }
        if (annotations.size() > 0) {
            registeredAnnotations.putAll(method, annotations);
        }
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length <= 1 || parameterTypes[1] == CommandSender.class) {
            serverCommands.add(method);
        }
    }
}
Also used : CommandSender(org.bukkit.command.CommandSender) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 37 with Annotation

use of java.lang.annotation.Annotation in project Denizen-For-Bukkit by DenizenScript.

the class CommandManager method executeMethod.

// Attempt to execute a command.
private void executeMethod(String[] args, CommandSender sender, Object[] methodArgs) throws CommandException {
    String cmdName = CoreUtilities.toLowerCase(args[0]);
    String modifier = args.length > 1 ? args[1] : "";
    boolean help = CoreUtilities.toLowerCase(modifier).equals("help");
    Method method = commands.get(cmdName + " " + CoreUtilities.toLowerCase(modifier));
    if (method == null && !help) {
        method = commands.get(cmdName + " *");
    }
    if (method == null && help) {
        executeHelp(args, sender);
        return;
    }
    if (method == null) {
        throw new UnhandledCommandException();
    }
    if (!serverCommands.contains(method) && sender instanceof ConsoleCommandSender) {
        throw new ServerCommandException();
    }
    if (!hasPermission(method, sender)) {
        throw new NoPermissionsException();
    }
    Command cmd = method.getAnnotation(Command.class);
    CommandContext context = new CommandContext(sender, args);
    if (context.argsLength() < cmd.min()) {
        throw new CommandUsageException("Too few arguments.", getUsage(args, cmd));
    }
    if (cmd.max() != -1 && context.argsLength() > cmd.max()) {
        throw new CommandUsageException("Too many arguments.", getUsage(args, cmd));
    }
    if (!cmd.flags().contains("*")) {
        for (char flag : context.getFlags()) {
            if (cmd.flags().indexOf(String.valueOf(flag)) == -1) {
                throw new CommandUsageException("Unknown flag: " + flag, getUsage(args, cmd));
            }
        }
    }
    methodArgs[0] = context;
    for (Annotation annotation : registeredAnnotations.get(method)) {
        CommandAnnotationProcessor processor = annotationProcessors.get(annotation.annotationType());
        processor.process(sender, context, annotation, methodArgs);
    }
    Object instance = instances.get(method);
    try {
        method.invoke(instance, methodArgs);
    } catch (IllegalArgumentException e) {
        dB.echoError(e);
    } catch (IllegalAccessException e) {
        dB.echoError(e);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof CommandException) {
            throw (CommandException) e.getCause();
        }
        throw new WrappedCommandException(e.getCause());
    }
}
Also used : Method(java.lang.reflect.Method) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 38 with Annotation

use of java.lang.annotation.Annotation in project CloudStack-archive by CloudStack-extras.

the class ComponentTestCase method setUp.

@Override
protected void setUp() {
    super.setUp();
    Annotation[] annotations = getClass().getAnnotations();
    if (annotations != null) {
        for (Annotation annotation : annotations) {
            if (annotation instanceof ComponentSetup) {
                ComponentLocator.getLocator(((ComponentSetup) annotation).managerName(), ((ComponentSetup) annotation).setupXml(), ((ComponentSetup) annotation).log4j());
                break;
            }
        }
    }
}
Also used : Annotation(java.lang.annotation.Annotation)

Example 39 with Annotation

use of java.lang.annotation.Annotation in project atmosphere by Atmosphere.

the class DefaultAnnotationProcessor method configure.

@Override
public void configure(final AtmosphereConfig config) {
    ServletContext sc = config.framework().getServletContext();
    Map<Class<? extends Annotation>, Set<Class<?>>> annotations = (Map<Class<? extends Annotation>, Set<Class<?>>>) sc.getAttribute(ANNOTATION_ATTRIBUTE);
    sc.removeAttribute(ANNOTATION_ATTRIBUTE);
    boolean useByteCodeProcessor = config.getInitParameter(ApplicationConfig.BYTECODE_PROCESSOR, false);
    boolean scanForAtmosphereAnnotation = false;
    if (useByteCodeProcessor || annotations == null || annotations.isEmpty()) {
        delegate = new BytecodeBasedAnnotationProcessor(handler);
        scanForAtmosphereAnnotation = true;
    } else {
        Map<Class<? extends Annotation>, Set<Class<?>>> clone = new HashMap<Class<? extends Annotation>, Set<Class<?>>>();
        clone.putAll(annotations);
        delegate = new ServletContainerInitializerAnnotationProcessor(handler, clone, config.framework());
    }
    logger.info("AnnotationProcessor {} being used", delegate.getClass());
    if (scanForAtmosphereAnnotation) {
        scanForAnnotation(config.framework());
    }
    delegate.configure(config.framework().getAtmosphereConfig());
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) ServletContext(javax.servlet.ServletContext) IOUtils.loadClass(org.atmosphere.util.IOUtils.loadClass) HashMap(java.util.HashMap) Map(java.util.Map) AtmosphereAnnotation(org.atmosphere.config.AtmosphereAnnotation) Annotation(java.lang.annotation.Annotation)

Example 40 with Annotation

use of java.lang.annotation.Annotation in project atmosphere by Atmosphere.

the class AnnotationScanningServletContainerInitializer method onStartup.

@Override
public void onStartup(final Set<Class<?>> classes, final ServletContext servletContext) throws ServletException {
    final Map<Class<? extends Annotation>, Set<Class<?>>> classesByAnnotation = new HashMap<Class<? extends Annotation>, Set<Class<?>>>();
    if (classes != null) {
        for (final Class<?> clazz : classes) {
            for (Annotation annotation : clazz.getAnnotations()) {
                Set<Class<?>> classSet = classesByAnnotation.get(annotation.annotationType());
                if (classSet == null) {
                    classesByAnnotation.put(annotation.annotationType(), classSet = new HashSet<Class<?>>());
                }
                classSet.add(clazz);
            }
        }
    }
    servletContext.setAttribute(DefaultAnnotationProcessor.ANNOTATION_ATTRIBUTE, classesByAnnotation);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) AtmosphereAnnotation(org.atmosphere.config.AtmosphereAnnotation) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Aggregations

Annotation (java.lang.annotation.Annotation)1784 Method (java.lang.reflect.Method)412 ArrayList (java.util.ArrayList)245 Field (java.lang.reflect.Field)172 Test (org.junit.Test)164 Type (java.lang.reflect.Type)149 HashMap (java.util.HashMap)146 HashSet (java.util.HashSet)129 Map (java.util.Map)95 List (java.util.List)92 IOException (java.io.IOException)71 Set (java.util.Set)71 ParameterizedType (java.lang.reflect.ParameterizedType)62 InvocationTargetException (java.lang.reflect.InvocationTargetException)53 Collection (java.util.Collection)52 LinkedHashMap (java.util.LinkedHashMap)36 Collectors (java.util.stream.Collectors)36 Constructor (java.lang.reflect.Constructor)33 LinkedHashSet (java.util.LinkedHashSet)32 Optional (java.util.Optional)32