use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class InnerClassCompletionVisitor method getFirstIfSpecialConstructorCall.
private ConstructorCallExpression getFirstIfSpecialConstructorCall(BlockStatement code) {
if (code == null)
return null;
final List<Statement> statementList = code.getStatements();
if (statementList.isEmpty())
return null;
final Statement statement = statementList.get(0);
if (!(statement instanceof ExpressionStatement))
return null;
Expression expression = ((ExpressionStatement) statement).getExpression();
if (!(expression instanceof ConstructorCallExpression))
return null;
ConstructorCallExpression cce = (ConstructorCallExpression) expression;
if (cce.isSpecialCall())
return cce;
return null;
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class InnerClassVisitor method passThisReference.
// this is the counterpart of addThisReference(). To non-static inner classes, outer this should be
// passed as the first argument implicitly.
private void passThisReference(ConstructorCallExpression call) {
ClassNode cn = call.getType().redirect();
if (!shouldHandleImplicitThisForInnerClass(cn))
return;
boolean isInStaticContext = true;
if (currentMethod != null)
isInStaticContext = currentMethod.getVariableScope().isInStaticContext();
else if (currentField != null)
isInStaticContext = currentField.isStatic();
else if (processingObjInitStatements)
isInStaticContext = false;
// if constructor call is not in static context, return
if (isInStaticContext) {
// constructor call is in static context and the inner class is non-static - 1st arg is supposed to be
// passed as enclosing "this" instance
//
Expression args = call.getArguments();
if (args instanceof TupleExpression && ((TupleExpression) args).getExpressions().isEmpty()) {
addError("No enclosing instance passed in constructor call of a non-static inner class", call);
}
return;
}
insertThis0ToSuperCall(call, cn);
}
use of org.codehaus.groovy.ast.expr.Expression in project grails-core by grails.
the class DefaultASTDatabindingHelper method getPropertyNamesToIncludeInWhiteList.
private Set<String> getPropertyNamesToIncludeInWhiteList(final SourceUnit sourceUnit, final ClassNode classNode) {
final Set<String> propertyNamesToIncludeInWhiteList = new HashSet<String>();
final Set<String> unbindablePropertyNames = new HashSet<String>();
final Set<String> bindablePropertyNames = new HashSet<String>();
if (!classNode.getSuperClass().equals(new ClassNode(Object.class))) {
final Set<String> parentClassPropertyNames = getPropertyNamesToIncludeInWhiteListForParentClass(sourceUnit, classNode.getSuperClass());
bindablePropertyNames.addAll(parentClassPropertyNames);
}
final FieldNode constraintsFieldNode = classNode.getDeclaredField(CONSTRAINTS_FIELD_NAME);
if (constraintsFieldNode != null && constraintsFieldNode.hasInitialExpression()) {
final Expression constraintsInitialExpression = constraintsFieldNode.getInitialExpression();
if (constraintsInitialExpression instanceof ClosureExpression) {
final Map<String, Map<String, Expression>> constraintsInfo = GrailsASTUtils.getConstraintMetadata((ClosureExpression) constraintsInitialExpression);
for (Entry<String, Map<String, Expression>> constraintConfig : constraintsInfo.entrySet()) {
final String propertyName = constraintConfig.getKey();
final Map<String, Expression> mapEntryExpressions = constraintConfig.getValue();
for (Entry<String, Expression> entry : mapEntryExpressions.entrySet()) {
final String constraintName = entry.getKey();
if (BINDABLE_CONSTRAINT_NAME.equals(constraintName)) {
final Expression valueExpression = entry.getValue();
Boolean bindableValue = null;
if (valueExpression instanceof ConstantExpression) {
final Object constantValue = ((ConstantExpression) valueExpression).getValue();
if (constantValue instanceof Boolean) {
bindableValue = (Boolean) constantValue;
}
}
if (bindableValue != null) {
if (Boolean.TRUE.equals(bindableValue)) {
unbindablePropertyNames.remove(propertyName);
bindablePropertyNames.add(propertyName);
} else {
bindablePropertyNames.remove(propertyName);
unbindablePropertyNames.add(propertyName);
}
} else {
GrailsASTUtils.warning(sourceUnit, valueExpression, "The bindable constraint for property [" + propertyName + "] in class [" + classNode.getName() + "] has a value which is not a boolean literal and will be ignored.");
}
}
}
}
}
}
final Set<String> fieldsInTransientsList = getPropertyNamesExpressedInTransientsList(classNode);
propertyNamesToIncludeInWhiteList.addAll(bindablePropertyNames);
final List<FieldNode> fields = classNode.getFields();
for (FieldNode fieldNode : fields) {
final String fieldName = fieldNode.getName();
final boolean isDomainClass = GrailsASTUtils.isDomainClass(classNode, sourceUnit);
if ((!unbindablePropertyNames.contains(fieldName)) && (bindablePropertyNames.contains(fieldName) || shouldFieldBeInWhiteList(fieldNode, fieldsInTransientsList, isDomainClass))) {
propertyNamesToIncludeInWhiteList.add(fieldName);
}
}
final Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
for (Entry<String, MethodNode> methodEntry : declaredMethodsMap.entrySet()) {
final MethodNode value = methodEntry.getValue();
if (classNode.equals(value.getDeclaringClass())) {
Parameter[] parameters = value.getParameters();
if (parameters != null && parameters.length == 1) {
final String methodName = value.getName();
if (methodName.startsWith("set")) {
final Parameter parameter = parameters[0];
final ClassNode paramType = parameter.getType();
if (!paramType.equals(new ClassNode(Object.class))) {
final String restOfMethodName = methodName.substring(3);
final String propertyName = GrailsNameUtils.getPropertyName(restOfMethodName);
if (!unbindablePropertyNames.contains(propertyName)) {
propertyNamesToIncludeInWhiteList.add(propertyName);
}
}
}
}
}
}
CLASS_NODE_TO_WHITE_LIST_PROPERTY_NAMES.put(classNode, propertyNamesToIncludeInWhiteList);
Map<String, ClassNode> allAssociationMap = GrailsASTUtils.getAllAssociationMap(classNode);
for (String associationName : allAssociationMap.keySet()) {
if (!propertyNamesToIncludeInWhiteList.contains(associationName) && !unbindablePropertyNames.contains(associationName)) {
propertyNamesToIncludeInWhiteList.add(associationName);
}
}
return propertyNamesToIncludeInWhiteList;
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class InvokeDynamicWriter method makeIndyCall.
private void makeIndyCall(MethodCallerMultiAdapter adapter, Expression receiver, boolean implicitThis, boolean safe, String methodName, Expression arguments) {
OperandStack operandStack = controller.getOperandStack();
String sig = prepareIndyCall(receiver, implicitThis);
// load arguments
int numberOfArguments = 1;
ArgumentListExpression ae = makeArgumentList(arguments);
boolean containsSpreadExpression = AsmClassGenerator.containsSpreadExpression(arguments);
if (containsSpreadExpression) {
controller.getAcg().despreadList(ae.getExpressions(), true);
sig += getTypeDescription(Object[].class);
} else {
for (Expression arg : ae.getExpressions()) {
arg.visit(controller.getAcg());
if (arg instanceof CastExpression) {
operandStack.box();
controller.getAcg().loadWrapper(arg);
sig += getTypeDescription(Wrapper.class);
} else {
sig += getTypeDescription(operandStack.getTopOperand());
}
numberOfArguments++;
}
}
sig += ")Ljava/lang/Object;";
String callSiteName = METHOD.getCallSiteName();
if (adapter == null)
callSiteName = INIT.getCallSiteName();
int flags = getMethodCallFlags(adapter, safe, containsSpreadExpression);
finishIndyCall(BSM, callSiteName, sig, numberOfArguments, methodName, flags);
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class StatementWriter method writeExpressionStatement.
public void writeExpressionStatement(ExpressionStatement statement) {
controller.getAcg().onLineNumber(statement, "visitExpressionStatement: " + statement.getExpression().getClass().getName());
writeStatementLabel(statement);
Expression expression = statement.getExpression();
int mark = controller.getOperandStack().getStackLength();
expression.visit(controller.getAcg());
controller.getOperandStack().popDownTo(mark);
}
Aggregations