diff --git a/java/java-psi-api/src/com/intellij/lang/jvm/JvmClass.java b/java/java-psi-api/src/com/intellij/lang/jvm/JvmClass.java index bcd199354713..fbb50eeb0970 100644 --- a/java/java-psi-api/src/com/intellij/lang/jvm/JvmClass.java +++ b/java/java-psi-api/src/com/intellij/lang/jvm/JvmClass.java @@ -1,21 +1,8 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.lang.jvm; import com.intellij.lang.jvm.types.JvmReferenceType; +import com.intellij.lang.jvm.util.JvmClassDefaults; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -70,6 +57,15 @@ public interface JvmClass extends JvmTypeParametersOwner, JvmTypeDeclaration { @NotNull JvmMethod[] getMethods(); + /** + * @return methods (excluding inherited) with the specified name + * @see Class#getDeclaredMethod + */ + @NotNull + default JvmMethod[] findMethodsByName(@NotNull String methodName) { + return JvmClassDefaults.findMethodsByName(this, methodName); + } + /** * @return all (static, private, etc) fields declared by this class but excluding inherited ones * @see Class#getDeclaredFields diff --git a/java/java-psi-api/src/com/intellij/lang/jvm/JvmMethod.java b/java/java-psi-api/src/com/intellij/lang/jvm/JvmMethod.java index 1bbdd0afb128..110498fcbe2e 100644 --- a/java/java-psi-api/src/com/intellij/lang/jvm/JvmMethod.java +++ b/java/java-psi-api/src/com/intellij/lang/jvm/JvmMethod.java @@ -15,6 +15,8 @@ import org.jetbrains.annotations.Nullable; */ public interface JvmMethod extends JvmTypeParametersOwner { + JvmMethod[] EMPTY_ARRAY = new JvmMethod[0]; + /** * @return {@code true} if this method is a constructor */ diff --git a/java/java-psi-api/src/com/intellij/lang/jvm/util/JvmClassDefaults.java b/java/java-psi-api/src/com/intellij/lang/jvm/util/JvmClassDefaults.java new file mode 100644 index 000000000000..4922552a88f1 --- /dev/null +++ b/java/java-psi-api/src/com/intellij/lang/jvm/util/JvmClassDefaults.java @@ -0,0 +1,24 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.lang.jvm.util; + +import com.intellij.lang.jvm.JvmClass; +import com.intellij.lang.jvm.JvmMethod; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +import static com.intellij.util.containers.ContainerUtil.filter; + +/** + * This class holds default implementations of {@link JvmClass} methods. + */ +public class JvmClassDefaults { + + private JvmClassDefaults() {} + + @NotNull + public static JvmMethod[] findMethodsByName(@NotNull JvmClass clazz, @NotNull String methodName) { + List result = filter(clazz.getMethods(), it -> it.getName().equals(methodName)); + return result.toArray(JvmMethod.EMPTY_ARRAY); + } +} diff --git a/java/java-psi-api/src/com/intellij/psi/PsiClass.java b/java/java-psi-api/src/com/intellij/psi/PsiClass.java index 2dce3b543b50..54d17da8eca1 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiClass.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiClass.java @@ -1,22 +1,9 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.psi; import com.intellij.lang.jvm.JvmClass; import com.intellij.lang.jvm.JvmClassKind; +import com.intellij.lang.jvm.JvmMethod; import com.intellij.lang.jvm.types.JvmReferenceType; import com.intellij.openapi.util.Pair; import com.intellij.pom.PomRenameableTarget; @@ -244,6 +231,12 @@ public interface PsiClass @NotNull PsiMethod[] findMethodsBySignature(PsiMethod patternMethod, boolean checkBases); + @NotNull + @Override + default JvmMethod[] findMethodsByName(@NotNull String methodName) { + return findMethodsByName(methodName, false); + } + /** * Searches the class (and optionally its superclasses) for the methods with the specified name. *