@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if (psiElement instanceof PsiParameterList) { String text = psiElement.getText(); if (StringUtil.startsWithChar(text, '(') && !StringUtil.endsWithChar(text, ')')) { PsiParameter[] params = ((PsiParameterList)psiElement).getParameters(); int offset; if (params.length == 0) { offset = psiElement.getTextRange().getStartOffset() + 1; } else { offset = params[params.length - 1].getTextRange().getEndOffset(); } editor.getDocument().insertString(offset, ")"); } } }
@Override public void visitMethod(@NotNull PsiMethod method) { super.visitMethod(method); final String name = method.getName(); if (!HardcodedMethodConstants.CLONE.equals(name)) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { return; } if (method.hasModifierProperty(PsiModifier.FINAL) || method.hasModifierProperty(PsiModifier.ABSTRACT)) { return; } final PsiClass containingClass = method.getContainingClass(); if (containingClass == null) { return; } if (containingClass.hasModifierProperty(PsiModifier.FINAL) || containingClass.isInterface()) { return; } registerMethodError(method); }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super @NonNls final String methodName = method.getName(); if (!"suite".equals(methodName)) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return; } if (!InheritanceUtil.isInheritor(aClass, "junit.framework.TestCase")) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { return; } if (method.hasModifierProperty(PsiModifier.STATIC)) { return; } registerMethodError(method); }
@Override public void visitMethod(PsiMethod method) { super.visitMethod(method); if (!method.isConstructor()) { return; } if (!method.hasModifierProperty(PsiModifier.PUBLIC)) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null || aClass.hasModifierProperty(PsiModifier.ABSTRACT)) { return; } if (SerializationUtils.isExternalizable(aClass)) { final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() == 0) { return; } } registerMethodError(method, Boolean.FALSE); }
@Override public void visitMethod(@NotNull PsiMethod method) { final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() == 0) { return; } final Query<MethodSignatureBackedByPsiMethod> query = SuperMethodsSearch.search( method, method.getContainingClass(), true, false); final MethodSignatureBackedByPsiMethod methodSignature = query.findFirst(); if (methodSignature == null) { return; } final PsiMethod superMethod = methodSignature.getMethod(); final PsiParameter[] parameters = parameterList.getParameters(); checkParameters(superMethod, parameters); }
@Override public void visitMethodCallExpression( @NotNull PsiMethodCallExpression expression) { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); if (!HardcodedMethodConstants.NOTIFY.equals(methodName) && !HardcodedMethodConstants.NOTIFY_ALL.equals(methodName)) { return; } final PsiMethod method = expression.resolveMethod(); if (method == null) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { return; } if (SynchronizationUtil.isInSynchronizedContext(expression)) { return; } registerMethodCallError(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)); }
@Override public void visitMethod(@NotNull PsiMethod method) { // note: no call to super if (method.getNameIdentifier() == null) { return; } if (method.isConstructor()) { return; } final PsiParameterList parameterList = method.getParameterList(); final int parametersCount = parameterList.getParametersCount(); if (parametersCount <= getLimit()) { return; } if (LibraryUtil.isOverrideOfLibraryMethod(method)) { return; } registerMethodError(method, Integer.valueOf(parametersCount)); }
private void initializePsiMethod(String methodName, String httpMethodValue, String pathValue) { PsiAnnotationMemberValue mockAnnotationMemberValue1 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue1.getText()).thenReturn(httpMethodValue); PsiAnnotationMemberValue mockAnnotationMemberValue2 = mock(PsiAnnotationMemberValue.class); when(mockAnnotationMemberValue2.getText()).thenReturn(pathValue); PsiAnnotation mockAnnotation = mock(PsiAnnotation.class); when(mockAnnotation.getQualifiedName()) .thenReturn(GctConstants.APP_ENGINE_ANNOTATION_API_METHOD); when(mockAnnotation.findAttributeValue("httpMethod")).thenReturn(mockAnnotationMemberValue1); when(mockAnnotation.findAttributeValue("path")).thenReturn(mockAnnotationMemberValue2); PsiAnnotation[] mockAnnotationsArray = {mockAnnotation}; PsiModifierList mockModifierList = mock(PsiModifierList.class); when(mockModifierList.getAnnotations()).thenReturn(mockAnnotationsArray); mockPsiMethod = mock(PsiMethod.class); when(mockPsiMethod.getModifierList()).thenReturn(mockModifierList); when(mockPsiMethod.getName()).thenReturn(methodName); when(mockPsiMethod.getContainingClass()).thenReturn(mockPsiClass); PsiParameterList mockParameterList = mock(PsiParameterList.class); when(mockParameterList.getParameters()).thenReturn(new PsiParameter[0]); when(mockPsiMethod.getParameterList()).thenReturn(mockParameterList); }
/** * Finds and removes a given method * @param methodName * @param arguments */ private void findAndRemoveMethod(String methodName, String... arguments) { // Maybe there's an easier way to do this with mClass.findMethodBySignature(), but I'm not an expert on Psi* PsiMethod[] methods = psiClass.findMethodsByName(methodName, false); for (PsiMethod method : methods) { PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() == arguments.length) { boolean shouldDelete = true; PsiParameter[] parameters = parameterList.getParameters(); for (int i = 0; i < arguments.length; i++) { if (!parameters[i].getType().getCanonicalText().equals(arguments[i])) { shouldDelete = false; } } if (shouldDelete) { method.delete(); } } } }
private Collection<PsiMethod> filterMethods(PsiMethod[] beforeMethods, PsiMethod compareMethod) { Collection<PsiMethod> result = new ArrayList<PsiMethod>(); for (PsiMethod psiMethod : beforeMethods) { final PsiParameterList compareMethodParameterList = compareMethod.getParameterList(); final PsiParameterList psiMethodParameterList = psiMethod.getParameterList(); if (compareMethod.getName().equals(psiMethod.getName()) && compareMethodParameterList.getParametersCount() == psiMethodParameterList.getParametersCount()) { final Collection<String> typesOfCompareMethod = mapToTypeString(compareMethodParameterList); final Collection<String> typesOfPsiMethod = mapToTypeString(psiMethodParameterList); if (typesOfCompareMethod.equals(typesOfPsiMethod)) { result.add(psiMethod); } } } return result; }
@UnsignedOrNotFound public static int findListIndex(@Unsigned int offset, @InstanceOf({PsiParameterList.class, PsiExpressionList.class}) @Nullable PsiElement listElement) { if ( listElement == null ) { return -1; } int index = -1; for( PsiElement element : listElement.getChildren() ) { if ( element.getTextRange().contains(offset) ) { return index; } if ( PsiUtil.isJavaToken(element, JavaTokenType.LPARENTH) || PsiUtil.isJavaToken(element, JavaTokenType.COMMA) ) { index++; } if ( PsiUtil.isJavaToken(element, JavaTokenType.RPARENTH) ) { return -1; } } return - 1; }
public static boolean isDefaultInstantiable(PsiClass psiClass) { final PsiMethod[] constructors = psiClass.getConstructors(); if (constructors.length == 0) { return true; } for (PsiMethod psiMethod : constructors) { final PsiParameterList parameterList = psiMethod.getParameterList(); if (psiMethod.getModifierList().hasModifierProperty("public") && parameterList.getParameters().length == 0) { return true; } } return false; }
static boolean areParameterListsEqual(PsiParameterList paramList1, PsiParameterList paramList2) { if (paramList1.getParametersCount() != paramList2.getParametersCount()) { return false; } final PsiParameter[] param1Params = paramList1.getParameters(); final PsiParameter[] param2Params = paramList2.getParameters(); for (int i = 0; i < param1Params.length; i++) { final PsiParameter param1Param = param1Params[i]; final PsiParameter param2Param = param2Params[i]; if (!areTypesPresentableEqual(param1Param.getType(), param2Param.getType())) { return false; } } return true; }
@SuppressWarnings("HardCodedStringLiteral") public static boolean isGetByStringOrByObjectMethod(@NotNull PsiMethod method) { String methodName = method.getName(); if(!"get".equals(methodName)) { return false; } PsiParameterList parameterList = method.getParameterList(); if(parameterList.getParametersCount() != 1) { return false; } PsiParameter parameter = parameterList.getParameters()[0]; final PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory(); PsiClassType javaLangString = factory.createTypeByFQClassName(JavaClassNames.JAVA_LANG_STRING, method.getResolveScope()); if(parameter.getType().isAssignableFrom(javaLangString)) { return true; } PsiClassType javaLangObject = factory.createTypeByFQClassName(JavaClassNames.JAVA_LANG_OBJECT, method.getResolveScope()); return parameter.getType().isAssignableFrom(javaLangObject); }
@Nullable public static HKey getKey(@NotNull PsiModifierListOwner owner, MessageDigest md) { LOG.assertTrue(owner instanceof PsiCompiledElement, owner); if(owner instanceof PsiMethod) { return BytecodeAnalysisConverter.psiKey((PsiMethod) owner, Out, md); } if(owner instanceof PsiParameter) { PsiElement parent = owner.getParent(); if(parent instanceof PsiParameterList) { PsiElement gParent = parent.getParent(); if(gParent instanceof PsiMethod) { final int index = ((PsiParameterList) parent).getParameterIndex((PsiParameter) owner); return BytecodeAnalysisConverter.psiKey((PsiMethod) gParent, new In(index, In.NOT_NULL_MASK), md); } } } return null; }
private static boolean maybeSuper(@NotNull final PsiMethod superMethod, @NotNull final PsiMethod method) { if(!superMethod.getName().equals(method.getName())) { return false; } final PsiParameterList superMethodParameterList = superMethod.getParameterList(); final PsiParameterList methodParameterList = method.getParameterList(); if(superMethodParameterList.getParametersCount() != methodParameterList.getParametersCount()) { return false; } final PsiParameter[] superMethodParameters = superMethodParameterList.getParameters(); final PsiParameter[] methodParameters = methodParameterList.getParameters(); for(int i = 0; i < methodParameters.length; i++) { if(!StringUtil.equals(getTypeShortName(superMethodParameters[i].getType()), getTypeShortName(methodParameters[i].getType()))) { return false; } } return true; }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(psiElement instanceof PsiParameterList) { String text = psiElement.getText(); if(StringUtil.startsWithChar(text, '(') && !StringUtil.endsWithChar(text, ')')) { PsiParameter[] params = ((PsiParameterList) psiElement).getParameters(); int offset; if(params.length == 0) { offset = psiElement.getTextRange().getStartOffset() + 1; } else { offset = params[params.length - 1].getTextRange().getEndOffset(); } editor.getDocument().insertString(offset, ")"); } } }
@Nullable private PsiMethod getSetupMethod(@NotNull PsiClass aClass) { final PsiMethod[] methods = aClass.findMethodsByName("setUp", false); for (PsiMethod method : methods) { if (method.hasModifierProperty(PsiModifier.STATIC)) { continue; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { continue; } if (PsiType.VOID.equals(method.getReturnType())) { return method; } } return null; }
public void visitMethod(JavaContext context, PsiMethod method) { boolean isPublicMethod = method.hasModifierProperty(PsiModifier.PUBLIC); if (!isPublicMethod) return; JavaEvaluator evaluator = context.getEvaluator(); if (evaluator == null) return; PsiParameterList parameterList = method.getParameterList(); PsiParameter[] psiParameters = parameterList.getParameters(); boolean hasBeanParameter = false; for (PsiParameter p : psiParameters) { PsiType type = p.getType(); if (!(type instanceof PsiClassType)) continue; PsiClassType classType = (PsiClassType) type; PsiClass psiCls = classType.resolve(); if (psiCls == null) continue; hasBeanParameter = Utils.isCustomBean(context, psiCls); if (hasBeanParameter) break; } if (hasBeanParameter) { mMethods.add(method); } }
/** * メソッドパラメータ取得 * @param method ルートノード * @return メソッドバラメータのリスト (空要素の場合もあります) */ public static @NonNull List<PsiParameter> getParameters(@NonNull PsiMethod method) { List<PsiParameter> parameterList; PsiParameterList parameters = method.getParameterList(); PsiParameter[] params = parameters.getParameters(); parameterList = Arrays.asList(params); return parameterList; }
@Override public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement psiElement) throws IncorrectOperationException { if(psiElement == null) { return; } final PsiCallExpression call = findParent(PsiCallExpression.class, psiElement); if(call == null) { return; } final PsiMethod psiMethod = call.resolveMethod(); if(psiMethod == null) { return; } final PsiParameterList parameterList = psiMethod.getParameterList(); if(parameterList == null) { return; } final PsiParameter[] params = parameterList.getParameters(); if(params == null) { return; } String prefix = ""; int offset = editor.getCaretModel().getOffset(); final Document doc = editor.getDocument(); for(final PsiParameter p : params) { doc.insertString(offset, prefix+p.getName()); offset += p.getName().length() + prefix.length(); prefix = ", "; } editor.getCaretModel().moveToOffset(offset + 1); }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super; if (method.isConstructor()) { return; } if (!TestUtils.isJUnit3TestMethod(method) && !TestUtils.isJUnit4TestMethod(method)) { return; } final PsiType returnType = method.getReturnType(); final PsiParameterList parameterList = method.getParameterList(); final boolean takesArguments; final boolean isStatic; if (parameterList.getParametersCount() == 0) { takesArguments = false; isStatic = method.hasModifierProperty(PsiModifier.STATIC); if (!isStatic && PsiType.VOID.equals(returnType) && method.hasModifierProperty(PsiModifier.PUBLIC)) { return; } } else { isStatic = false; takesArguments = true; } registerMethodError(method, Boolean.valueOf(takesArguments), Boolean.valueOf(isStatic)); }
private void checkParameters(PsiMethod superMethod, PsiParameter[] parameters) { if (m_ignoreOverridesOfLibraryMethods) { final PsiClass containingClass = superMethod.getContainingClass(); if (containingClass != null && LibraryUtil.classIsInLibrary(containingClass)) { return; } } final PsiParameterList superParameterList = superMethod.getParameterList(); final PsiParameter[] superParameters = superParameterList.getParameters(); for (int i = 0; i < parameters.length; i++) { final PsiParameter parameter = parameters[i]; final String parameterName = parameter.getName(); final String superParameterName = superParameters[i].getName(); if (superParameterName == null) { continue; } if (superParameterName.equals(parameterName)) { continue; } if (m_ignoreSingleCharacterNames && superParameterName.length() == 1) { continue; } registerVariableError(parameter, superParameterName); } }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super @NonNls final String methodName = method.getName(); if (!"tostring".equals(methodName)) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { return; } registerMethodError(method); }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super @NonNls final String methodName = method.getName(); if (!"equal".equals(methodName)) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 1) { return; } registerMethodError(method); }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super @NonNls final String methodName = method.getName(); if (!"compareto".equals(methodName)) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 1) { return; } registerMethodError(method); }
@Override public void visitMethod(@NotNull PsiMethod method) { //note: no call to super @NonNls final String methodName = method.getName(); if (!"hashcode".equals(methodName)) { return; } final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() != 0) { return; } registerMethodError(method); }