use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class Java5 method setMethodDefaultValue.
private static void setMethodDefaultValue(MethodNode mn, Method m) {
Object defaultValue = m.getDefaultValue();
ConstantExpression cExp = ConstantExpression.NULL;
if (defaultValue != null)
cExp = new ConstantExpression(defaultValue);
mn.setCode(new ReturnStatement(cExp));
mn.setAnnotationDefault(true);
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class Java5 method annotationValueToExpression.
private Expression annotationValueToExpression(Object value) {
if (value == null || value instanceof String || value instanceof Number || value instanceof Character || value instanceof Boolean)
return new ConstantExpression(value);
if (value instanceof Class)
return new ClassExpression(ClassHelper.makeWithoutCaching((Class) value));
if (value.getClass().isArray()) {
ListExpression elementExprs = new ListExpression();
int len = Array.getLength(value);
for (int i = 0; i != len; ++i) elementExprs.addExpression(annotationValueToExpression(Array.get(value, i)));
return elementExprs;
}
return null;
}
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(";");
}
Aggregations