use of org.codehaus.groovy.ast.expr.ConstantExpression in project ratpack by ratpack.
the class ScriptEngine method createClassLoader.
private GroovyClassLoader createClassLoader(final Path scriptPath) {
final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
if (!scriptBaseClass.equals(Script.class)) {
compilerConfiguration.setScriptBaseClass(scriptBaseClass.getName());
}
compilerConfiguration.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {
@Override
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
if (staticCompile) {
classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
}
classNode.addAnnotation(new AnnotationNode(new ClassNode(InheritConstructors.class)));
if (scriptPath != null) {
AnnotationNode scriptPathAnnotation = new AnnotationNode(new ClassNode(ScriptPath.class));
scriptPathAnnotation.addMember("value", new ConstantExpression(scriptPath.toUri().toString()));
classNode.addAnnotation(scriptPathAnnotation);
}
}
});
return new GroovyClassLoader(parentLoader, compilerConfiguration) {
@Override
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
return new CompilationUnit(config, source, this) {
{
verifier = new Verifier() {
@Override
public void visitClass(ClassNode node) {
if (node.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
AnnotationNode lineNumberAnnotation = new AnnotationNode(LINE_NUMBER_CLASS_NODE);
lineNumberAnnotation.addMember("value", new ConstantExpression(node.getLineNumber(), true));
node.addAnnotation(lineNumberAnnotation);
}
super.visitClass(node);
}
};
}
};
}
};
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class GrabAnnotationTransformation method checkForConvenienceForm.
private static void checkForConvenienceForm(AnnotationNode node, boolean exclude) {
Object val = node.getMember("value");
if (val == null || !(val instanceof ConstantExpression))
return;
Object allParts = ((ConstantExpression) val).getValue();
if (!(allParts instanceof String))
return;
String allstr = (String) allParts;
// strip off trailing attributes
boolean done = false;
while (!done) {
Matcher attrs = ATTRIBUTES_PATTERN.matcher(allstr);
if (attrs.find()) {
String attrName = attrs.group(2);
String attrValue = attrs.group(3);
if (attrName == null || attrValue == null)
continue;
boolean isBool = GRAB_BOOLEAN.contains(attrName);
ConstantExpression value = constX(isBool ? Boolean.valueOf(attrValue) : attrValue);
value.setSourcePosition(node);
node.addMember(attrName, value);
int lastSemi = allstr.lastIndexOf(';');
if (lastSemi == -1) {
allstr = "";
break;
}
allstr = allstr.substring(0, lastSemi);
} else {
done = true;
}
}
if (allstr.contains("#")) {
// see: http://ant.apache.org/ivy/history/latest-milestone/textual.html
Matcher m = IVY_PATTERN.matcher(allstr);
if (!m.find())
return;
if (m.group(1) == null || m.group(2) == null)
return;
node.addMember("module", constX(m.group(2)));
node.addMember("group", constX(m.group(1)));
if (m.group(6) != null)
node.addMember("conf", constX(m.group(6)));
if (m.group(4) != null)
node.addMember("version", constX(m.group(4)));
else if (!exclude && node.getMember("version") == null)
node.addMember("version", constX("*"));
node.getMembers().remove("value");
} else if (allstr.contains(":")) {
// assume gradle syntax
// see: http://www.gradle.org/latest/docs/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
Map<String, Object> parts = GrapeUtil.getIvyParts(allstr);
for (String key : parts.keySet()) {
String value = parts.get(key).toString();
if (!key.equals("version") || !value.equals("*") || !exclude) {
node.addMember(key, constX(value));
}
}
node.getMembers().remove("value");
}
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class AbstractASTTransformation method getMemberStringList.
public static List<String> getMemberStringList(AnnotationNode anno, String name) {
Expression expr = anno.getMember(name);
if (expr == null) {
return null;
}
if (expr instanceof ListExpression) {
List<String> list = new ArrayList<String>();
final ListExpression listExpression = (ListExpression) expr;
if (isUndefinedMarkerList(listExpression)) {
return null;
}
for (Expression itemExpr : listExpression.getExpressions()) {
if (itemExpr != null && itemExpr instanceof ConstantExpression) {
Object value = ((ConstantExpression) itemExpr).getValue();
if (value != null)
list.add(value.toString());
}
}
return list;
}
return tokenize(getMemberStringValue(anno, name));
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class JavaStubGenerator method printField.
private void printField(PrintWriter out, FieldNode fieldNode, boolean isInterface) {
if ((fieldNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0)
return;
printAnnotations(out, fieldNode);
if (!isInterface) {
printModifiers(out, fieldNode.getModifiers());
}
ClassNode type = fieldNode.getType();
printType(out, type);
out.print(" ");
out.print(fieldNode.getName());
if (isInterface || (fieldNode.getModifiers() & Opcodes.ACC_FINAL) != 0) {
out.print(" = ");
Expression valueExpr = fieldNode.getInitialValueExpression();
if (valueExpr instanceof ConstantExpression) {
valueExpr = Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) valueExpr);
}
if (valueExpr instanceof ConstantExpression && fieldNode.isStatic() && fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(valueExpr.getType()) && valueExpr.getType().equals(fieldNode.getType())) {
// GROOVY-5150 : Initialize value with a dummy constant so that Java cross compiles correctly
if (ClassHelper.STRING_TYPE.equals(valueExpr.getType())) {
out.print(formatString(valueExpr.getText()));
} else if (ClassHelper.char_TYPE.equals(valueExpr.getType())) {
out.print("'" + valueExpr.getText() + "'");
} else {
ClassNode constantType = valueExpr.getType();
out.print('(');
printType(out, type);
out.print(") ");
out.print(valueExpr.getText());
if (ClassHelper.Long_TYPE.equals(ClassHelper.getWrapper(constantType)))
out.print('L');
}
} else if (ClassHelper.isPrimitiveType(type)) {
String val = type == ClassHelper.boolean_TYPE ? "false" : "0";
out.print("new " + ClassHelper.getWrapper(type) + "((" + type + ")" + val + ")");
} else {
out.print("null");
}
}
out.println(";");
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class MacroInvocationTrap method visitConstructorCallExpression.
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression call) {
ClassNode type = call.getType();
if (type instanceof InnerClassNode) {
if (((InnerClassNode) type).isAnonymous() && MACROCLASS_TYPE.getNameWithoutPackage().equals(type.getSuperClass().getNameWithoutPackage())) {
//System.out.println("call = " + call.getText());
try {
String source = convertInnerClassToSource(type);
List<Expression> macroArgumentsExpressions = new LinkedList<Expression>();
macroArgumentsExpressions.add(new ConstantExpression(source));
macroArgumentsExpressions.add(buildSubstitutionMap(type));
macroArgumentsExpressions.add(new ClassExpression(ClassHelper.make(ClassNode.class)));
MethodCallExpression macroCall = new MethodCallExpression(new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"), MacroTransformation.MACRO_METHOD, new ArgumentListExpression(macroArgumentsExpressions));
macroCall.setSpreadSafe(false);
macroCall.setSafe(false);
macroCall.setImplicitThis(false);
call.putNodeMetaData(MacroTransformation.class, macroCall);
List<ClassNode> classes = sourceUnit.getAST().getClasses();
for (Iterator<ClassNode> iterator = classes.iterator(); iterator.hasNext(); ) {
final ClassNode aClass = iterator.next();
if (aClass == type || type == aClass.getOuterClass()) {
iterator.remove();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
}
super.visitConstructorCallExpression(call);
}
Aggregations