use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
the class TupleConstructorASTTransformation method processArgsBlock.
private static BlockStatement processArgsBlock(ClassNode cNode, VariableExpression namedArgs) {
BlockStatement block = new BlockStatement();
for (PropertyNode pNode : cNode.getProperties()) {
if (pNode.isStatic())
continue;
// if namedArgs.containsKey(propertyName) setProperty(propertyName, namedArgs.get(propertyName));
Statement ifStatement = ifS(callX(namedArgs, "containsKey", constX(pNode.getName())), assignS(varX(pNode), propX(namedArgs, pNode.getName())));
block.addStatement(ifStatement);
}
block.addStatement(stmt(callX(CHECK_METHOD_TYPE, "checkPropNames", args(varX("this"), namedArgs))));
return block;
}
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);
}
use of org.codehaus.groovy.ast.PropertyNode in project grails-core by grails.
the class DefaultGrailsDomainClassInjector method injectAssociations.
private void injectAssociations(ClassNode classNode) {
List<PropertyNode> propertiesToAdd = new ArrayList<PropertyNode>();
for (PropertyNode propertyNode : classNode.getProperties()) {
final String name = propertyNode.getName();
final boolean isHasManyProperty = name.equals(GormProperties.HAS_MANY);
if (isHasManyProperty) {
Expression e = propertyNode.getInitialExpression();
propertiesToAdd.addAll(createPropertiesForHasManyExpression(e, classNode));
}
final boolean isBelongsToOrHasOne = name.equals(GormProperties.BELONGS_TO) || name.equals(GormProperties.HAS_ONE);
if (isBelongsToOrHasOne) {
Expression initialExpression = propertyNode.getInitialExpression();
if ((!(initialExpression instanceof MapExpression)) && (!(initialExpression instanceof ClassExpression))) {
if (name.equals(GormProperties.HAS_ONE)) {
final String message = "WARNING: The hasOne property in class [" + classNode.getName() + "] should have an initial expression of type Map or Class.";
System.err.println(message);
} else if (!(initialExpression instanceof ListExpression)) {
final String message = "WARNING: The belongsTo property in class [" + classNode.getName() + "] should have an initial expression of type List, Map or Class.";
System.err.println(message);
}
}
propertiesToAdd.addAll(createPropertiesForBelongsToOrHasOneExpression(initialExpression, classNode));
}
}
injectAssociationProperties(classNode, propertiesToAdd);
}
use of org.codehaus.groovy.ast.PropertyNode in project grails-core by grails.
the class DefaultGrailsDomainClassInjector method createPropertiesForBelongsToOrHasOneExpression.
private Collection<PropertyNode> createPropertiesForBelongsToOrHasOneExpression(Expression e, ClassNode classNode) {
List<PropertyNode> properties = new ArrayList<>();
if (e instanceof MapExpression) {
MapExpression me = (MapExpression) e;
for (MapEntryExpression mme : me.getMapEntryExpressions()) {
String key = mme.getKeyExpression().getText();
final Expression expression = mme.getValueExpression();
ClassNode type;
if (expression instanceof ClassExpression) {
type = expression.getType();
} else {
type = ClassHelper.make(expression.getText());
}
properties.add(new PropertyNode(key, Modifier.PUBLIC, type, classNode, null, null, null));
}
}
return properties;
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
the class VetoableASTTransformation method addListenerToProperty.
private void addListenerToProperty(SourceUnit source, AnnotationNode node, AnnotatedNode parent) {
ClassNode declaringClass = parent.getDeclaringClass();
FieldNode field = ((FieldNode) parent);
String fieldName = field.getName();
for (PropertyNode propertyNode : declaringClass.getProperties()) {
boolean bindable = BindableASTTransformation.hasBindableAnnotation(parent) || BindableASTTransformation.hasBindableAnnotation(parent.getDeclaringClass());
if (propertyNode.getName().equals(fieldName)) {
if (field.isStatic()) {
//noinspection ThrowableInstanceNeverThrown
source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Vetoable cannot annotate a static property.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
} else {
createListenerSetter(source, bindable, declaringClass, propertyNode);
}
return;
}
}
//noinspection ThrowableInstanceNeverThrown
source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Vetoable must be on a property, not a field. Try removing the private, protected, or public modifier.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
}
Aggregations