diff --git a/java/java-psi-api/src/com/intellij/lang/jvm/JvmAnnotatedElement.java b/java/java-psi-api/src/com/intellij/lang/jvm/JvmAnnotatedElement.java index c0a09daa1d68..078a31b8ed63 100644 --- a/java/java-psi-api/src/com/intellij/lang/jvm/JvmAnnotatedElement.java +++ b/java/java-psi-api/src/com/intellij/lang/jvm/JvmAnnotatedElement.java @@ -1,24 +1,27 @@ -/* - * 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 org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public interface JvmAnnotatedElement extends JvmElement { @NotNull JvmAnnotation[] getAnnotations(); + + /** + * @since 2018.2 + */ + @Nullable + default JvmAnnotation getAnnotation(@NotNull @NonNls String fqn) { + return JvmAnnotatedElementDefaults.getAnnotation(this, fqn); + } + + /** + * @since 2018.2 + */ + default boolean hasAnnotation(@NotNull @NonNls String fqn) { + return getAnnotation(fqn) != null; + } } diff --git a/java/java-psi-api/src/com/intellij/lang/jvm/JvmAnnotatedElementDefaults.java b/java/java-psi-api/src/com/intellij/lang/jvm/JvmAnnotatedElementDefaults.java new file mode 100644 index 000000000000..26fce4b2e9a8 --- /dev/null +++ b/java/java-psi-api/src/com/intellij/lang/jvm/JvmAnnotatedElementDefaults.java @@ -0,0 +1,16 @@ +// 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 org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +class JvmAnnotatedElementDefaults { + + @Nullable + static JvmAnnotation getAnnotation(@NotNull JvmAnnotatedElement element, @NotNull String fqn) { + for (JvmAnnotation annotation : element.getAnnotations()) { + if (fqn.equals(annotation.getQualifiedName())) return annotation; + } + return null; + } +} diff --git a/java/java-psi-api/src/com/intellij/psi/PsiJvmConversionHelper.java b/java/java-psi-api/src/com/intellij/psi/PsiJvmConversionHelper.java index 9465f38d5ecc..5ddda5deb41e 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiJvmConversionHelper.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiJvmConversionHelper.java @@ -1,4 +1,4 @@ -// Copyright 2000-2017 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. +// 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.JvmClassKind; @@ -41,6 +41,17 @@ class PsiJvmConversionHelper { return list == null ? PsiAnnotation.EMPTY_ARRAY : list.getAnnotations(); } + @Nullable + static PsiAnnotation getListAnnotation(@NotNull PsiModifierListOwner modifierListOwner, @NotNull String fqn) { + PsiModifierList list = modifierListOwner.getModifierList(); + return list == null ? null : list.findAnnotation(fqn); + } + + static boolean hasListAnnotation(@NotNull PsiModifierListOwner modifierListOwner, @NotNull String fqn) { + PsiModifierList list = modifierListOwner.getModifierList(); + return list != null && list.hasAnnotation(fqn); + } + @NotNull static JvmModifier[] getListModifiers(@NotNull PsiModifierListOwner modifierListOwner) { final Set result = EnumSet.noneOf(JvmModifier.class); diff --git a/java/java-psi-api/src/com/intellij/psi/PsiModifierListOwner.java b/java/java-psi-api/src/com/intellij/psi/PsiModifierListOwner.java index 127ff21f2ae0..f9a5b18129b8 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiModifierListOwner.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiModifierListOwner.java @@ -1,18 +1,4 @@ -/* - * 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.JvmModifier; @@ -21,8 +7,7 @@ import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import static com.intellij.psi.PsiJvmConversionHelper.getListAnnotations; -import static com.intellij.psi.PsiJvmConversionHelper.getListModifiers; +import static com.intellij.psi.PsiJvmConversionHelper.*; /** * Represents a PSI element which has a list of modifiers (public/private/protected/etc.) @@ -53,6 +38,17 @@ public interface PsiModifierListOwner extends PsiElement, JvmModifiersOwner { return getListAnnotations(this); } + @Nullable + @Override + default PsiAnnotation getAnnotation(@NotNull @NonNls String fqn) { + return getListAnnotation(this, fqn); + } + + @Override + default boolean hasAnnotation(@NotNull @NonNls String fqn) { + return hasListAnnotation(this, fqn); + } + @NotNull @Override default JvmModifier[] getModifiers() { diff --git a/java/java-psi-api/src/com/intellij/psi/PsiTypeParameter.java b/java/java-psi-api/src/com/intellij/psi/PsiTypeParameter.java index 580f4bd1ef89..cc2c784593ca 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiTypeParameter.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiTypeParameter.java @@ -5,6 +5,7 @@ import com.intellij.lang.jvm.JvmElementVisitor; import com.intellij.lang.jvm.JvmTypeParameter; import com.intellij.lang.jvm.types.JvmReferenceType; import com.intellij.util.ArrayFactory; +import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -52,6 +53,11 @@ public interface PsiTypeParameter extends PsiClass, PsiAnnotationOwner, JvmTypeP return PsiClass.super.getAnnotations(); } + @Override + default boolean hasAnnotation(@NotNull @NonNls String fqn) { + return PsiClass.super.hasAnnotation(fqn); + } + @NotNull @Override default JvmReferenceType[] getBounds() {