/** * @return true if this class is derived from a groovy object * i.e. it implements GroovyObject */ public boolean isDerivedFromGroovyObject() { return implementsInterface(ClassHelper.GROOVY_OBJECT_TYPE); }
@Override public boolean implementsInterface(final ClassNode classNode) { for (ClassNode delegate : delegates) { if (delegate.implementsInterface(classNode)) return true; } return false; }
private static boolean implementsExternalizable(ClassNode cNode) { return cNode.implementsInterface(EXTERNALIZABLE_TYPE); }
private static boolean isValidAnnotationClass(ClassNode node) { return node.implementsInterface(ClassHelper.Annotation_TYPE); }
private static boolean implementsSerializable(ClassNode cNode) { return cNode.implementsInterface(SERIALIZABLE_TYPE); }
@Override public void visitClass(ClassNode node) { this.optimizeMethodCall = !node.implementsInterface(GROOVY_INTERCEPTABLE_TYPE); this.node = node; this.scope = nonStaticScope; super.visitClass(node); this.scope=null; this.node=null; }
private static void addMostSpecificInterface(ClassNode interfaceNode, List<ClassNode> nodes) { if (nodes.isEmpty()) nodes.add(interfaceNode); for (int i = 0, nodesSize = nodes.size(); i < nodesSize; i++) { final ClassNode node = nodes.get(i); if (node.equals(interfaceNode)||node.implementsInterface(interfaceNode)) { // a more specific interface exists in the list, keep it return; } if (interfaceNode.implementsInterface(node)) { // the interface beeing added is more specific than the one in the list, replace it nodes.set(i, interfaceNode); return; } } // if we reach this point, this means the interface is new nodes.add(interfaceNode); }
public static boolean isOrImplements(ClassNode type, ClassNode interfaceType) { return type.equals(interfaceType) || type.implementsInterface(interfaceType); }
private static void extractMostSpecificImplementedInterfaces(final ClassNode type, final ClassNode inode, final List<ClassNode> result) { if (type.implementsInterface(inode)) result.add(inode); else { ClassNode[] interfaces = inode.getInterfaces(); for (ClassNode interfaceNode : interfaces) { if (type.implementsInterface(interfaceNode)) result.add(interfaceNode); } if (result.isEmpty() && interfaces.length>0) { // none if the direct interfaces match, but we must check "upper" in the hierarchy for (ClassNode interfaceNode : interfaces) { extractMostSpecificImplementedInterfaces(type, interfaceNode, result); } } } }
private static void removeMethodInSuperInterface(List<MethodNode> toBeRemoved, MethodNode one, MethodNode two) { ClassNode oneDC = one.getDeclaringClass(); ClassNode twoDC = two.getDeclaringClass(); if (oneDC.implementsInterface(twoDC)) { toBeRemoved.add(two); } else { toBeRemoved.add(one); } }
private static void implementComparable(ClassNode classNode) { if (!classNode.implementsInterface(COMPARABLE_TYPE)) { classNode.addInterface(makeClassSafeWithGenerics(Comparable.class, classNode)); } }
private void updateStaticCompileFlag(final MethodNode mn) { ClassNode classNode = getClassNode(); AnnotatedNode node = mn; if (classNode.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) { node = classNode.getOuterClass(); } isInStaticallyCheckedMethod = mn != null && ( StaticCompilationVisitor.isStaticallyCompiled(node) || classNode.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)&&classNode.getNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE)!=null); }
/** * Determines if the source class implements an interface or subclasses the target type. * This method takes the {@link org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode lowest * upper bound class node} type into account, allowing to remove unnecessary casts. * @param source the type of interest * @param targetType the target type of interest */ public static boolean implementsInterfaceOrSubclassOf(final ClassNode source, final ClassNode targetType) { if (source.isDerivedFrom(targetType) || source.implementsInterface(targetType)) return true; if (targetType instanceof WideningCategories.LowestUpperBoundClassNode) { WideningCategories.LowestUpperBoundClassNode lub = (WideningCategories.LowestUpperBoundClassNode) targetType; if (implementsInterfaceOrSubclassOf(source, lub.getSuperClass())) return true; for (ClassNode classNode : lub.getInterfaces()) { if (source.implementsInterface(classNode)) return true; } } return false; } }
private static boolean isCovariant(ClassNode left, ClassNode right) { if (left.isArray() && right.isArray()) { return isCovariant(left.getComponentType(), right.getComponentType()); } return left.isDerivedFrom(right) || left.implementsInterface(right); }
private void checkComparable(PropertyNode pNode) { if (pNode.getType().implementsInterface(COMPARABLE_TYPE) || isPrimitiveType(pNode.getType()) || hasAnnotation(pNode.getType(), MY_TYPE)) { return; } addError("Error during " + MY_TYPE_NAME + " processing: property '" + pNode.getName() + "' must be Comparable", pNode); }
private boolean isAssignable(ClassNode node, ClassNode testNode) { if (node.isArray() && testNode.isArray()) { return isArrayAssignable(node.getComponentType(), testNode.getComponentType()); } if (testNode.isInterface()) { if (node.equals(testNode) || node.implementsInterface(testNode)) return true; } return node.isDerivedFrom(testNode); }
private void checkClassExtendsAllSelfTypes(ClassNode node) { int modifiers = node.getModifiers(); if (!isInterface(modifiers)) { for (ClassNode anInterface : GeneralUtils.getInterfacesAndSuperInterfaces(node)) { if (Traits.isTrait(anInterface)) { LinkedHashSet<ClassNode> selfTypes = new LinkedHashSet<ClassNode>(); for (ClassNode type : Traits.collectSelfTypes(anInterface, selfTypes, true, false)) { if (type.isInterface() && !node.implementsInterface(type)) { addError(getDescription(node) + " implements " + getDescription(anInterface) + " but does not implement self type " + getDescription(type), anInterface); } else if (!type.isInterface() && !node.isDerivedFrom(type)) { addError(getDescription(node) + " implements " + getDescription(anInterface) + " but does not extend self type " + getDescription(type), anInterface); } } } } } }
private boolean compatibleArgumentType(ClassNode argumentType, ClassNode paramType) { if (argumentType == null) return false; if (ClassHelper.getWrapper(argumentType).equals(ClassHelper.getWrapper(paramType))) return true; if (paramType.isInterface()) return argumentType.implementsInterface(paramType); if (paramType.isArray() && argumentType.isArray()) return compatibleArgumentType(argumentType.getComponentType(), paramType.getComponentType()); return ClassHelper.getWrapper(argumentType).isDerivedFrom(ClassHelper.getWrapper(paramType)); }
protected void addPropertyMethod(MethodNode method) { classNode.addMethod(method); markAsGenerated(classNode, method); // GROOVY-4415 / GROOVY-4645: check that there's no abstract method which corresponds to this one List<MethodNode> abstractMethods = classNode.getAbstractMethods(); if (abstractMethods == null) return; String methodName = method.getName(); Parameter[] parameters = method.getParameters(); ClassNode methodReturnType = method.getReturnType(); for (MethodNode node : abstractMethods) { if (!node.getDeclaringClass().equals(classNode)) continue; if (node.getName().equals(methodName) && node.getParameters().length == parameters.length) { if (parameters.length == 1) { // setter ClassNode abstractMethodParameterType = node.getParameters()[0].getType(); ClassNode methodParameterType = parameters[0].getType(); if (!methodParameterType.isDerivedFrom(abstractMethodParameterType) && !methodParameterType.implementsInterface(abstractMethodParameterType)) { continue; } } ClassNode nodeReturnType = node.getReturnType(); if (!methodReturnType.isDerivedFrom(nodeReturnType) && !methodReturnType.implementsInterface(nodeReturnType)) { continue; } // matching method, remove abstract status and use the same body node.setModifiers(node.getModifiers() ^ ACC_ABSTRACT); node.setCode(method.getCode()); } } }
public boolean implementsInterfaceOrIsSubclassOf(ClassNode type, ClassNode superOrInterface) { boolean result = type.equals(superOrInterface) || type.isDerivedFrom(superOrInterface) || type.implementsInterface(superOrInterface); if (result) { return true; } if (GROOVY_OBJECT_TYPE.equals(superOrInterface) && type.getCompileUnit()!=null) { // type is being compiled so it will implement GroovyObject later return true; } if (superOrInterface instanceof WideningCategories.LowestUpperBoundClassNode) { WideningCategories.LowestUpperBoundClassNode cn = (WideningCategories.LowestUpperBoundClassNode) superOrInterface; result = implementsInterfaceOrIsSubclassOf(type, cn.getSuperClass()); if (result) { for (ClassNode interfaceNode : cn.getInterfaces()) { result = implementsInterfaceOrIsSubclassOf(type,interfaceNode); if (!result) break; } } if (result) return true; } if (type.isArray() && superOrInterface.isArray()) { return implementsInterfaceOrIsSubclassOf(type.getComponentType(), superOrInterface.getComponentType()); } return false; }