[jvm-lang] add JvmAnnotatedElement #getAnnotation and #hasAnnotation

This commit is contained in:
Daniil Ovchinnikov
2018-04-09 17:42:56 +03:00
parent e523947ed7
commit 3a43ebcaf2
5 changed files with 65 additions and 33 deletions
@@ -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;
}
}
@@ -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;
}
}
@@ -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<JvmModifier> result = EnumSet.noneOf(JvmModifier.class);
@@ -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() {
@@ -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() {