Skip to content

Commit 3d5860b

Browse files
authored
PsiAnnotationOwner refactoring (#185)
* Reformatting PsiAnnotationXx. * Refactoring PsiAnnotationXx. * Splitting PsiAnnotationAttributeValues.java to separate class files. * Reformatting descendants of PsiAnnotationOwner. * Reformatting PsiModifierList. * Refactoring descendants of PsiAnnotationOwner.
1 parent cbd69f7 commit 3d5860b

30 files changed

Lines changed: 3345 additions & 3169 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2000-2019 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.
2+
package com.intellij.java.language.psi;
3+
4+
import com.intellij.java.language.jvm.annotation.JvmAnnotationArrayValue;
5+
import com.intellij.java.language.jvm.annotation.JvmAnnotationAttributeValue;
6+
import consulo.util.collection.ContainerUtil;
7+
import jakarta.annotation.Nonnull;
8+
9+
import java.util.List;
10+
11+
class PsiAnnotationArrayValue extends PsiAnnotationAttributeValue<PsiArrayInitializerMemberValue> implements JvmAnnotationArrayValue {
12+
PsiAnnotationArrayValue(@Nonnull PsiArrayInitializerMemberValue value) {
13+
super(value);
14+
}
15+
16+
@Nonnull
17+
@Override
18+
public List<JvmAnnotationAttributeValue> getValues() {
19+
return ContainerUtil.map(myElement.getInitializers(), PsiJvmConversionHelper::getAnnotationAttributeValue);
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2000-2019 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.
2+
package com.intellij.java.language.psi;
3+
4+
import com.intellij.java.language.jvm.annotation.JvmAnnotationAttributeValue;
5+
import jakarta.annotation.Nonnull;
6+
7+
abstract class PsiAnnotationAttributeValue<T extends PsiAnnotationMemberValue> implements JvmAnnotationAttributeValue {
8+
protected final T myElement;
9+
10+
protected PsiAnnotationAttributeValue(@Nonnull T value) {
11+
myElement = value;
12+
}
13+
}

java-language-api/src/main/java/com/intellij/java/language/psi/PsiAnnotationAttributeValues.java

Lines changed: 0 additions & 106 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2000-2019 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.
2+
package com.intellij.java.language.psi;
3+
4+
import com.intellij.java.language.jvm.JvmClass;
5+
import com.intellij.java.language.jvm.annotation.JvmAnnotationClassValue;
6+
import consulo.annotation.access.RequiredReadAction;
7+
import consulo.language.psi.PsiElement;
8+
import jakarta.annotation.Nonnull;
9+
import jakarta.annotation.Nullable;
10+
11+
class PsiAnnotationClassValue extends PsiAnnotationAttributeValue<PsiClassObjectAccessExpression> implements JvmAnnotationClassValue {
12+
PsiAnnotationClassValue(@Nonnull PsiClassObjectAccessExpression value) {
13+
super(value);
14+
}
15+
16+
private PsiJavaCodeReferenceElement getReferenceElement() {
17+
return myElement.getOperand().getInnermostComponentReferenceElement();
18+
}
19+
20+
@Nullable
21+
@Override
22+
public String getQualifiedName() {
23+
PsiJavaCodeReferenceElement referenceElement = getReferenceElement();
24+
return referenceElement == null ? null : referenceElement.getQualifiedName();
25+
}
26+
27+
@Nullable
28+
@Override
29+
@RequiredReadAction
30+
public JvmClass getClazz() {
31+
PsiJavaCodeReferenceElement referenceElement = getReferenceElement();
32+
if (referenceElement == null) {
33+
return null;
34+
}
35+
PsiElement resolved = referenceElement.resolve();
36+
return resolved instanceof JvmClass jvmClass ? jvmClass : null;
37+
}
38+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2000-2019 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.
2+
package com.intellij.java.language.psi;
3+
4+
import com.intellij.java.language.jvm.annotation.JvmAnnotationConstantValue;
5+
import jakarta.annotation.Nonnull;
6+
import jakarta.annotation.Nullable;
7+
8+
class PsiAnnotationConstantValue extends PsiAnnotationAttributeValue<PsiExpression> implements JvmAnnotationConstantValue {
9+
PsiAnnotationConstantValue(@Nonnull PsiExpression value) {
10+
super(value);
11+
}
12+
13+
@Nullable
14+
@Override
15+
public Object getConstantValue() {
16+
PsiConstantEvaluationHelper evaluationHelper = JavaPsiFacade.getInstance(myElement.getProject()).getConstantEvaluationHelper();
17+
return evaluationHelper.computeConstantExpression(myElement);
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2000-2019 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.
2+
package com.intellij.java.language.psi;
3+
4+
import com.intellij.java.language.jvm.JvmEnumField;
5+
import com.intellij.java.language.jvm.annotation.JvmAnnotationEnumFieldValue;
6+
import jakarta.annotation.Nonnull;
7+
import jakarta.annotation.Nullable;
8+
9+
class PsiAnnotationEnumFieldValue extends PsiAnnotationAttributeValue<PsiReferenceExpression> implements JvmAnnotationEnumFieldValue {
10+
private final JvmEnumField myEnumField;
11+
12+
PsiAnnotationEnumFieldValue(@Nonnull PsiReferenceExpression value, @Nonnull JvmEnumField field) {
13+
super(value);
14+
myEnumField = field;
15+
}
16+
17+
@Nullable
18+
@Override
19+
public JvmEnumField getField() {
20+
return myEnumField;
21+
}
22+
}

java-language-api/src/main/java/com/intellij/java/language/psi/PsiAnnotationMemberValue.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
* @author ven
2626
*/
2727
public interface PsiAnnotationMemberValue extends PsiElement {
28-
/**
29-
* The empty array of PSI annotation member values which can be reused to avoid unnecessary allocations.
30-
*/
31-
PsiAnnotationMemberValue[] EMPTY_ARRAY = new PsiAnnotationMemberValue[0];
28+
/**
29+
* The empty array of PSI annotation member values which can be reused to avoid unnecessary allocations.
30+
*/
31+
PsiAnnotationMemberValue[] EMPTY_ARRAY = new PsiAnnotationMemberValue[0];
3232

33-
ArrayFactory<PsiAnnotationMemberValue> ARRAY_FACTORY = new ArrayFactory<PsiAnnotationMemberValue>() {
34-
@Nonnull
35-
@Override
36-
public PsiAnnotationMemberValue[] create(final int count) {
37-
return count == 0 ? EMPTY_ARRAY : new PsiAnnotationMemberValue[count];
38-
}
39-
};
33+
ArrayFactory<PsiAnnotationMemberValue> ARRAY_FACTORY = new ArrayFactory<>() {
34+
@Nonnull
35+
@Override
36+
public PsiAnnotationMemberValue[] create(int count) {
37+
return count == 0 ? EMPTY_ARRAY : new PsiAnnotationMemberValue[count];
38+
}
39+
};
4040
}

java-language-api/src/main/java/com/intellij/java/language/psi/PsiAnnotationMethod.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
* @author ven
2525
*/
2626
public interface PsiAnnotationMethod extends PsiMethod {
27-
/**
28-
* The empty array of PSI annotation methods which can be reused to avoid unnecessary allocations.
29-
*/
30-
PsiAnnotationMethod[] EMPTY_ARRAY = new PsiAnnotationMethod[0];
27+
/**
28+
* The empty array of PSI annotation methods which can be reused to avoid unnecessary allocations.
29+
*/
30+
PsiAnnotationMethod[] EMPTY_ARRAY = new PsiAnnotationMethod[0];
3131

32-
/**
33-
* Returns the default value of the annotation element defined by the method.
34-
*
35-
* @return the default value of the element, or null if no default value is specified.
36-
*/
37-
@Nullable
38-
PsiAnnotationMemberValue getDefaultValue();
32+
/**
33+
* Returns the default value of the annotation element defined by the method.
34+
*
35+
* @return the default value of the element, or null if no default value is specified.
36+
*/
37+
@Nullable
38+
PsiAnnotationMemberValue getDefaultValue();
3939
}

java-language-api/src/main/java/com/intellij/java/language/psi/PsiAnnotationOwner.java

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,59 @@
1515
*/
1616
package com.intellij.java.language.psi;
1717

18-
import org.jetbrains.annotations.NonNls;
1918
import jakarta.annotation.Nonnull;
20-
2119
import jakarta.annotation.Nullable;
2220

2321
/**
2422
* @author cdr
2523
*/
26-
public interface PsiAnnotationOwner
27-
{
28-
/**
29-
* Returns the list of annotations syntactically contained in the element.
30-
*
31-
* @return the list of annotations.
32-
*/
33-
@Nonnull
34-
PsiAnnotation[] getAnnotations();
24+
public interface PsiAnnotationOwner {
25+
/**
26+
* Returns the list of annotations syntactically contained in the element.
27+
*
28+
* @return the list of annotations.
29+
*/
30+
@Nonnull
31+
PsiAnnotation[] getAnnotations();
3532

36-
/**
37-
* @return the list of annotations which are applicable to this owner
38-
* (e.g. type annotations on method belong to its type element, not the method).
39-
*/
40-
@Nonnull
41-
PsiAnnotation[] getApplicableAnnotations();
33+
/**
34+
* @return the list of annotations which are applicable to this owner
35+
* (e.g. type annotations on method belong to its type element, not the method).
36+
*/
37+
@Nonnull
38+
PsiAnnotation[] getApplicableAnnotations();
4239

43-
/**
44-
* Searches the owner for an annotation with the specified fully qualified name
45-
* and returns {@code true} if it is found.
46-
* <p/>
47-
* This method is preferable over {@link #findAnnotation}
48-
* since implementations are free not to instantiate the {@link PsiAnnotation}.
49-
*
50-
* @param qualifiedName the fully qualified name of the annotation to find
51-
* @return {@code true} is such annotation is found, otherwise {@code false}
52-
*/
53-
default boolean hasAnnotation(@Nonnull @NonNls String qualifiedName)
54-
{
55-
//noinspection SSBasedInspection
56-
return findAnnotation(qualifiedName) != null;
57-
}
40+
/**
41+
* Searches the owner for an annotation with the specified fully qualified name
42+
* and returns {@code true} if it is found.
43+
* <p/>
44+
* This method is preferable over {@link #findAnnotation}
45+
* since implementations are free not to instantiate the {@link PsiAnnotation}.
46+
*
47+
* @param qualifiedName the fully qualified name of the annotation to find
48+
* @return {@code true} is such annotation is found, otherwise {@code false}
49+
*/
50+
default boolean hasAnnotation(@Nonnull String qualifiedName) {
51+
//noinspection SSBasedInspection
52+
return findAnnotation(qualifiedName) != null;
53+
}
5854

59-
/**
60-
* Searches the owner for an annotation with the specified fully qualified name
61-
* and returns one if it is found.
62-
*
63-
* @param qualifiedName the fully qualified name of the annotation to find.
64-
* @return the annotation instance, or null if no such annotation is found.
65-
*/
66-
@Nullable
67-
PsiAnnotation findAnnotation(@Nonnull @NonNls String qualifiedName);
55+
/**
56+
* Searches the owner for an annotation with the specified fully qualified name
57+
* and returns one if it is found.
58+
*
59+
* @param qualifiedName the fully qualified name of the annotation to find.
60+
* @return the annotation instance, or null if no such annotation is found.
61+
*/
62+
@Nullable
63+
PsiAnnotation findAnnotation(@Nonnull String qualifiedName);
6864

69-
/**
70-
* Adds a new annotation to this owner. The annotation class name will be shortened. No attributes will be defined.
71-
*
72-
* @param qualifiedName qualifiedName
73-
* @return newly added annotation
74-
*/
75-
@Nonnull
76-
PsiAnnotation addAnnotation(@Nonnull @NonNls String qualifiedName);
65+
/**
66+
* Adds a new annotation to this owner. The annotation class name will be shortened. No attributes will be defined.
67+
*
68+
* @param qualifiedName qualifiedName
69+
* @return newly added annotation
70+
*/
71+
@Nonnull
72+
PsiAnnotation addAnnotation(@Nonnull String qualifiedName);
7773
}

0 commit comments

Comments
 (0)