private static int getModifiers(FieldNode fNode) { int mods = ACC_PUBLIC; if (fNode.isStatic()) mods |= ACC_STATIC; return mods; }
public boolean isInStaticContext() { return isStatic(); }
public static List<FieldNode> getInstanceNonPropertyFields(ClassNode cNode) { final List<FieldNode> result = new ArrayList<FieldNode>(); for (FieldNode fNode : cNode.getFields()) { if (!fNode.isStatic() && cNode.getProperty(fNode.getName()) == null) { result.add(fNode); } } return result; }
private void checkFieldAccess(ASTNode context, FieldNode field) { if (AstUtil.hasAnnotation(field, Shared.class) || field.isStatic()) return; resources.getErrorReporter().error(context, "Only @Shared and static fields may be accessed from here"); } }
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); } }
public void visitField(FieldNode node) { pushState(node.isStatic()); super.visitField(node); popState(); }
private boolean isStaticThis(InnerClassNode innerClass, VariableScope scope) { if (inClosure) return false; boolean ret = innerClass.isStaticClass(); if ( innerClass.getEnclosingMethod()!=null) { ret = ret || innerClass.getEnclosingMethod().isStatic(); } else if (currentField!=null) { ret = ret || currentField.isStatic(); } else if (currentMethod!=null && "<clinit>".equals(currentMethod.getName())) { ret = true; } return ret; }
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; }
@Override public void visitField(FieldNode gField) { PropertyNode owner = spec.getAst().getProperty(gField.getName()); if (gField.isStatic()) return; Field field = new Field(spec, gField, fieldCount++); field.setShared(AstUtil.hasAnnotation(gField, Shared.class)); field.setOwner(owner); spec.getFields().add(field); }
private static void addMethod(FieldNode fieldNode, BlockStatement body, ClassNode type) { int visibility = ACC_PUBLIC; if (fieldNode.isStatic()) visibility |= ACC_STATIC; String propName = MetaClassHelper.capitalize(fieldNode.getName().substring(1)); ClassNode declaringClass = fieldNode.getDeclaringClass(); addGeneratedMethod(declaringClass, "get" + propName, visibility, type, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body); if (ClassHelper.boolean_TYPE.equals(type)) { addGeneratedMethod(declaringClass, "is" + propName, visibility, type, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, stmt(callThisX("get" + propName))); } }
private static Expression findStaticField(ClassNode staticImportType, String fieldName) { if (staticImportType.isPrimaryClassNode() || staticImportType.isResolved()) { FieldNode field = getField(staticImportType, fieldName); if (field != null && field.isStatic()) return new PropertyExpression(new ClassExpression(staticImportType), fieldName); } return 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 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 boolean storeField(FieldNode field, boolean returnTrueIfFieldExists, PropertyExpression expressionToStoreOn, ClassNode receiver, ClassCodeVisitorSupport visitor, String delegationData, boolean lhsOfAssignment) { if (field == null || !returnTrueIfFieldExists) return false; if (visitor != null) visitor.visitField(field); storeWithResolve(field.getOriginType(), receiver, field.getDeclaringClass(), field.isStatic(), expressionToStoreOn); checkOrMarkPrivateAccess(expressionToStoreOn, field, lhsOfAssignment); if (delegationData != null) { expressionToStoreOn.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, delegationData); } return true; }
private Expression transformFieldExpression(final FieldExpression exp) { FieldNode field = exp.getField(); MethodCallExpression mce = new MethodCallExpression( createFieldHelperReceiver(), Traits.helperGetterName(field), ArgumentListExpression.EMPTY_ARGUMENTS ); mce.setSourcePosition(exp); mce.setImplicitThis(false); markDynamicCall(mce, field, field.isStatic()); return mce; }
private static void create(FieldNode fieldNode, final Expression initExpr) { final BlockStatement body = new BlockStatement(); if (fieldNode.isStatic()) { addHolderClassIdiomBody(body, fieldNode, initExpr); } else if (fieldNode.isVolatile()) { addDoubleCheckedLockingBody(body, fieldNode, initExpr); } else { addNonThreadSafeBody(body, fieldNode, initExpr); } addMethod(fieldNode, body, fieldNode.getType()); }
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); } } }
private void addStaticVariableError(VariableExpression ve) { // closures are always dynamic // propertyExpressions will handle the error a bit differently if (!inSpecialConstructorCall && (inClosure || !ve.isInStaticContext())) return; if (ve.isThisExpression() || ve.isSuperExpression()) return; Variable v = ve.getAccessedVariable(); if (currentMethod != null && currentMethod.isStatic()) { FieldNode fieldNode = getDeclaredOrInheritedField(currentMethod.getDeclaringClass(), ve.getName()); if (fieldNode != null && fieldNode.isStatic()) return; } if (v != null && !(v instanceof DynamicVariable) && v.isInStaticContext()) return; addVariableError(ve); }
public void visit(MethodVisitor mv) { if (field.isStatic()) { mv.visitFieldInsn(GETSTATIC, BytecodeHelper.getClassInternalName(classNode), field.getName(), BytecodeHelper.getTypeDescription(field.getType())); } else { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, BytecodeHelper.getClassInternalName(classNode), field.getName(), BytecodeHelper.getTypeDescription(field.getType())); } BytecodeHelper.doReturn(mv, field.getType()); } });
public void visit(MethodVisitor mv) { if (field.isStatic()) { BytecodeHelper.load(mv, field.getType(), 0); mv.visitFieldInsn(PUTSTATIC, BytecodeHelper.getClassInternalName(classNode), field.getName(), BytecodeHelper.getTypeDescription(field.getType())); } else { mv.visitVarInsn(ALOAD, 0); BytecodeHelper.load(mv, field.getType(), 1); mv.visitFieldInsn(PUTFIELD, BytecodeHelper.getClassInternalName(classNode), field.getName(), BytecodeHelper.getTypeDescription(field.getType())); } mv.visitInsn(RETURN); } });