|
Groovy example source code file (GrabAnnotationTransformation.java)
This example Groovy source code file (GrabAnnotationTransformation.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Groovy GrabAnnotationTransformation.java source code
/*
* Copyright 2003-2011 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 groovy.lang.Grab;
import groovy.lang.GrabResolver;
import groovy.lang.Grapes;
import groovy.lang.GrabExclude;
import groovy.lang.GrabConfig;
import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.expr.*;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.control.CompilePhase;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.transform.ASTTransformation;
import org.codehaus.groovy.transform.ASTTransformationVisitor;
import org.codehaus.groovy.transform.GroovyASTTransformation;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
* Transformation for declarative dependency management.
*/
@GroovyASTTransformation(phase=CompilePhase.CONVERSION)
public class GrabAnnotationTransformation extends ClassCodeVisitorSupport implements ASTTransformation {
private static final String GRAB_CLASS_NAME = Grab.class.getName();
private static final String GRAB_DOT_NAME = GRAB_CLASS_NAME.substring(GRAB_CLASS_NAME.lastIndexOf("."));
private static final String GRAB_SHORT_NAME = GRAB_DOT_NAME.substring(1);
private static final String GRABEXCLUDE_CLASS_NAME = GrabExclude.class.getName();
private static final String GRABEXCLUDE_DOT_NAME = dotName(GRABEXCLUDE_CLASS_NAME);
private static final String GRABEXCLUDE_SHORT_NAME = shortName(GRABEXCLUDE_DOT_NAME);
private static final String GRABCONFIG_CLASS_NAME = GrabConfig.class.getName();
private static final String GRABCONFIG_DOT_NAME = dotName(GRABCONFIG_CLASS_NAME);
private static final String GRABCONFIG_SHORT_NAME = shortName(GRABCONFIG_DOT_NAME);
private static final String GRAPES_CLASS_NAME = Grapes.class.getName();
private static final String GRAPES_DOT_NAME = dotName(GRAPES_CLASS_NAME);
private static final String GRAPES_SHORT_NAME = shortName(GRAPES_DOT_NAME);
private static final String GRABRESOLVER_CLASS_NAME = GrabResolver.class.getName();
private static final String GRAPERESOLVER_DOT_NAME = dotName(GRABRESOLVER_CLASS_NAME);
private static final String GRABRESOLVER_SHORT_NAME = shortName(GRAPERESOLVER_DOT_NAME);
private static final ClassNode THREAD_CLASSNODE = ClassHelper.make(Thread.class);
private static final List<String> GRABEXCLUDE_REQUIRED = Arrays.asList("group", "module");
private static final List<String> GRAPERESOLVER_REQUIRED = Arrays.asList("name", "root");
private static final List<String> GRAB_REQUIRED = Arrays.asList("group", "module", "version");
private static final List<String> GRAB_OPTIONAL = Arrays.asList("classifier", "transitive", "conf", "ext", "type", "changing", "force");
private static final Collection<String> GRAB_ALL = DefaultGroovyMethods.plus(GRAB_REQUIRED, GRAB_OPTIONAL);
private static final Pattern IVY_PATTERN = Pattern.compile("([a-zA-Z0-9-/._+=]+)#([a-zA-Z0-9-/._+=]+)(;([a-zA-Z0-9-/.\\(\\)\\[\\]\\{\\}_+=,:@][a-zA-Z0-9-/.\\(\\)\\]\\{\\}_+=,:@]*))?(\\[([a-zA-Z0-9-/._+=,]*)\\])?");
private static final Pattern ATTRIBUTES_PATTERN = Pattern.compile("(.*;|^)([a-zA-Z0-9]+)=([a-zA-Z0-9.*\\[\\]\\-\\(\\),]*)$");
private static String dotName(String className) {
return className.substring(className.lastIndexOf("."));
}
private static String shortName(String className) {
return className.substring(1);
}
boolean allowShortGrab;
Set<String> grabAliases;
List<AnnotationNode> grabAnnotations;
boolean allowShortGrabExcludes;
Set<String> grabExcludeAliases;
List<AnnotationNode> grabExcludeAnnotations;
boolean allowShortGrabConfig;
Set<String> grabConfigAliases;
List<AnnotationNode> grabConfigAnnotations;
boolean allowShortGrapes;
Set<String> grapesAliases;
List<AnnotationNode> grapesAnnotations;
boolean allowShortGrabResolver;
Set<String> grabResolverAliases;
List<AnnotationNode> grabResolverAnnotations;
SourceUnit sourceUnit;
ClassLoader loader;
boolean initContextClassLoader;
public SourceUnit getSourceUnit() {
return sourceUnit;
}
public void visit(ASTNode[] nodes, SourceUnit source) {
sourceUnit = source;
loader = null;
initContextClassLoader = false;
ModuleNode mn = (ModuleNode) nodes[0];
allowShortGrab = true;
allowShortGrabExcludes = true;
allowShortGrabConfig = true;
allowShortGrapes = true;
allowShortGrabResolver = true;
grabAliases = new HashSet<String>();
grabExcludeAliases = new HashSet<String>();
grabConfigAliases = new HashSet<String>();
grapesAliases = new HashSet<String>();
grabResolverAliases = new HashSet<String>();
for (ImportNode im : mn.getImports()) {
String alias = im.getAlias();
String className = im.getClassName();
if ((className.endsWith(GRAB_DOT_NAME) && ((alias == null) || (alias.length() == 0)))
|| (GRAB_CLASS_NAME.equals(alias)))
{
allowShortGrab = false;
} else if (GRAB_CLASS_NAME.equals(className)) {
grabAliases.add(im.getAlias());
}
if ((className.endsWith(GRAPES_DOT_NAME) && ((alias == null) || (alias.length() == 0)))
|| (GRAPES_CLASS_NAME.equals(alias)))
{
allowShortGrapes = false;
} else if (GRAPES_CLASS_NAME.equals(className)) {
grapesAliases.add(im.getAlias());
}
if ((className.endsWith(GRAPERESOLVER_DOT_NAME) && ((alias == null) || (alias.length() == 0)))
|| (GRABRESOLVER_CLASS_NAME.equals(alias)))
{
allowShortGrabResolver = false;
} else if (GRABRESOLVER_CLASS_NAME.equals(className)) {
grabResolverAliases.add(im.getAlias());
}
}
List<Map grabMaps = new ArrayList
Other Groovy examples (source code examples)
Here is a short list of links related to this Groovy GrabAnnotationTransformation.java source code file:
|