public boolean isStaticMethod() { return methodNode != null && methodNode.isStatic(); }
private boolean checkStatic(MethodNode mNode, String annotationName) { if (!mNode.isStatic() && !mNode.isStaticConstructor() && !(mNode instanceof ConstructorNode)) { addError("Error processing method '" + mNode.getName() + "'. " + annotationName + " not allowed for instance methods.", mNode); return false; } return true; }
private void storeMissingCovariantMethods(List declaredMethods, Map methodsToAdd, Map genericsSpec, List<MethodNode> methodNodeList) { for (Object declaredMethod : declaredMethods) { MethodNode method = (MethodNode) declaredMethod; if (method.isStatic()) continue; storeMissingCovariantMethods(methodNodeList, method, methodsToAdd, genericsSpec, false); } }
public void setVariableScope(VariableScope variableScope) { this.variableScope = variableScope; variableScope.setInStaticContext(isStatic()); }
private boolean isFixtureMethod(MethodNode method) { String name = method.getName(); for (String fmName : FIXTURE_METHODS) { if (!fmName.equalsIgnoreCase(name)) continue; // assertion: is (meant to be) a fixture method, so we'll return true in the end if (method.isStatic()) errorReporter.error(method, "Fixture methods must not be static"); if (!fmName.equals(name)) errorReporter.error(method, "Misspelled '%s()' method (wrong capitalization)", fmName); return true; } return false; }
private boolean isFeatureMethod(MethodNode method) { for (Statement stat : AstUtil.getStatements(method)) { String label = stat.getStatementLabel(); if (label == null) continue; // assertion: is (meant to be) a feature method, so we'll return true in the end if (method.isStatic()) errorReporter.error(method, "Feature methods must not be static"); return true; } return false; }
static Map<GenericsTypeName, GenericsType> extractGenericsParameterMapOfThis(MethodNode mn) { if (mn == null) return null; Map<GenericsTypeName, GenericsType> map; if (mn.isStatic()) { map = new HashMap<>(); } else { map = getGenericsParameterMapOfThis(mn.getDeclaringClass()); } return mergeGenerics(map, mn.getGenericsTypes()); }
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 void handleMainMethodIfPresent(List methods) { boolean found = false; for (Iterator iter = methods.iterator(); iter.hasNext();) { MethodNode node = (MethodNode) iter.next(); if(node.getName().equals("main")) { if (node.isStatic() && node.getParameters().length == 1) { boolean retTypeMatches, argTypeMatches; ClassNode argType = node.getParameters()[0].getType(); ClassNode retType = node.getReturnType(); argTypeMatches = (argType.equals(ClassHelper.OBJECT_TYPE) || argType.getName().contains("String[]")); retTypeMatches = (retType == ClassHelper.VOID_TYPE || retType == ClassHelper.OBJECT_TYPE); if(retTypeMatches && argTypeMatches) { if(found) { throw new RuntimeException("Repetitive main method found."); } else { found = true; } // if script has both loose statements as well as main(), then main() is ignored if(statementBlock.isEmpty()) { addStatement(node.getCode()); } iter.remove(); } } } } }
public boolean isStaticContext() { if (compileStack!=null && compileStack.getScope()!=null) { return compileStack.getScope().isInStaticContext(); } if (!isInClosure()) return false; if (constructorNode != null) return false; return classNode.isStaticClass() || methodNode.isStatic(); }
private boolean handleMockCall(MethodCallExpression expr) { if (!currSpecialMethodCall.isTestDouble(expr)) return false; if (resources.getCurrentMethod().getAst().isStatic()) { resources.getErrorReporter().error(expr, "Mocks cannot be created in static scope"); // expand nevertheless so that inner scope (if any) won't trip over this again } currSpecialMethodCall.expand(); return true; }
public boolean isInteraction(ExpressionStatement stat) throws InvalidSpecCompileException { if (scanResult != null) { if (stat != this.stat) { throw new InvalidSpecCompileException(stat, "InteractionRewriter was reused"); } return scanResult; } this.stat = stat; Expression expr = parseCount(parseResults(stat.getExpression())); boolean interaction = (count != null || !responses.isEmpty()) && parseCall(expr); if (interaction && resources.getCurrentMethod().getAst().isStatic()) { throw new InvalidSpecCompileException(stat, "Interactions cannot be declared in static scope"); } return scanResult = interaction; }
private static void adjustTypesIfStaticMainMethod(MethodNode node) { if (node.getName().equals("main") && node.isStatic()) { Parameter[] params = node.getParameters(); if (params.length == 1) { Parameter param = params[0]; if (param.getType() == null || param.getType() == ClassHelper.OBJECT_TYPE) { param.setType(ClassHelper.STRING_TYPE.makeArray()); ClassNode returnType = node.getReturnType(); if (returnType == ClassHelper.OBJECT_TYPE) { node.setReturnType(ClassHelper.VOID_TYPE); } } } } }
public void setParameters(Parameter[] parameters) { invalidateCachedData(); VariableScope scope = new VariableScope(); this.parameters = parameters; if (parameters != null && parameters.length > 0) { for (Parameter para : parameters) { if (para.hasInitialExpression()) { this.hasDefaultValue = true; } para.setInStaticContext(isStatic()); scope.putDeclaredVariable(para); } } setVariableScope(scope); }
private void checkAndAddCannotCallPrivateMethodError(MethodNode target, Expression receiver, ClassNode classNode, ClassNode declaringClass) { if (declaringClass != classNode) { controller.getSourceUnit().addError(new SyntaxException("Cannot call private method " + (target.isStatic() ? "static " : "") + declaringClass.toString(false) + "#" + target.getName() + " from class " + classNode.toString(false), receiver.getLineNumber(), receiver.getColumnNumber(), receiver.getLastLineNumber(), receiver.getLastColumnNumber())); } }
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); }
protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) { pushState(node.isStatic()); inConstructor = isConstructor; node.setVariableScope(currentScope); visitAnnotations(node); // GROOVY-2156 Parameter[] parameters = node.getParameters(); for (Parameter parameter : parameters) { visitAnnotations(parameter); } declare(node.getParameters(), node); visitClassCodeContainer(node.getCode()); popState(); }
private void checkMethodsForIncorrectModifiers(ClassNode cn) { if (!cn.isInterface()) return; for (MethodNode method : cn.getMethods()) { if (method.isFinal()) { addError("The " + getDescription(method) + " from " + getDescription(cn) + " must not be final. It is by definition abstract.", method); } if (method.isStatic() && !isConstructor(method)) { addError("The " + getDescription(method) + " from " + getDescription(cn) + " must not be static. Only fields may be static in an interface.", method); } } }
private static MethodNode buildDelegatingMethod(final MethodNode annotatedMethod, final ClassNode ownerClassNode) { Statement code = annotatedMethod.getCode(); int access = ACC_PROTECTED; if (annotatedMethod.isStatic()) { access = ACC_PRIVATE | ACC_STATIC; } MethodNode method = new MethodNode( buildUniqueName(ownerClassNode, METHOD_LABEL, annotatedMethod), access, annotatedMethod.getReturnType(), cloneParams(annotatedMethod.getParameters()), annotatedMethod.getExceptions(), code ); method.addAnnotations(filterAnnotations(annotatedMethod.getAnnotations())); return method; }
private void checkNoAbstractMethodsNonabstractClass(ClassNode node) { if (isAbstract(node.getModifiers())) return; List<MethodNode> abstractMethods = node.getAbstractMethods(); if (abstractMethods == null) return; for (MethodNode method : abstractMethods) { MethodNode sameArgsMethod = node.getMethod(method.getName(), method.getParameters()); if (sameArgsMethod==null || method.getReturnType().equals(sameArgsMethod.getReturnType())) { addError("Can't have an abstract method in a non-abstract class." + " The " + getDescription(node) + " must be declared abstract or" + " the " + getDescription(method) + " must be implemented.", node); } else { addError("Abstract "+getDescription(method)+" is not implemented but a " + "method of the same name but different return type is defined: "+ (sameArgsMethod.isStatic()?"static ":"")+ getDescription(sameArgsMethod), method ); } } }