Refine search
private static Expression getInitExpr(ErrorCollecting xform, FieldNode fieldNode) { Expression initExpr = fieldNode.getInitialValueExpression(); fieldNode.setInitialValueExpression(null); if (initExpr == null || initExpr instanceof EmptyExpression) { if (fieldNode.getType().isAbstract()) { xform.addError("You cannot lazily initialize '" + fieldNode.getName() + "' from the abstract class '" + fieldNode.getType().getName() + "'", fieldNode); } initExpr = ctorX(fieldNode.getType()); } return initExpr; } }
private static FieldNode createFieldCopy(ClassNode buildee, FieldNode fNode) { Map<String,ClassNode> genericsSpec = createGenericsSpec(fNode.getDeclaringClass()); extractSuperClassGenerics(fNode.getType(), buildee, genericsSpec); ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, fNode.getType()); return new FieldNode(fNode.getName(), fNode.getModifiers(), correctedType, buildee, DEFAULT_INITIAL_VALUE); }
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; }
public static void setVisibility(FieldNode field, int visibility) { int modifiers = field.getModifiers(); modifiers &= ~(Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE); field.setModifiers(modifiers | visibility); }
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; }
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); } }
code.addStatement(new ReturnStatement(cce)); initMethod.setCode(code); enumClass.addMethod(initMethod); FieldNode tempMax = null; for (FieldNode field : fields) { if ((field.getModifiers() & Opcodes.ACC_ENUM) == 0) continue; value++; if (tempMin == null) tempMin = field; args.addExpression(new ConstantExpression(field.getName())); args.addExpression(new ConstantExpression(value)); if (field.getInitialExpression() == null) { if ((enumClass.getModifiers() & Opcodes.ACC_ABSTRACT) != 0) { addError(field, "The enum constant " + field.getName() + " must override abstract methods from " + enumBase.getName() + "."); continue; ListExpression oldArgs = (ListExpression) field.getInitialExpression(); List<MapEntryExpression> savedMapEntries = new ArrayList<MapEntryExpression>(); for (Expression exp : oldArgs.getExpressions()) { MethodNode enumConstMethod = inner.getMethod(methodNode.getName(), methodNode.getParameters()); if (enumConstMethod == null || (enumConstMethod.getModifiers() & Opcodes.ACC_ABSTRACT) != 0) { addError(field, "Can't have an abstract method in enum constant " + field.getName() + ". Implement method '" + methodNode.getTypeDescriptor() + "'."); field.setInitialValueExpression(null); block.add( new ExpressionStatement(
private void processField(final FieldNode field, final MethodNode initializer, final MethodNode staticInitializer, final ClassNode fieldHelper, final ClassNode helper, final ClassNode staticFieldHelper, final ClassNode trait, final Set<String> knownFields) { if (field.isProtected()) { unit.addError(new SyntaxException("Cannot have protected field in a trait (" + trait.getName() + "#" + field.getName() + ")", field.getLineNumber(), field.getColumnNumber())); return; Expression initialExpression = field.getInitialExpression(); MethodNode selectedMethod = field.isStatic()?staticInitializer:initializer; ClassNode target = field.isStatic() && staticFieldHelper != null ? staticFieldHelper : fieldHelper; if (initialExpression != null) { VariableExpression thisObject = new VariableExpression(selectedMethod.getParameters()[0]); 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( baseName + Traits.remappedFieldName(trait, field.getName()), ACC_STATIC | ACC_PUBLIC | ACC_SYNTHETIC, field.getOriginType(), new Parameter[]{createSelfParameter(trait, field.isStatic())}, ClassNode.EMPTY_ARRAY, returnS(initCode.getExpression()) ); helper.addMethod(fieldInitializer); } else { BlockStatement code = (BlockStatement) selectedMethod.getCode(); MethodCallExpression mce; if (field.isStatic()) {
delegate.delegate = fieldNode; delegate.annotation = node; delegate.name = fieldNode.getName(); delegate.type = fieldNode.getType(); delegate.owner = fieldNode.getOwner(); delegate.getOp = varX(fieldNode); delegate.origin = "field"; if (delegate.type.equals(ClassHelper.OBJECT_TYPE) || delegate.type.equals(GROOVYOBJECT_TYPE)) { addError(MY_TYPE_NAME + " " + delegate.origin + " '" + delegate.name + "' has an inappropriate type: " + delegate.type.getName() + ". Please add an explicit type but not java.lang.Object or groovy.lang.GroovyObject.", parent); return;
final List<PropertyNode> result = new ArrayList<PropertyNode>(); if (cNode != ClassHelper.OBJECT_TYPE && traverseSuperClasses && !reverse) { result.addAll(getAllProperties(names, origType, cNode.getSuperClass(), includeProperties, includeFields, includePseudoGetters, includePseudoSetters, true, skipReadonly)); for (PropertyNode pNode : cNode.getProperties()) { if ((!pNode.isStatic() || includeStatic) && !names.contains(pNode.getName())) { result.add(pNode); for (FieldNode fNode : cNode.getFields()) { if ((fNode.isStatic() && !includeStatic) || fNode.isSynthetic() || cNode.getProperty(fNode.getName()) != null || names.contains(fNode.getName())) { continue; if (fNode.getName().contains("$") && !allNames) { continue; if (fNode.isPrivate() && !cNode.equals(origType)) { continue; if (fNode.isFinal() && fNode.getInitialExpression() != null && skipReadonly) { continue; result.add(new PropertyNode(fNode, fNode.getModifiers(), null, null)); names.add(fNode.getName());
private static boolean moveOptimizedConstantsInitialization(final ClassNode node) { if (node.isInterface() && !Traits.isTrait(node)) return false; for (FieldNode fn : node.getFields()) { if (!fn.isStatic() || !fn.isSynthetic() || !fn.getName().startsWith("$const$")) continue; if (fn.getInitialExpression() == null) continue; final FieldExpression fe = new FieldExpression(fn); if (fn.getType().equals(ClassHelper.REFERENCE_TYPE)) fe.setUseReferenceDirectly(true); ConstantExpression init = (ConstantExpression) fn.getInitialExpression(); init = new ConstantExpression(init.getValue(), true); ExpressionStatement statement = new BinaryExpression( fe, Token.newSymbol(Types.EQUAL, fn.getLineNumber(), fn.getColumnNumber()), init)); fn.setInitialValueExpression(null); methodCode.addStatement(statement); swapInitRequired = true;
protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode, boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) { Expression expression = fieldNode.getInitialExpression(); if (expression != null) { final FieldExpression fe = new FieldExpression(fieldNode); if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0)) { fe.setUseReferenceDirectly(true); new BinaryExpression( fe, Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()), expression)); if (fieldNode.isStatic()) { Expression initialValueExpression = fieldNode.getInitialValueExpression(); Expression transformed = transformInlineConstants(initialValueExpression, fieldNode.getType()); if (transformed instanceof ConstantExpression) { 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 staticList.add(statement); fieldNode.setInitialValueExpression(null); // to avoid double initialization in case of several constructors if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) { initStmtsAfterEnumValuesInit.add(statement);
@Override public void visitClass(ClassNode node) { FieldNode logField = node.getField(logFieldName); if (logField != null && logField.getOwner().equals(node)) { addError("Class annotated with Log annotation cannot have log field declared", logField); } else if (logField != null && !Modifier.isPrivate(logField.getModifiers())) { addError("Class annotated with Log annotation cannot have log field declared because the field exists in the parent class: " + logField.getOwner().getName(), logField); } else { logNode = loggingStrategy.addLoggerFieldToClass(node, logFieldName, categoryName); } super.visitClass(node); }
static void visitField(ErrorCollecting xform, AnnotationNode node, FieldNode fieldNode) { final Expression soft = node.getMember("soft"); final Expression init = getInitExpr(xform, fieldNode); String backingFieldName = "$" + fieldNode.getName(); fieldNode.rename(backingFieldName); fieldNode.setModifiers(ACC_PRIVATE | (fieldNode.getModifiers() & (~(ACC_PUBLIC | ACC_PROTECTED)))); PropertyNode pNode = fieldNode.getDeclaringClass().getProperty(backingFieldName); if (pNode != null) { fieldNode.getDeclaringClass().getProperties().remove(pNode); } if (soft instanceof ConstantExpression && ((ConstantExpression) soft).getValue().equals(true)) { createSoft(fieldNode, init); } else { create(fieldNode, init); // @Lazy not meaningful with primitive so convert to wrapper if needed if (ClassHelper.isPrimitiveType(fieldNode.getType())) { fieldNode.setType(ClassHelper.getWrapper(fieldNode.getType())); } } }
if (isAttribute || (isThisExpression && receiverType.getDeclaredField(property)!=null)) { ClassNode current = receiverType; FieldNode fn = null; while (fn==null && current!=null) { fn = current.getDeclaredField(property); if (fn==null){ current = current.getSuperClass(); if (fn!=null && receiverType!=current && !fn.isPublic()) { if (!fn.isProtected()) { return false; if (!fn.isStatic()) { receiver.visit(controller.getAcg()); operandStack.doGroovyCast(fn.getOriginType()); mv.visitFieldInsn(fn.isStatic() ? PUTSTATIC : PUTFIELD, BytecodeHelper.getClassInternalName(fn.getOwner()), property, BytecodeHelper.getTypeDescription(fn.getOriginType())); operandStack.remove(fn.isStatic()?1:2); return true;
public void visitField(FieldNode fieldNode) { onLineNumber(fieldNode, "visitField: " + fieldNode.getName()); ClassNode t = fieldNode.getType(); String signature = BytecodeHelper.getGenericsBounds(t); Expression initialValueExpression = fieldNode.getInitialValueExpression(); ConstantExpression cexp = initialValueExpression instanceof ConstantExpression? (ConstantExpression) initialValueExpression :null; if (cexp!=null) { && cexp.getType().equals(t) && fieldNode.isStatic() && fieldNode.isFinal() if (ClassHelper.byte_TYPE.equals(t) || ClassHelper.short_TYPE.equals(t)) { value = ((Number) value).intValue(); } else if (ClassHelper.char_TYPE.equals(t)) { fieldNode.getModifiers(), fieldNode.getName(), BytecodeHelper.getTypeDescription(t), signature,
private void checkProps(List<FieldNode> list, List<String> excludes, boolean checkPropertyTypes) { for (FieldNode fNode : list) { if (excludes != null && excludes.contains(fNode.getName())) continue; if ((fNode.getModifiers() & ACC_TRANSIENT) != 0) continue; if ((fNode.getModifiers() & ACC_FINAL) != 0) { addError(MY_TYPE_NAME + ": The Externalizable property (or field) '" + fNode.getName() + "' cannot be final", fNode); } ClassNode propType = fNode.getType(); if (checkPropertyTypes && !isPrimitiveType(propType) && !implementsExternalizable(propType) && !implementsSerializable(propType)) { addError(MY_TYPE_NAME + ": strict type checking is enabled and the non-primitive property (or field) '" + fNode.getName() + "' in an Externalizable class has the type '" + propType.getName() + "' which isn't Externalizable or Serializable", fNode); } } }
private static FieldNode checkFieldDoesNotExist(ClassNode node, String fieldName) { FieldNode ret = node.getDeclaredField(fieldName); if (ret != null) { if (isPublic(ret.getModifiers()) && ret.getType().redirect() == ClassHelper.boolean_TYPE) { return ret; } throw new RuntimeParserException("The class " + node.getName() + " cannot declare field '" + fieldName + "' as this" + " field is needed for internal groovy purposes", ret); } return null; }
/** * RHS instance field. should move most of the code in the BytecodeHelper * * @param fldExp */ public void loadInstanceField(FieldExpression fldExp) { MethodVisitor mv = controller.getMethodVisitor(); FieldNode field = fldExp.getField(); boolean holder = field.isHolder() && !controller.isInClosureConstructor(); ClassNode type = field.getType(); String ownerName = (field.getOwner().equals(controller.getClassNode())) ? controller.getInternalClassName() : BytecodeHelper.getClassInternalName(field.getOwner()); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, ownerName, fldExp.getFieldName(), BytecodeHelper.getTypeDescription(type)); if (holder) { mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "get", "()Ljava/lang/Object;", false); controller.getOperandStack().push(ClassHelper.OBJECT_TYPE); } else { controller.getOperandStack().push(field.getType()); } }
private static void visitDeprecation(AnnotatedNode node, AnnotationNode visited) { if (visited.getClassNode().isResolved() && visited.getClassNode().getName().equals("java.lang.Deprecated")) { if (node instanceof MethodNode) { MethodNode mn = (MethodNode) node; mn.setModifiers(mn.getModifiers() | Opcodes.ACC_DEPRECATED); } else if (node instanceof FieldNode) { FieldNode fn = (FieldNode) node; fn.setModifiers(fn.getModifiers() | Opcodes.ACC_DEPRECATED); } else if (node instanceof ClassNode) { ClassNode cn = (ClassNode) node; cn.setModifiers(cn.getModifiers() | Opcodes.ACC_DEPRECATED); } } }