use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.
the class AntlrParserPlugin method fieldDef.
protected void fieldDef(AST fieldDef) {
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = fieldDef.getFirstChild();
int modifiers = 0;
if (isType(MODIFIERS, node)) {
modifiers = modifiers(node, annotations, modifiers);
node = node.getNextSibling();
}
if (classNode.isInterface()) {
modifiers |= Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
if ((modifiers & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
modifiers |= Opcodes.ACC_PUBLIC;
}
}
ClassNode type = null;
if (isType(TYPE, node)) {
type = makeTypeWithArguments(node);
node = node.getNextSibling();
}
String name = identifier(node);
node = node.getNextSibling();
Expression initialValue = null;
if (node != null) {
assertNodeType(ASSIGN, node);
initialValue = expression(node.getFirstChild());
}
if (classNode.isInterface() && initialValue == null && type != null) {
initialValue = getDefaultValueForPrimitive(type);
}
FieldNode fieldNode = new FieldNode(name, modifiers, type, classNode, initialValue);
fieldNode.addAnnotations(annotations);
configureAST(fieldNode, fieldDef);
if (!hasVisibility(modifiers)) {
// let's set the modifiers on the field
int fieldModifiers = 0;
int flags = Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT | Opcodes.ACC_VOLATILE | Opcodes.ACC_FINAL;
if (!hasVisibility(modifiers)) {
modifiers |= Opcodes.ACC_PUBLIC;
fieldModifiers |= Opcodes.ACC_PRIVATE;
}
// let's pass along any other modifiers we need
fieldModifiers |= (modifiers & flags);
fieldNode.setModifiers(fieldModifiers);
fieldNode.setSynthetic(true);
// in the case that there is already a field, we would
// like to use that field, instead of the default field
// for the property
FieldNode storedNode = classNode.getDeclaredField(fieldNode.getName());
if (storedNode != null && !classNode.hasProperty(name)) {
fieldNode = storedNode;
// we remove it here, because addProperty will add it
// again and we want to avoid it showing up multiple
// times in the fields list.
classNode.getFields().remove(storedNode);
}
PropertyNode propertyNode = new PropertyNode(fieldNode, modifiers, null, null);
configureAST(propertyNode, fieldDef);
classNode.addProperty(propertyNode);
} else {
fieldNode.setModifiers(modifiers);
// if there is a property of that name, then a field of that
// name already exists, which means this new field here should
// be used instead of the field the property originally has.
PropertyNode pn = classNode.getProperty(name);
if (pn != null && pn.getField().isSynthetic()) {
classNode.getFields().remove(pn.getField());
pn.setField(fieldNode);
}
classNode.addField(fieldNode);
}
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.
the class ClassNodeUtils method hasPossibleStaticProperty.
/**
* Return true if we have a static accessor
*/
public static boolean hasPossibleStaticProperty(ClassNode candidate, String methodName) {
// assume explicit static method call checked first so we can assume a simple check here
if (!methodName.startsWith("get") && !methodName.startsWith("is")) {
return false;
}
String propName = getPropNameForAccessor(methodName);
PropertyNode pNode = getStaticProperty(candidate, propName);
return pNode != null && (methodName.startsWith("get") || boolean_TYPE.equals(pNode.getType()));
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.
the class ImmutableASTTransformation method createConstructorOrdered.
private static void createConstructorOrdered(ClassNode cNode, List<PropertyNode> list) {
final MapExpression argMap = new MapExpression();
final Parameter[] orderedParams = new Parameter[list.size()];
int index = 0;
for (PropertyNode pNode : list) {
Parameter param = new Parameter(pNode.getField().getType(), pNode.getField().getName());
orderedParams[index++] = param;
argMap.addMapEntryExpression(constX(pNode.getName()), varX(pNode.getName()));
}
final BlockStatement orderedBody = new BlockStatement();
orderedBody.addStatement(stmt(ctorX(ClassNode.THIS, args(castX(HASHMAP_TYPE, argMap)))));
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, orderedParams, ClassNode.EMPTY_ARRAY, orderedBody));
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.
the class ImmutableASTTransformation method createConstructorMap.
private void createConstructorMap(ClassNode cNode, List<PropertyNode> list, List<String> knownImmutableClasses, List<String> knownImmutables) {
final BlockStatement body = new BlockStatement();
body.addStatement(ifS(equalsNullX(varX("args")), assignS(varX("args"), new MapExpression())));
for (PropertyNode pNode : list) {
body.addStatement(createConstructorStatement(cNode, pNode, knownImmutableClasses, knownImmutables));
}
// check for missing properties
body.addStatement(stmt(callX(SELF_TYPE, "checkPropNames", args("this", "args"))));
createConstructorMapCommon(cNode, body);
if (!list.isEmpty()) {
createNoArgConstructor(cNode);
}
}
use of org.codehaus.groovy.ast.PropertyNode in project grails-core by grails.
the class AbstractGrailsArtefactTransformer method performInstanceImplementationInjection.
protected void performInstanceImplementationInjection(SourceUnit source, ClassNode classNode, Map<String, ClassNode> genericsPlaceholders, Class instanceImplementation) {
ClassNode implementationNode;
final ConstructorCallExpression constructorCallExpression;
try {
implementationNode = GrailsASTUtils.replaceGenericsPlaceholders(ClassHelper.make(instanceImplementation), genericsPlaceholders);
constructorCallExpression = GrailsASTUtils.hasZeroArgsConstructor(implementationNode) ? new ConstructorCallExpression(implementationNode, ZERO_ARGS) : null;
} catch (Throwable e) {
// this may well be ok, as we want to be able to compile against, for example, non servlet environments. In this case just bail out.
return;
}
String apiInstanceProperty = INSTANCE_PREFIX + instanceImplementation.getSimpleName();
Expression apiInstance = new VariableExpression(apiInstanceProperty, implementationNode);
if (requiresStaticLookupMethod()) {
final String lookupMethodName = CURRENT_PREFIX + instanceImplementation.getSimpleName();
MethodNode lookupMethod = createStaticLookupMethod(classNode, implementationNode, apiInstanceProperty, lookupMethodName);
apiInstance = new MethodCallExpression(new ClassExpression(classNode), lookupMethodName, ZERO_ARGS);
((MethodCallExpression) apiInstance).setMethodTarget(lookupMethod);
} else if (requiresAutowiring()) {
PropertyNode propertyNode = new PropertyNode(apiInstanceProperty, Modifier.PUBLIC, implementationNode, classNode, constructorCallExpression, null, null);
propertyNode.addAnnotation(AUTO_WIRED_ANNOTATION);
if (getMarkerAnnotation() != null) {
propertyNode.addAnnotation(getMarkerAnnotation());
}
classNode.addProperty(propertyNode);
} else {
FieldNode fieldNode = classNode.getField(apiInstanceProperty);
if (fieldNode == null || (Modifier.isPrivate(fieldNode.getModifiers()) && !fieldNode.getDeclaringClass().equals(classNode))) {
fieldNode = new FieldNode(apiInstanceProperty, PRIVATE_STATIC_MODIFIER, implementationNode, classNode, constructorCallExpression);
classNode.addField(fieldNode);
}
}
while (!implementationNode.equals(AbstractGrailsArtefactTransformer.OBJECT_CLASS)) {
List<MethodNode> declaredMethods = implementationNode.getMethods();
for (MethodNode declaredMethod : declaredMethods) {
if (GrailsASTUtils.isConstructorMethod(declaredMethod)) {
GrailsASTUtils.addDelegateConstructor(classNode, declaredMethod, genericsPlaceholders);
} else if (isCandidateInstanceMethod(classNode, declaredMethod)) {
addDelegateInstanceMethod(classNode, apiInstance, declaredMethod, getMarkerAnnotation(), genericsPlaceholders);
}
}
implementationNode = implementationNode.getSuperClass();
}
performInjectionInternal(apiInstanceProperty, source, classNode);
}
Aggregations