Java Code Examples for org.codehaus.groovy.ast.ClassNode#getDeclaredField()
The following examples show how to use
org.codehaus.groovy.ast.ClassNode#getDeclaredField() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassNodeUtils.java From groovy with Apache License 2.0 | 6 votes |
/** * Return the (potentially inherited) field of the classnode. * * @param classNode the classnode * @param fieldName the name of the field * @return the field or null if not found */ public static FieldNode getField(ClassNode classNode, String fieldName) { ClassNode node = classNode; Set<String> visited = new HashSet<>(); while (node != null) { FieldNode fn = node.getDeclaredField(fieldName); if (fn != null) return fn; ClassNode[] interfaces = node.getInterfaces(); for (ClassNode iNode : interfaces) { if (visited.contains(iNode.getName())) continue; FieldNode ifn = getField(iNode, fieldName); visited.add(iNode.getName()); if (ifn != null) return ifn; } node = node.getSuperClass(); } return null; }
Example 2
Source File: StaticVerifier.java From groovy with Apache License 2.0 | 6 votes |
private static FieldNode getDeclaredOrInheritedField(ClassNode cn, String fieldName) { ClassNode node = cn; while (node != null) { FieldNode fn = node.getDeclaredField(fieldName); if (fn != null) return fn; List<ClassNode> interfacesToCheck = new ArrayList<>(Arrays.asList(node.getInterfaces())); while (!interfacesToCheck.isEmpty()) { ClassNode nextInterface = interfacesToCheck.remove(0); fn = nextInterface.getDeclaredField(fieldName); if (fn != null) return fn; interfacesToCheck.addAll(Arrays.asList(nextInterface.getInterfaces())); } node = node.getSuperClass(); } return null; }
Example 3
Source File: AnnotationVisitor.java From groovy with Apache License 2.0 | 6 votes |
private boolean validateEnumConstant(Expression exp) { if (exp instanceof PropertyExpression) { PropertyExpression pe = (PropertyExpression) exp; String name = pe.getPropertyAsString(); if (pe.getObjectExpression() instanceof ClassExpression && name != null) { ClassExpression ce = (ClassExpression) pe.getObjectExpression(); ClassNode type = ce.getType(); if (type.isEnum()) { boolean ok = false; try { FieldNode enumField = type.getDeclaredField(name); ok = enumField != null && enumField.getType().equals(type); } catch(Exception ex) { // ignore } if(!ok) { addError("No enum const " + type.getName() + "." + name, pe); return false; } } } } return true; }
Example 4
Source File: Verifier.java From groovy with Apache License 2.0 | 6 votes |
private static FieldNode getMetaClassField(ClassNode node) { FieldNode ret = node.getDeclaredField("metaClass"); if (ret != null) { ClassNode mcFieldType = ret.getType(); if (!mcFieldType.equals(ClassHelper.METACLASS_TYPE)) { throw new RuntimeParserException("The class " + node.getName() + " cannot declare field 'metaClass' of type " + mcFieldType.getName() + " as it needs to be of " + "the type " + ClassHelper.METACLASS_TYPE.getName() + " for internal groovy purposes", ret); } return ret; } ClassNode current = node; while (current != ClassHelper.OBJECT_TYPE) { current = current.getSuperClass(); if (current == null) break; ret = current.getDeclaredField("metaClass"); if (ret == null) continue; if (isPrivate(ret.getModifiers())) continue; return ret; } return null; }
Example 5
Source File: UnionTypeClassNode.java From groovy with Apache License 2.0 | 5 votes |
@Override public FieldNode getDeclaredField(final String name) { for (ClassNode delegate : delegates) { FieldNode node = delegate.getDeclaredField(name); if (node != null) return node; } return null; }
Example 6
Source File: TraitReceiverTransformer.java From groovy with Apache License 2.0 | 5 votes |
private static FieldNode tryGetFieldNode(final ClassNode weavedType, final String fieldName) { FieldNode fn = weavedType.getDeclaredField(fieldName); if (fn == null && ClassHelper.CLASS_Type.equals(weavedType)) { GenericsType[] genericsTypes = weavedType.getGenericsTypes(); if (genericsTypes != null && genericsTypes.length == 1) { // for static properties fn = genericsTypes[0].getType().getDeclaredField(fieldName); } } return fn; }
Example 7
Source File: Verifier.java From groovy with Apache License 2.0 | 5 votes |
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; }
Example 8
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 5 votes |
public static FieldNode getDeclaredFieldOfCurrentClassOrAccessibleFieldOfSuper(final ClassNode accessingNode, final ClassNode current, final String name, final boolean skipCurrent) { if (!skipCurrent) { FieldNode currentClassField = current.getDeclaredField(name); if (isValidFieldNodeForByteCodeAccess(currentClassField, accessingNode)) return currentClassField; } for (ClassNode node = current.getSuperClass(); node != null; node = node.getSuperClass()) { FieldNode fn = node.getDeclaredField(name); if (isValidFieldNodeForByteCodeAccess(fn, accessingNode)) return fn; } return null; }
Example 9
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 5 votes |
private boolean checkStaticOuterField(final PropertyExpression pexp, final String propertyName) { for (final ClassNode outer : controller.getClassNode().getOuterClasses()) { FieldNode field = outer.getDeclaredField(propertyName); if (field != null) { if (!field.isStatic()) break; Expression outerClass = classX(outer); outerClass.setNodeMetaData(PROPERTY_OWNER, outer); outerClass.setSourcePosition(pexp.getObjectExpression()); Expression outerField = attrX(outerClass, pexp.getProperty()); outerField.setSourcePosition(pexp); outerField.visit(this); return true; } else { field = outer.getField(propertyName); // checks supers if (field != null && !field.isPrivate() && (field.isPublic() || field.isProtected() || Objects.equals(field.getDeclaringClass().getPackageName(), outer.getPackageName()))) { if (!field.isStatic()) break; Expression upperClass = classX(field.getDeclaringClass()); upperClass.setNodeMetaData(PROPERTY_OWNER, field.getDeclaringClass()); upperClass.setSourcePosition(pexp.getObjectExpression()); Expression upperField = propX(upperClass, pexp.getProperty()); upperField.setSourcePosition(pexp); upperField.visit(this); return true; } } } return false; }
Example 10
Source File: ClosureWriter.java From groovy with Apache License 2.0 | 5 votes |
public static void loadReference(final String name, final WriterController controller) { CompileStack compileStack = controller.getCompileStack(); MethodVisitor mv = controller.getMethodVisitor(); ClassNode classNode = controller.getClassNode(); AsmClassGenerator acg = controller.getAcg(); // compileStack.containsVariable(name) means to ask if the variable is already declared // compileStack.getScope().isReferencedClassVariable(name) means to ask if the variable is a field // If it is no field and is not yet declared, then it is either a closure shared variable or // an already declared variable. if (!compileStack.containsVariable(name) && compileStack.getScope().isReferencedClassVariable(name)) { acg.visitFieldExpression(new FieldExpression(classNode.getDeclaredField(name))); } else { BytecodeVariable v = compileStack.getVariable(name, !classNodeUsesReferences(controller.getClassNode())); if (v == null) { // variable is not on stack because we are // inside a nested Closure and this variable // was not used before // then load it from the Closure field FieldNode field = classNode.getDeclaredField(name); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, controller.getInternalClassName(), name, BytecodeHelper.getTypeDescription(field.getType())); } else { mv.visitVarInsn(ALOAD, v.getIndex()); } controller.getOperandStack().push(ClassHelper.REFERENCE_TYPE); } }
Example 11
Source File: InvocationWriter.java From groovy with Apache License 2.0 | 5 votes |
private boolean isClosureCall(final MethodCallExpression call) { // are we a local variable? // it should not be an explicitly "this" qualified method call // and the current class should have a possible method ClassNode classNode = controller.getClassNode(); String methodName = call.getMethodAsString(); if (methodName == null) return false; if (!call.isImplicitThis()) return false; if (!AsmClassGenerator.isThisExpression(call.getObjectExpression())) return false; FieldNode field = classNode.getDeclaredField(methodName); if (field == null) return false; if (isStaticInvocation(call) && !field.isStatic()) return false; Expression arguments = call.getArguments(); return !classNode.hasPossibleMethod(methodName, arguments); }
Example 12
Source File: Verifier.java From groovy with Apache License 2.0 | 4 votes |
private void addStaticMetaClassField(final ClassNode node, final String classInternalName) { String _staticClassInfoFieldName = "$staticClassInfo"; while (node.getDeclaredField(_staticClassInfoFieldName) != null) _staticClassInfoFieldName = _staticClassInfoFieldName + "$"; final String staticMetaClassFieldName = _staticClassInfoFieldName; FieldNode staticMetaClassField = node.addField(staticMetaClassFieldName, ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.make(ClassInfo.class, false), null); staticMetaClassField.setSynthetic(true); node.addSyntheticMethod( "$getStaticMetaClass", ACC_PROTECTED, ClassHelper.make(MetaClass.class), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() { @Override public void visit(MethodVisitor mv) { mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); if (BytecodeHelper.isClassLiteralPossible(node) || BytecodeHelper.isSameCompilationUnit(classNode, node)) { BytecodeHelper.visitClassLiteral(mv, node); } else { mv.visitMethodInsn(INVOKESTATIC, classInternalName, "$get$$class$" + classInternalName.replace('/', '$'), "()Ljava/lang/Class;", false); } Label l1 = new Label(); mv.visitJumpInsn(IF_ACMPEQ, l1); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter", "initMetaClass", "(Ljava/lang/Object;)Lgroovy/lang/MetaClass;", false); mv.visitInsn(ARETURN); mv.visitLabel(l1); mv.visitFieldInsn(GETSTATIC, classInternalName, staticMetaClassFieldName, "Lorg/codehaus/groovy/reflection/ClassInfo;"); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); Label l0 = new Label(); mv.visitJumpInsn(IFNONNULL, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/reflection/ClassInfo", "getClassInfo", "(Ljava/lang/Class;)Lorg/codehaus/groovy/reflection/ClassInfo;", false); mv.visitInsn(DUP); mv.visitVarInsn(ASTORE, 1); mv.visitFieldInsn(PUTSTATIC, classInternalName, staticMetaClassFieldName, "Lorg/codehaus/groovy/reflection/ClassInfo;"); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/ClassInfo", "getMetaClass", "()Lgroovy/lang/MetaClass;", false); mv.visitInsn(ARETURN); } }) ); }
Example 13
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitPropertyExpression(final PropertyExpression expression) { Expression objectExpression = expression.getObjectExpression(); OperandStack operandStack = controller.getOperandStack(); int mark = operandStack.getStackLength() - 1; boolean visited = false; if (isThisOrSuper(objectExpression)) { String name = expression.getPropertyAsString(); if (name != null) { FieldNode fieldNode = null; ClassNode classNode = controller.getClassNode(); if (isThisExpression(objectExpression)) { if (controller.isInGeneratedFunction()) { // params are stored as fields if (expression.isImplicitThis()) fieldNode = classNode.getDeclaredField(name); } else { fieldNode = classNode.getDeclaredField(name); if (fieldNode == null && !isValidFieldNodeForByteCodeAccess(classNode.getField(name), classNode)) { // GROOVY-9501, GROOVY-9569 if (checkStaticOuterField(expression, name)) return; } } } else { fieldNode = classNode.getSuperClass().getDeclaredField(name); // GROOVY-4497: do not visit super class field if it is private if (fieldNode != null && fieldNode.isPrivate()) fieldNode = null; if (fieldNode == null) { visited = tryPropertyOfSuperClass(expression, name); } } if (fieldNode != null) { fieldX(fieldNode).visit(this); visited = true; } } } if (!visited) { boolean useMetaObjectProtocol = isGroovyObject(objectExpression) && (!isThisOrSuper(objectExpression) || !controller.isStaticContext() || controller.isInGeneratedFunction()); MethodCallerMultiAdapter adapter; if (controller.getCompileStack().isLHS()) { adapter = useMetaObjectProtocol ? setGroovyObjectProperty : setProperty; } else { adapter = useMetaObjectProtocol ? getGroovyObjectProperty : getProperty; } visitAttributeOrProperty(expression, adapter); } if (controller.getCompileStack().isLHS()) { operandStack.remove(operandStack.getStackLength() - mark); } else { controller.getAssertionWriter().record(expression.getProperty()); } }