@Override public void handle( final AnnotationValues<ListenerSupport> annotation, final JCAnnotation source, final JavacNode annotationNode) { deleteAnnotationIfNeccessary(annotationNode, ListenerSupport.class); JavacType type = JavacType.typeOf(annotationNode, source); if (type.isAnnotation() || type.isInterface()) { annotationNode.addError(canBeUsedOnClassAndEnumOnly(ListenerSupport.class)); return; } List<Object> listenerInterfaces = annotation.getActualExpressions("value"); if (listenerInterfaces.isEmpty()) { annotationNode.addError( String.format( "@%s has no effect since no interface types were specified.", ListenerSupport.class.getName())); return; } List<TypeSymbol> resolvedInterfaces = resolveInterfaces(annotationNode, ListenerSupport.class, listenerInterfaces); for (TypeSymbol interfaze : resolvedInterfaces) { handler.addListenerField(type, interfaze); handler.addAddListenerMethod(type, interfaze); handler.addRemoveListenerMethod(type, interfaze); addFireListenerMethods(type, interfaze); } type.editor().rebuild(); }
private List<TypeSymbol> resolveInterfaces( final JavacNode annotationNode, final Class<? extends java.lang.annotation.Annotation> annotationType, final List<Object> listenerInterfaces) { List<TypeSymbol> resolvedInterfaces = new ArrayList<TypeSymbol>(); for (Object listenerInterface : listenerInterfaces) { if (listenerInterface instanceof JCFieldAccess) { JCFieldAccess interfaze = (JCFieldAccess) listenerInterface; if ("class".equals(As.string(interfaze.name))) { Type interfaceType = CLASS.resolveMember(annotationNode, interfaze.selected); if (interfaceType == null) continue; if (interfaceType.isInterface()) { TypeSymbol interfaceSymbol = interfaceType.asElement(); if (interfaceSymbol != null) resolvedInterfaces.add(interfaceSymbol); } else { annotationNode.addWarning( String.format( "@%s works only with interfaces. %s was skipped", annotationType.getName(), listenerInterface)); } } } } return resolvedInterfaces; }
@Override protected void createParamsAndArgs( final Object method, final List<Argument> params, final List<Expression<?>> args) { MethodType mtype = (MethodType) type(method); if (mtype.argtypes.isEmpty()) return; int argCounter = 0; for (Type parameter : mtype.getParameterTypes()) { String arg = "arg" + argCounter++; params.add(Arg(Type(parameter), arg)); args.add(Name(arg)); } }