@Override public Statement createPropInit(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, PropertyNode pNode, Parameter namedArgsMap) { FieldNode fNode = pNode.getField(); if (fNode.isFinal() && fNode.isStatic()) return null; if (fNode.isFinal() && fNode.getInitialExpression() != null) { return checkFinalArgNotOverridden(cNode, fNode); } return createConstructorStatement(xform, cNode, pNode, namedArgsMap != null); }
@Override public Statement createPropInit(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, PropertyNode pNode, Parameter namedArgsMap) { FieldNode fNode = pNode.getField(); if (fNode.isFinal() && fNode.isStatic()) return null; if (fNode.isFinal() && fNode.getInitialExpression() != null) { return checkFinalArgNotOverridden(cNode, fNode); } return createLegacyConstructorStatementMapSpecial(fNode); } }
private static void ensureNotPublic(AbstractASTTransformation xform, String cNode, FieldNode fNode) { String fName = fNode.getName(); // TODO: do we need to lock down things like: $ownClass if (fNode.isPublic() && !fName.contains("$") && !(fNode.isStatic() && fNode.isFinal())) { xform.addError("Public field '" + fName + "' not allowed for " + MY_TYPE_NAME + " class '" + cNode + "'.", fNode); } }
private static Expression findConstant(FieldNode fn) { if (fn != null && !fn.isEnum() && fn.isStatic() && fn.isFinal()) { if (fn.getInitialValueExpression() instanceof ConstantExpression) { return fn.getInitialValueExpression(); } } return null; }
continue; if (fNode.isFinal() && fNode.getInitialExpression() != null && skipReadonly) { continue;
&& fieldNode.isStatic() && fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(valueExpr.getType()) && valueExpr.getType().equals(fieldNode.getType())) {
@Override public Statement createPropInit(AbstractASTTransformation xform, AnnotationNode anno, ClassNode cNode, PropertyNode pNode, Parameter namedArgsMap) { String name = pNode.getName(); FieldNode fNode = pNode.getField(); boolean useSetters = xform.memberHasValue(anno, "useSetters", true); boolean hasSetter = cNode.getProperty(name) != null && !fNode.isFinal(); if (namedArgsMap != null) { return assignFieldS(useSetters, namedArgsMap, name); } else { Expression var = varX(name); if (useSetters && hasSetter) { return setViaSetterS(name, var); } else { return assignToFieldS(name, var); } } }
private void addListenerToClass(SourceUnit source, ClassNode classNode) { boolean bindable = BindableASTTransformation.hasBindableAnnotation(classNode); for (PropertyNode propertyNode : classNode.getProperties()) { if (!hasVetoableAnnotation(propertyNode.getField()) && !propertyNode.getField().isFinal() && !propertyNode.getField().isStatic()) { createListenerSetter(source, bindable || BindableASTTransformation.hasBindableAnnotation(propertyNode.getField()), classNode, propertyNode); } } }
if (fn != null && fn.isStatic() && fn.isFinal()) { Expression ce2 = transformInlineConstants(fn.getInitialValueExpression(), attrType); if (ce2 != null) { if (ve.getAccessedVariable() instanceof FieldNode) { FieldNode fn = (FieldNode) ve.getAccessedVariable(); if (fn.isStatic() && fn.isFinal()) { Expression ce = transformInlineConstants(fn.getInitialValueExpression(), attrType); if (ce != null) {
Parameter parameter = param(field.getType(), fieldName); buildee.addMethod(methodName, Opcodes.ACC_PUBLIC, newClass(buildee), params(parameter), NO_EXCEPTIONS, block( stmt(useSetters && !field.isFinal() ? callThisX(getSetterName("set", fieldName), varX(parameter)) : assignX(fieldX(field), varX(parameter))
private void checkFinalFieldAccess(Expression expression) { if (!(expression instanceof VariableExpression) && !(expression instanceof PropertyExpression)) return; Variable v = null; if (expression instanceof VariableExpression) { VariableExpression ve = (VariableExpression) expression; v = ve.getAccessedVariable(); } else { PropertyExpression propExp = ((PropertyExpression) expression); Expression objectExpression = propExp.getObjectExpression(); if (objectExpression instanceof VariableExpression) { VariableExpression varExp = (VariableExpression) objectExpression; if (varExp.isThisExpression()) { v = currentClass.getDeclaredField(propExp.getPropertyAsString()); } } } if (v instanceof FieldNode) { FieldNode fn = (FieldNode) v; /* * if it is static final but not accessed inside a static constructor, or, * if it is an instance final but not accessed inside a instance constructor, it is an error */ boolean isFinal = fn.isFinal(); boolean isStatic = fn.isStatic(); boolean error = isFinal && ((isStatic && !inStaticConstructor) || (!isStatic && !inConstructor)); if (error) addError("cannot modify" + (isStatic ? " static" : "") + " final field '" + fn.getName() + "' outside of " + (isStatic ? "static initialization block." : "constructor."), expression); } }
ConstantExpression cexp = (ConstantExpression) transformed; cexp = transformToPrimitiveConstantIfPossible(cexp); if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) { fieldNode.setInitialValueExpression(transformed); return; // GROOVY-5150: primitive type constants will be initialized directly
private static void initializeFields(List<FieldNode> fields, BlockStatement body, boolean useSetters) { for (FieldNode field : fields) { String name = field.getName(); body.addStatement( stmt(useSetters && !field.isFinal() ? callThisX(getSetterName(name), varX(param(field.getType(), name))) : assignX(propX(varX("this"), field.getName()), varX(param(field.getType(), name))) ) ); } }
&& fieldNode.isStatic() && fieldNode.isFinal()
if (fieldNode.isFinal()) { String baseName = fieldNode.isStatic() ? Traits.STATIC_INIT_METHOD : Traits.INIT_METHOD; StaticMethodCallExpression mce = callX(helperClassNode, baseName + fieldNode.getName(), args(varX("this")));
cNode.addField(fieldNode); if (fieldNode.isFinal()) { if (!de.getAnnotations(OPTION_TYPE).isEmpty()) { addError("Can't have a final field also annotated with @" + OPTION_TYPE.getNameWithoutPackage(), de);
private void setPropertyOfSuperClass(ClassNode classNode, PropertyExpression expression, MethodVisitor mv) { String fieldName = expression.getPropertyAsString(); FieldNode fieldNode = classNode.getSuperClass().getField(fieldName); if (null == fieldNode) { throw new RuntimeParserException("Failed to find field[" + fieldName + "] of " + classNode.getName() + "'s super class", expression); } if (fieldNode.isFinal()) { throw new RuntimeParserException("Cannot modify final field[" + fieldName + "] of " + classNode.getName() + "'s super class", expression); } MethodNode setter = findSetterOfSuperClass(classNode, fieldNode); MethodNode getter = findGetterOfSuperClass(classNode, fieldNode); if (fieldNode.isPrivate() && !getterAndSetterExists(setter, getter)) { throw new RuntimeParserException("Cannot access private field[" + fieldName + "] of " + classNode.getName() + "'s super class", expression); } OperandStack operandStack = controller.getOperandStack(); operandStack.doAsType(fieldNode.getType()); mv.visitVarInsn(ALOAD, 0); operandStack.push(classNode); operandStack.swap(); String owner = BytecodeHelper.getClassInternalName(classNode.getSuperClass().getName()); String desc = BytecodeHelper.getTypeDescription(fieldNode.getType()); if (fieldNode.isPublic() || fieldNode.isProtected()) { mv.visitFieldInsn(PUTFIELD, owner, fieldName, desc); } else { mv.visitMethodInsn(INVOKESPECIAL, owner, setter.getName(), BytecodeHelper.getMethodDescriptor(setter), false); } }
ExpressionStatement initCode = new ExpressionStatement(initialExpression); processBody(thisObject, initCode, trait, helper, fieldHelper, knownFields); if (field.isFinal()) { String baseName = field.isStatic() ? Traits.STATIC_INIT_METHOD : Traits.INIT_METHOD; MethodNode fieldInitializer = new MethodNode(
while (outer!=null) { outerClassField = outer.getDeclaredField(name); if (outerClassField!=null && outerClassField.isStatic() && outerClassField.isFinal()) { if (outer!=classNode.getOuterClass() && outerClassField.isPrivate()) { throw new GroovyBugError("Trying to access private constant field ["+outerClassField.getDeclaringClass()+"#"+outerClassField.getName()+"] from inner class");
private void ensureNotPublic(String cNode, FieldNode fNode) { String fName = fNode.getName(); // TODO: do we need to lock down things like: $ownClass if (fNode.isPublic() && !fName.contains("$") && !(fNode.isStatic() && fNode.isFinal())) { addError("Public field '" + fName + "' not allowed for " + MY_TYPE_NAME + " class '" + cNode + "'.", fNode); } }