use of org.codehaus.groovy.ast.FieldNode in project groovy-core by groovy.
the class AsmClassGenerator method storeThisInstanceField.
private void storeThisInstanceField(FieldExpression expression) {
MethodVisitor mv = controller.getMethodVisitor();
FieldNode field = expression.getField();
boolean setReferenceFromReference = field.isHolder() && expression.isUseReferenceDirectly();
String ownerName = (field.getOwner().equals(controller.getClassNode())) ? controller.getInternalClassName() : BytecodeHelper.getClassInternalName(field.getOwner());
OperandStack operandStack = controller.getOperandStack();
if (setReferenceFromReference) {
// rhs is ready to use reference, just put it in the field
mv.visitVarInsn(ALOAD, 0);
operandStack.push(controller.getClassNode());
operandStack.swap();
mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
} else if (field.isHolder()) {
// rhs is normal value, set the value in the Reference
operandStack.doGroovyCast(field.getOriginType());
operandStack.box();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, ownerName, expression.getFieldName(), BytecodeHelper.getTypeDescription(field.getType()));
mv.visitInsn(SWAP);
mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
} else {
// rhs is normal value, set normal value
operandStack.doGroovyCast(field.getOriginType());
mv.visitVarInsn(ALOAD, 0);
operandStack.push(controller.getClassNode());
operandStack.swap();
mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
}
}
use of org.codehaus.groovy.ast.FieldNode in project groovy-core by groovy.
the class EnumVisitor method completeEnum.
private void completeEnum(ClassNode enumClass) {
boolean isAic = isAnonymousInnerClass(enumClass);
// create MIN_VALUE and MAX_VALUE fields
FieldNode minValue = null, maxValue = null, values = null;
if (!isAic) {
ClassNode enumRef = enumClass.getPlainNodeReference();
// create values field
values = new FieldNode("$VALUES", PRIVATE_FS | Opcodes.ACC_SYNTHETIC, enumRef.makeArray(), enumClass, null);
values.setSynthetic(true);
addMethods(enumClass, values);
checkForAbstractMethods(enumClass);
// create MIN_VALUE and MAX_VALUE fields
minValue = new FieldNode("MIN_VALUE", PUBLIC_FS, enumRef, enumClass, null);
maxValue = new FieldNode("MAX_VALUE", PUBLIC_FS, enumRef, enumClass, null);
}
addInit(enumClass, minValue, maxValue, values, isAic);
}
use of org.codehaus.groovy.ast.FieldNode in project grails-core by grails.
the class DefaultASTDatabindingHelper method addDefaultDatabindingWhitelistField.
private void addDefaultDatabindingWhitelistField(final SourceUnit sourceUnit, final ClassNode classNode) {
final FieldNode defaultWhitelistField = classNode.getDeclaredField(DEFAULT_DATABINDING_WHITELIST);
if (defaultWhitelistField != null) {
return;
}
final Set<String> propertyNamesToIncludeInWhiteList = getPropertyNamesToIncludeInWhiteList(sourceUnit, classNode);
final ListExpression listExpression = new ListExpression();
if (propertyNamesToIncludeInWhiteList.size() > 0) {
for (String propertyName : propertyNamesToIncludeInWhiteList) {
listExpression.addExpression(new ConstantExpression(propertyName));
final FieldNode declaredField = getDeclaredFieldInInheritanceHierarchy(classNode, propertyName);
boolean isSimpleType = false;
if (declaredField != null) {
final ClassNode type = declaredField.getType();
if (type != null) {
isSimpleType = SIMPLE_TYPES.contains(type);
}
}
if (!isSimpleType) {
listExpression.addExpression(new ConstantExpression(propertyName + "_*"));
listExpression.addExpression(new ConstantExpression(propertyName + ".*"));
}
}
} else {
listExpression.addExpression(new ConstantExpression(NO_BINDABLE_PROPERTIES));
}
classNode.addField(DEFAULT_DATABINDING_WHITELIST, Modifier.STATIC | Modifier.PUBLIC | Modifier.FINAL, new ClassNode(List.class), listExpression);
}
use of org.codehaus.groovy.ast.FieldNode 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.FieldNode in project groovy-core by groovy.
the class OptimizerVisitor method setConstField.
private void setConstField(ConstantExpression constantExpression) {
final Object n = constantExpression.getValue();
if (!(n instanceof Number))
return;
if (n instanceof Integer || n instanceof Double)
return;
// LCONST_0, LCONST_1
if (n instanceof Long && (0L == (Long) n || 1L == (Long) n))
return;
FieldNode field = (FieldNode) const2Var.get(n);
if (field != null) {
constantExpression.setConstantName(field.getName());
return;
}
final String name = "$const$" + const2Var.size();
//TODO: this part here needs a bit of rethinking. If it can happen that the field is defined already,
// then is this code still valid?
field = currentClass.getDeclaredField(name);
if (field == null) {
field = new FieldNode(name, Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_FINAL, constantExpression.getType(), currentClass, constantExpression);
field.setSynthetic(true);
missingFields.add(field);
}
constantExpression.setConstantName(field.getName());
const2Var.put(n, field);
}
Aggregations