Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2003-2009 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package groovy.grape;
 
 
 import java.util.*;

Created by IntelliJ IDEA. User: Danno Date: Jan 18, 2008 Time: 9:48:57 PM
 
 
     private static final String GRAB_CLASS_NAME = Grab.class.getName();
     private static final String GRAB_DOT_NAME = .substring(.lastIndexOf("."));
     private static final String GRAB_SHORT_NAME = .substring(1);
 
     private static final String GRAPES_CLASS_NAME = Grapes.class.getName();
     private static final String GRAPES_SHORT_NAME = .substring(1);
 
     boolean allowShortGrab;
     Set<StringgrabAliases;
 
     boolean allowShortGrapes;
 
 
     public SourceUnit getSourceUnit() {
         return ;
     }
 
     public void visit(ASTNode[] nodesSourceUnit source) {
          = source;
 
         ModuleNode mn = (ModuleNodenodes[0];
 
          = true;
          = true;
          = new HashSet();
          = new HashSet();
         for (ImportNode im : (Collection<ImportNode>) mn.getImports()) {
             String alias = im.getAlias();
             String className = im.getClassName();
             if ((className.endsWith() && ((alias == null) || (alias.length() == 0)))
                 || (.equals(alias)))
             {
                  = false;
             } else if (.equals(className)) {
                 .add(im.getAlias());
             }
             if ((className.endsWith() && ((alias == null) || (alias.length() == 0)))
                 || (.equals(alias)))
             {
                  = false;
             } else if (.equals(className)) {
                 .add(im.getAlias());
             }
         }
 
         List<Map<String,Object>> grabMaps = new ArrayList();
 
         for (ClassNode classNode : (Collection<ClassNode>) .getAST().getClasses()) {
              = new ArrayList<AnnotationNode>();
              = new ArrayList<AnnotationNode>();
 
             visitClass(classNode);
 
             ClassNode grapeClassNode = new ClassNode(Grape.class);
 
            if (!.isEmpty()) {
                for (AnnotationNode node : ) {
                    Expression init = node.getMember("initClass");
                    Expression value = node.getMember("value");
                    if (value instanceof ListExpression) {
                        for (Object o : ((ListExpression)value).getExpressions()) {
                            if (o instanceof AnnotationConstantExpression) {
                                if (((AnnotationConstantExpression)o).getValue() instanceof AnnotationNode) {
                                    AnnotationNode annotation = (AnnotationNode) ((AnnotationConstantExpression)o).getValue();
                                    if ((init != null) && (annotation.getMember("initClass") != null)) {
                                        annotation.setMember("initClass"init);
                                    }
                                    String name = annotation.getClassNode().getName();
                                    if ((.equals(name))
                                        || ( && .equals(name))
                                        || (.contains(name)))
                                    {
                                        .add(annotation);
                                    }
                                }
                            }
                        }
                    }
                    // don't worry if it's not a ListExpression, or AnnotationConstant, etc.
                    // the rest of GroovyC will flag it as a syntax error later, so we don't
                    // need to raise the error ourselves
                }
            }
            if (!.isEmpty()) {
                grabAnnotationLoop:
                for (AnnotationNode node : ) {
                    Map<StringObjectgrabMap = new HashMap();
                    for (String s : new String[]{"group""module""version"}) {
                        if (node.getMember(s) == null) {
                            addError("The missing attribute \"" + s + "\" is required in @" + node.getClassNode().getNameWithoutPackage() + " annotations"node);
                            continue grabAnnotationLoop;
                        }
                    }
                    grabMap.put("group", ((ConstantExpression)node.getMember("group")).getValue());
                    grabMap.put("module", ((ConstantExpression)node.getMember("module")).getValue());
                    grabMap.put("version", ((ConstantExpression)node.getMember("version")).getValue());
                    if (node.getMember("classifier") != null)
                        grabMap.put("classifier", ((ConstantExpression)node.getMember("classifier")).getValue());
                    grabMaps.add(grabMap);
                    if ((node.getMember("initClass") == null)
                        || (node.getMember("initClass") == .))
                    {
                        List grabInitializers = new ArrayList();
                        // add Grape.grab([group:group, module:module, version:version, classifier:classifier])
                        MapExpression me = new MapExpression();
                        me.addMapEntryExpression(new ConstantExpression("group"),node.getMember("group"));
                        me.addMapEntryExpression(new ConstantExpression("module"),node.getMember("module"));
                        me.addMapEntryExpression(new ConstantExpression("version"),node.getMember("version"));
                        if (node.getMember("classifier") != null)
                            me.addMapEntryExpression(new ConstantExpression("classifier"),node.getMember("classifier"));
                        grabInitializers.add(new ExpressionStatement(
                                new StaticMethodCallExpression(
                                    grapeClassNode,
                                    "grab",
                                    new ArgumentListExpression(me))));
                        // insert at beginning so we have the classloader set up before the class is called
                        classNode.addStaticInitializerStatements(grabInitializerstrue);
                    }
                }
            }
        }
        if (!grabMaps.isEmpty()) {
            Map basicArgs = new HashMap();
            basicArgs.put("classLoader".getClassLoader());
            try {
                Grape.grab(basicArgsgrabMaps.toArray(new Map[grabMaps.size()]));
            } catch (RuntimeException re) {
                // Decided against syntax exception since this is not a syntax error.
                // The down side is we lose line number information fo the offending
                // @Grab annotation.
                source.addException(re);
            }
        }
    }
    protected void visitConstructorOrMethod(MethodNode nodeboolean isConstructor) {
        super.visitConstructorOrMethod(nodeisConstructor);
        // this should be pushed into the super class...
        for (Parameter param : node.getParameters()) {
            visitAnnotations(param);
        }
    }

    
Adds the annotation to the internal target list if a match is found

Parameters:
node
    public void visitAnnotations(AnnotatedNode node) {
        super.visitAnnotations(node);
        for (AnnotationNode an : (Collection<AnnotationNode>) node.getAnnotations()) {
            String name = an.getClassNode().getName();
            if ((.equals(name))
                || ( && .equals(name))
                || (.contains(name)))
            {
                .add(an);
            }
            if ((.equals(name))
                || ( && .equals(name))
                || (.contains(name)))
            {
                .add(an);
            }
        }
    }
New to GrepCode? Check out our FAQ X