use of org.codehaus.groovy.ast.ImportNode in project groovy by apache.
the class StaticImportVisitor method transformMapEntryExpression.
// if you have a Bar class with a static foo property, and this:
// import static Bar.foo as baz
// then this constructor (not normal usage of statics):
// new Bar(baz:1)
// will become:
// new Bar(foo:1)
private Expression transformMapEntryExpression(MapEntryExpression me, ClassNode constructorCallType) {
Expression key = me.getKeyExpression();
Expression value = me.getValueExpression();
ModuleNode module = currentClass.getModule();
if (module != null && key instanceof ConstantExpression) {
Map<String, ImportNode> importNodes = module.getStaticImports();
if (importNodes.containsKey(key.getText())) {
ImportNode importNode = importNodes.get(key.getText());
if (importNode.getType().equals(constructorCallType)) {
String newKey = importNode.getFieldName();
return new MapEntryExpression(new ConstantExpression(newKey), value.transformExpression(this));
}
}
}
return me;
}
use of org.codehaus.groovy.ast.ImportNode in project groovy by apache.
the class StaticImportVisitor method findStaticMethodImportFromModule.
private Expression findStaticMethodImportFromModule(Expression method, Expression args) {
ModuleNode module = currentClass.getModule();
if (module == null || !(method instanceof ConstantExpression))
return null;
Map<String, ImportNode> importNodes = module.getStaticImports();
ConstantExpression ce = (ConstantExpression) method;
Expression expression;
Object value = ce.getValue();
// skip non-Strings, e.g. Integer
if (!(value instanceof String))
return null;
final String name = (String) value;
// when resolving methodCall() or getProp() or setProp()
if (importNodes.containsKey(name)) {
ImportNode importNode = importNodes.get(name);
expression = findStaticMethod(importNode.getType(), importNode.getFieldName(), args);
if (expression != null)
return expression;
expression = findStaticPropertyAccessorGivenArgs(importNode.getType(), getPropNameForAccessor(importNode.getFieldName()), args);
if (expression != null) {
return new StaticMethodCallExpression(importNode.getType(), importNode.getFieldName(), args);
}
}
// when resolving getProp() or setProp()
if (isValidAccessorName(name)) {
String propName = getPropNameForAccessor(name);
if (importNodes.containsKey(propName)) {
ImportNode importNode = importNodes.get(propName);
ClassNode importClass = importNode.getType();
String importMember = importNode.getFieldName();
expression = findStaticMethod(importClass, prefix(name) + capitalize(importMember), args);
if (expression != null)
return expression;
expression = findStaticPropertyAccessorGivenArgs(importClass, importMember, args);
if (expression != null) {
return new StaticMethodCallExpression(importClass, prefix(name) + capitalize(importMember), args);
}
}
}
Map<String, ImportNode> starImports = module.getStaticStarImports();
ClassNode starImportType;
if (currentClass.isEnum() && starImports.containsKey(currentClass.getName())) {
ImportNode importNode = starImports.get(currentClass.getName());
starImportType = importNode == null ? null : importNode.getType();
expression = findStaticMethod(starImportType, name, args);
if (expression != null)
return expression;
} else {
for (ImportNode importNode : starImports.values()) {
starImportType = importNode == null ? null : importNode.getType();
expression = findStaticMethod(starImportType, name, args);
if (expression != null)
return expression;
expression = findStaticPropertyAccessorGivenArgs(starImportType, getPropNameForAccessor(name), args);
if (expression != null) {
return new StaticMethodCallExpression(starImportType, name, args);
}
}
}
return null;
}
use of org.codehaus.groovy.ast.ImportNode in project groovy by apache.
the class StaticImportVisitor method findStaticProperty.
private Expression findStaticProperty(Map<String, ImportNode> importNodes, String accessorName) {
Expression result = null;
ImportNode importNode = importNodes.get(accessorName);
ClassNode importClass = importNode.getType();
String importMember = importNode.getFieldName();
result = findStaticPropertyAccessorByFullName(importClass, importMember);
if (result == null) {
result = findStaticPropertyAccessor(importClass, getPropNameForAccessor(importMember));
}
return result;
}
use of org.codehaus.groovy.ast.ImportNode in project groovy by apache.
the class BaseScriptASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(node.getClassNode()))
return;
if (parent instanceof DeclarationExpression) {
changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node);
} else if (parent instanceof ImportNode || parent instanceof PackageNode) {
changeBaseScriptTypeFromPackageOrImport(source, parent, node);
} else if (parent instanceof ClassNode) {
changeBaseScriptTypeFromClass((ClassNode) parent, node);
}
}
use of org.codehaus.groovy.ast.ImportNode in project groovy-core by groovy.
the class BaseScriptASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(node.getClassNode()))
return;
if (parent instanceof DeclarationExpression) {
changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node);
} else if (parent instanceof ImportNode || parent instanceof PackageNode) {
changeBaseScriptTypeFromPackageOrImport(source, parent, node);
} else if (parent instanceof ClassNode) {
changeBaseScriptTypeFromClass((ClassNode) parent, node);
}
}
Aggregations