private void reportNullableReturns( DataFlowInstructionVisitor visitor, ProblemsHolder holder, Set<PsiElement> reportedAnchors, @NotNull PsiElement block) { final PsiMethod method = getScopeMethod(block); if (method == null || NullableStuffInspectionBase.isNullableNotInferred(method, true)) return; boolean notNullRequired = NullableNotNullManager.isNotNull(method); if (!notNullRequired && !SUGGEST_NULLABLE_ANNOTATIONS) return; PsiType returnType = method.getReturnType(); // no warnings in void lambdas, where the expression is not returned anyway if (block instanceof PsiExpression && block.getParent() instanceof PsiLambdaExpression && returnType == PsiType.VOID) return; // no warnings for Void methods, where only null can be possibly returned if (returnType == null || returnType.equalsToText(CommonClassNames.JAVA_LANG_VOID)) return; for (PsiElement statement : visitor.getProblems(NullabilityProblem.nullableReturn)) { assert statement instanceof PsiExpression; final PsiExpression expr = (PsiExpression) statement; if (!reportedAnchors.add(expr)) continue; if (notNullRequired) { final String text = isNullLiteralExpression(expr) ? InspectionsBundle.message("dataflow.message.return.null.from.notnull") : InspectionsBundle.message("dataflow.message.return.nullable.from.notnull"); holder.registerProblem(expr, text); } else if (AnnotationUtil.isAnnotatingApplicable(statement)) { final NullableNotNullManager manager = NullableNotNullManager.getInstance(expr.getProject()); final String defaultNullable = manager.getDefaultNullable(); final String presentableNullable = StringUtil.getShortName(defaultNullable); final String text = isNullLiteralExpression(expr) ? InspectionsBundle.message( "dataflow.message.return.null.from.notnullable", presentableNullable) : InspectionsBundle.message( "dataflow.message.return.nullable.from.notnullable", presentableNullable); final LocalQuickFix[] fixes = PsiTreeUtil.getParentOfType(expr, PsiMethod.class, PsiLambdaExpression.class) instanceof PsiLambdaExpression ? LocalQuickFix.EMPTY_ARRAY : new LocalQuickFix[] { new AnnotateMethodFix( defaultNullable, ArrayUtil.toStringArray(manager.getNotNulls())) { @Override public int shouldAnnotateBaseMethod( PsiMethod method, PsiMethod superMethod, Project project) { return 1; } } }; holder.registerProblem(expr, text, fixes); } } }
public static boolean isMainOrPremainMethod(@NotNull PsiMethod method) { if (!PsiType.VOID.equals(method.getReturnType())) return false; String name = method.getName(); if (!("main".equals(name) || "premain".equals(name))) return false; PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory(); MethodSignature signature = method.getSignature(PsiSubstitutor.EMPTY); try { MethodSignature main = createSignatureFromText(factory, "void main(String[] args);"); if (MethodSignatureUtil.areSignaturesEqual(signature, main)) return true; MethodSignature premain = createSignatureFromText( factory, "void premain(String args, java.lang.instrument.Instrumentation i);"); if (MethodSignatureUtil.areSignaturesEqual(signature, premain)) return true; } catch (IncorrectOperationException e) { LOG.error(e); } return false; }
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; }
@Override public void visitLiteralExpression(@NotNull PsiLiteralExpression value) { super.visitLiteralExpression(value); final String text = value.getText(); if (!PsiKeyword.NULL.equals(text)) { return; } PsiElement parent = value.getParent(); while (parent instanceof PsiParenthesizedExpression || parent instanceof PsiConditionalExpression || parent instanceof PsiTypeCastExpression) { parent = parent.getParent(); } if (parent == null || !(parent instanceof PsiReturnStatement)) { return; } final PsiMethod method = PsiTreeUtil.getParentOfType(value, PsiMethod.class); if (method == null) { return; } final PsiType returnType = method.getReturnType(); if (returnType == null) { return; } final boolean isArray = returnType.getArrayDimensions() > 0; if (AnnotationUtil.isAnnotated(method, AnnotationUtil.NULLABLE, false)) { return; } if (m_reportCollectionMethods && CollectionUtils.isCollectionClassOrInterface(returnType)) { registerError(value, value); } else if (m_reportArrayMethods && isArray) { registerError(value, value); } else if (m_reportObjectMethods && !isArray) { registerError(value, value); } }
private static LookupElementBuilder createGenerateMethodElement( PsiMethod prototype, PsiSubstitutor substitutor, Icon icon, String typeText, InsertHandler<LookupElement> insertHandler) { String methodName = prototype.getName(); String visibility = VisibilityUtil.getVisibilityModifier(prototype.getModifierList()); String modifiers = (visibility == PsiModifier.PACKAGE_LOCAL ? "" : visibility + " "); PsiType type = substitutor.substitute(prototype.getReturnType()); String signature = modifiers + (type == null ? "" : type.getPresentableText() + " ") + methodName; String parameters = PsiFormatUtil.formatMethod( prototype, substitutor, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_NAME); String overrideSignature = " @Override " + signature; // leading space to make it a middle match, under all annotation // suggestions LookupElementBuilder element = LookupElementBuilder.create(prototype, signature) .withLookupString(methodName) .withLookupString(signature) .withLookupString(overrideSignature) .withInsertHandler(insertHandler) .appendTailText(parameters, false) .appendTailText(" {...}", true) .withTypeText(typeText) .withIcon(icon); element.putUserData(GENERATE_ELEMENT, true); return element; }
public static void prepareWizardData(final WizardData data, PsiClass boundClass) throws MyException { final PsiMethod[] allGetDataMethods = boundClass.findMethodsByName("getData", false); final PsiMethod[] allSetDataMethods = boundClass.findMethodsByName("setData", false); PsiMethod setDataMethod = null; PsiClass beanClass = null; // find get/set pair and bean class outer: for (int i = 0; i < allGetDataMethods.length; i++) { final PsiMethod _getMethod = allGetDataMethods[i]; if (_getMethod.getReturnType() != PsiType.VOID) { continue; } final PsiParameter[] _getMethodParameters = _getMethod.getParameterList().getParameters(); if (_getMethodParameters.length != 1) { continue; } final PsiClass _getParameterClass = getClassByType(_getMethodParameters[0].getType()); if (_getParameterClass == null) { continue; } for (final PsiMethod _setMethod : allSetDataMethods) { if (_setMethod.getReturnType() != PsiType.VOID) { continue; } final PsiParameter[] _setMethodParameters = _setMethod.getParameterList().getParameters(); if (_setMethodParameters.length != 1) { continue; } final PsiClass _setParameterClass = getClassByType(_setMethodParameters[0].getType()); if (_setParameterClass != _getParameterClass) { continue; } // pair found !!! setDataMethod = _setMethod; beanClass = _getParameterClass; break outer; } } if (beanClass == null) { // nothing found return; } data.myBindToNewBean = false; data.myBeanClass = beanClass; // parse setData() and try to associate fields with bean { final PsiCodeBlock body = setDataMethod.getBody(); if (body == null) { return; } final PsiElement[] children = body.getChildren(); for (PsiElement child : children) { // Parses sequences like: a.foo(b.bar()); final PsiField bindingField; if (!(child instanceof PsiExpressionStatement)) { continue; } final PsiExpression expression = ((PsiExpressionStatement) child).getExpression(); if (!(expression instanceof PsiMethodCallExpression)) { continue; } final PsiMethodCallExpression callExpression = (PsiMethodCallExpression) expression; // find binding field ('a') int index = -1; { final PsiElement psiElement = getObjectForWhichMethodWasCalled(callExpression); if (!(psiElement instanceof PsiField)) { continue; } if (((PsiField) psiElement).getContainingClass() != boundClass) { continue; } bindingField = (PsiField) psiElement; // find binding for this field final FormProperty2BeanProperty[] bindings = data.myBindings; for (int j = 0; j < bindings.length; j++) { final FormProperty2BeanProperty binding = bindings[j]; if (bindingField .getName() .equals(binding.myFormProperty.getLwComponent().getBinding())) { index = j; break; } } } if (index == -1) { continue; } // find 'bar()' { final PsiReferenceParameterList parameterList = callExpression.getMethodExpression().getParameterList(); if (parameterList == null) { continue; } final PsiExpressionList argumentList = callExpression.getArgumentList(); if (argumentList == null) { continue; } final PsiExpression[] expressions = argumentList.getExpressions(); if (expressions == null || expressions.length != 1) { continue; } if (!(expressions[0] instanceof PsiMethodCallExpression)) { continue; } final PsiMethodCallExpression callExpression2 = ((PsiMethodCallExpression) expressions[0]); // check that 'b' is parameter final PsiElement psiElement = getObjectForWhichMethodWasCalled(callExpression2); if (!(psiElement instanceof PsiParameter)) { continue; } final PsiMethod barMethod = ((PsiMethod) callExpression2.getMethodExpression().resolve()); if (barMethod == null) { continue; } if (!PropertyUtil.isSimplePropertyGetter(barMethod)) { continue; } final String propertyName = PropertyUtil.getPropertyName(barMethod); // There are two possible types: boolean and java.lang.String String typeName = barMethod.getReturnType().getCanonicalText(); if (!"boolean".equals(typeName) && !"java.lang.String".equals(typeName)) { continue; } data.myBindings[index].myBeanProperty = new BeanProperty(propertyName, typeName); } } } }