@Override public void visitNewExpression(PsiNewExpression expression) { super.visitNewExpression(expression); final PsiJavaCodeReferenceElement classReference = expression.getClassReference(); if (classReference == null) { return; } final String name = classReference.getReferenceName(); if (!"BigDecimal".equals(name)) { return; } final PsiMethod constructor = expression.resolveConstructor(); if (constructor == null) { return; } final PsiParameterList parameterList = constructor.getParameterList(); final int length = parameterList.getParametersCount(); if (length != 1 && length != 2) { return; } final PsiParameter[] parameters = parameterList.getParameters(); final PsiParameter firstParameter = parameters[0]; final PsiType type = firstParameter.getType(); if (type != PsiType.DOUBLE) { return; } registerNewExpressionError(expression); }
@Override public void visitMethod(@NotNull PsiMethod method) { // note: no call to super if (method.getNameIdentifier() == null) { return; } if (!method.isConstructor()) { return; } if (ignoreScope != Scope.NONE) { switch (ignoreScope.ordinal()) { case 3: if (method.hasModifierProperty(PsiModifier.PROTECTED)) return; case 2: if (method.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) return; case 1: if (method.hasModifierProperty(PsiModifier.PRIVATE)) return; } } final PsiParameterList parameterList = method.getParameterList(); final int parametersCount = parameterList.getParametersCount(); if (parametersCount <= getLimit()) { return; } registerMethodError(method, Integer.valueOf(parametersCount)); }
static boolean hasNullArgConstructor(PsiClass aClass) { final PsiMethod[] constructors = aClass.getConstructors(); for (final PsiMethod constructor : constructors) { final PsiParameterList params = constructor.getParameterList(); if (params.getParametersCount() == 0) { return true; } } return false; }
@Override public void visitMethod(@NotNull PsiMethod method) { // note: no call to super; final String methodName = method.getName(); if (!HardcodedMethodConstants.FINALIZE.equals(methodName)) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { return; } if (ignoreTrivialFinalizers && isTrivial(method)) { return; } registerMethodError(method); }
private static boolean classHasNoArgConstructor(PsiClass ancestor) { boolean hasConstructor = false; boolean hasNoArgConstructor = false; final PsiMethod[] methods = ancestor.getMethods(); for (final PsiMethod method : methods) { if (method.isConstructor()) { hasConstructor = true; final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() == 0 && (method.hasModifierProperty(PsiModifier.PUBLIC) || method.hasModifierProperty(PsiModifier.PROTECTED))) { hasNoArgConstructor = true; } } } return hasNoArgConstructor || !hasConstructor; }
private boolean hasGoodToString(PsiClass aClass) { final PsiMethod[] methods = aClass.findMethodsByName(HardcodedMethodConstants.TO_STRING, true); for (PsiMethod method : methods) { final PsiClass containingClass = method.getContainingClass(); if (containingClass == null) { continue; } final String name = containingClass.getQualifiedName(); if (CommonClassNames.JAVA_LANG_OBJECT.equals(name)) { continue; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() == 0) { return true; } } return false; }
@Override public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement classNameIdentifier = descriptor.getPsiElement(); final PsiClass aClass = (PsiClass) classNameIdentifier.getParent(); if (aClass == null) { return; } final PsiMethod[] constructurs = aClass.getConstructors(); for (final PsiMethod constructor : constructurs) { final PsiParameterList parameterList = constructor.getParameterList(); if (parameterList.getParametersCount() == 0) { final PsiModifierList modifiers = constructor.getModifierList(); modifiers.setModifierProperty(PsiModifier.PUBLIC, false); modifiers.setModifierProperty(PsiModifier.PROTECTED, false); modifiers.setModifierProperty(PsiModifier.PRIVATE, true); } } }
private boolean hasOnlyMain(PsiClass aClass) { final PsiMethod[] methods = aClass.getMethods(); if (methods.length == 0) { return false; } for (PsiMethod method : methods) { if (method.isConstructor()) { continue; } if (!method.hasModifierProperty(PsiModifier.STATIC)) { return false; } if (method.hasModifierProperty(PsiModifier.PRIVATE)) { continue; } if (!method.hasModifierProperty(PsiModifier.PUBLIC)) { return false; } final String name = method.getName(); if (!name.equals(HardcodedMethodConstants.MAIN)) { return false; } final PsiType returnType = method.getReturnType(); if (!PsiType.VOID.equals(returnType)) { return false; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 1) { return false; } final PsiParameter[] parameters = parameterList.getParameters(); final PsiParameter parameter = parameters[0]; final PsiType type = parameter.getType(); if (!type.equalsToText("java.lang.String[]")) { return false; } } return true; }