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);
}
}
}
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());
}
}
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;
}
}
}
}
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());
}
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);
}
Aggregations