|
26 | 26 |
|
27 | 27 | import jakarta.annotation.Nonnull; |
28 | 28 | import jakarta.annotation.Nullable; |
| 29 | + |
29 | 30 | import java.util.ArrayList; |
30 | 31 | import java.util.Collections; |
31 | 32 | import java.util.List; |
|
36 | 37 | * @author Pavel.Dolgov |
37 | 38 | */ |
38 | 39 | public class MergePackageAccessibilityStatementsFix extends MergeModuleStatementsFix<PsiPackageAccessibilityStatement> { |
| 40 | + private static final Logger LOG = Logger.getInstance(MergePackageAccessibilityStatementsFix.class); |
| 41 | + private final String myPackageName; |
| 42 | + private final Role myRole; |
39 | 43 |
|
40 | | - private static final Logger LOG = Logger.getInstance(MergePackageAccessibilityStatementsFix.class); |
41 | | - private final String myPackageName; |
42 | | - private final Role myRole; |
43 | | - |
44 | | - protected MergePackageAccessibilityStatementsFix(@Nonnull PsiJavaModule javaModule, @Nonnull String packageName, @Nonnull Role role) { |
45 | | - super(javaModule); |
46 | | - myPackageName = packageName; |
47 | | - myRole = role; |
48 | | - } |
| 44 | + protected MergePackageAccessibilityStatementsFix(@Nonnull PsiJavaModule javaModule, @Nonnull String packageName, @Nonnull Role role) { |
| 45 | + super(javaModule); |
| 46 | + myPackageName = packageName; |
| 47 | + myRole = role; |
| 48 | + } |
49 | 49 |
|
50 | | - @Nls |
51 | | - @Nonnull |
52 | | - @Override |
53 | | - public String getText() { |
54 | | - return JavaQuickFixBundle.message("java.9.merge.module.statements.fix.name", getKeyword(), myPackageName); |
55 | | - } |
| 50 | + @Nls |
| 51 | + @Nonnull |
| 52 | + @Override |
| 53 | + public String getText() { |
| 54 | + return JavaQuickFixBundle.message("java.9.merge.module.statements.fix.name", getKeyword(), myPackageName); |
| 55 | + } |
56 | 56 |
|
57 | | - @Nls |
58 | | - @Nonnull |
59 | | - @Override |
60 | | - public String getFamilyName() { |
61 | | - return JavaQuickFixBundle.message("java.9.merge.module.statements.fix.family.name", getKeyword()); |
62 | | - } |
| 57 | + @Nls |
| 58 | + @Nonnull |
| 59 | + @Override |
| 60 | + public String getFamilyName() { |
| 61 | + return JavaQuickFixBundle.message("java.9.merge.module.statements.fix.family.name", getKeyword()); |
| 62 | + } |
63 | 63 |
|
64 | | - @Nonnull |
65 | | - @Override |
66 | | - protected String getReplacementText(@Nonnull List<PsiPackageAccessibilityStatement> statementsToMerge) { |
67 | | - final List<String> moduleNames = getModuleNames(statementsToMerge); |
68 | | - if (!moduleNames.isEmpty()) { |
69 | | - return getKeyword() + " " + myPackageName + " " + PsiKeyword.TO + " " + joinUniqueNames(moduleNames) + ";"; |
| 64 | + @Nonnull |
| 65 | + @Override |
| 66 | + protected String getReplacementText(@Nonnull List<PsiPackageAccessibilityStatement> statementsToMerge) { |
| 67 | + final List<String> moduleNames = getModuleNames(statementsToMerge); |
| 68 | + if (!moduleNames.isEmpty()) { |
| 69 | + return getKeyword() + " " + myPackageName + " " + PsiKeyword.TO + " " + joinUniqueNames(moduleNames) + ";"; |
| 70 | + } |
| 71 | + return getKeyword() + " " + myPackageName + ";"; |
70 | 72 | } |
71 | | - return getKeyword() + " " + myPackageName + ";"; |
72 | | - } |
73 | 73 |
|
74 | | - @Nonnull |
75 | | - private static List<String> getModuleNames(@Nonnull List<PsiPackageAccessibilityStatement> statements) { |
76 | | - final List<String> result = new ArrayList<>(); |
77 | | - for (PsiPackageAccessibilityStatement statement : statements) { |
78 | | - final List<String> moduleNames = statement.getModuleNames(); |
79 | | - if (moduleNames.isEmpty()) { |
80 | | - return Collections.emptyList(); |
81 | | - } |
82 | | - result.addAll(moduleNames); |
| 74 | + @Nonnull |
| 75 | + private static List<String> getModuleNames(@Nonnull List<PsiPackageAccessibilityStatement> statements) { |
| 76 | + final List<String> result = new ArrayList<>(); |
| 77 | + for (PsiPackageAccessibilityStatement statement : statements) { |
| 78 | + final List<String> moduleNames = statement.getModuleNames(); |
| 79 | + if (moduleNames.isEmpty()) { |
| 80 | + return Collections.emptyList(); |
| 81 | + } |
| 82 | + result.addAll(moduleNames); |
| 83 | + } |
| 84 | + return result; |
83 | 85 | } |
84 | | - return result; |
85 | | - } |
86 | 86 |
|
87 | | - @Nonnull |
88 | | - @Override |
89 | | - protected List<PsiPackageAccessibilityStatement> getStatementsToMerge(@Nonnull PsiJavaModule javaModule) { |
90 | | - return StreamSupport.stream(getStatements(javaModule, myRole).spliterator(), false).filter(statement -> myPackageName.equals(statement.getPackageName())).collect(Collectors.toList()); |
91 | | - } |
| 87 | + @Nonnull |
| 88 | + @Override |
| 89 | + protected List<PsiPackageAccessibilityStatement> getStatementsToMerge(@Nonnull PsiJavaModule javaModule) { |
| 90 | + return StreamSupport.stream(getStatements(javaModule, myRole).spliterator(), false) |
| 91 | + .filter(statement -> myPackageName.equals(statement.getPackageName())) |
| 92 | + .collect(Collectors.toList()); |
| 93 | + } |
92 | 94 |
|
93 | | - @Nullable |
94 | | - public static MergeModuleStatementsFix createFix(@Nullable PsiPackageAccessibilityStatement statement) { |
95 | | - if (statement != null) { |
96 | | - final PsiElement parent = statement.getParent(); |
97 | | - if (parent instanceof PsiJavaModule) { |
98 | | - final String packageName = statement.getPackageName(); |
99 | | - if (packageName != null) { |
100 | | - return new MergePackageAccessibilityStatementsFix((PsiJavaModule) parent, packageName, statement.getRole()); |
| 95 | + @Nullable |
| 96 | + public static MergeModuleStatementsFix createFix(@Nullable PsiPackageAccessibilityStatement statement) { |
| 97 | + if (statement != null) { |
| 98 | + final PsiElement parent = statement.getParent(); |
| 99 | + if (parent instanceof PsiJavaModule) { |
| 100 | + final String packageName = statement.getPackageName(); |
| 101 | + if (packageName != null) { |
| 102 | + return new MergePackageAccessibilityStatementsFix((PsiJavaModule) parent, packageName, statement.getRole()); |
| 103 | + } |
| 104 | + } |
101 | 105 | } |
102 | | - } |
| 106 | + return null; |
103 | 107 | } |
104 | | - return null; |
105 | | - } |
106 | 108 |
|
107 | | - @Nonnull |
108 | | - private static Iterable<PsiPackageAccessibilityStatement> getStatements(@Nonnull PsiJavaModule javaModule, @Nonnull Role role) { |
109 | | - switch (role) { |
110 | | - case OPENS: |
111 | | - return javaModule.getOpens(); |
112 | | - case EXPORTS: |
113 | | - return javaModule.getExports(); |
| 109 | + @Nonnull |
| 110 | + private static Iterable<PsiPackageAccessibilityStatement> getStatements(@Nonnull PsiJavaModule javaModule, @Nonnull Role role) { |
| 111 | + switch (role) { |
| 112 | + case OPENS: |
| 113 | + return javaModule.getOpens(); |
| 114 | + case EXPORTS: |
| 115 | + return javaModule.getExports(); |
| 116 | + } |
| 117 | + LOG.error("Unexpected role " + role); |
| 118 | + return Collections.emptyList(); |
114 | 119 | } |
115 | | - LOG.error("Unexpected role " + role); |
116 | | - return Collections.emptyList(); |
117 | | - } |
118 | 120 |
|
119 | | - @Nonnull |
120 | | - private String getKeyword() { |
121 | | - switch (myRole) { |
122 | | - case OPENS: |
123 | | - return PsiKeyword.OPENS; |
124 | | - case EXPORTS: |
125 | | - return PsiKeyword.EXPORTS; |
| 121 | + @Nonnull |
| 122 | + private String getKeyword() { |
| 123 | + switch (myRole) { |
| 124 | + case OPENS: |
| 125 | + return PsiKeyword.OPENS; |
| 126 | + case EXPORTS: |
| 127 | + return PsiKeyword.EXPORTS; |
| 128 | + } |
| 129 | + LOG.error("Unexpected role " + myRole); |
| 130 | + return ""; |
126 | 131 | } |
127 | | - LOG.error("Unexpected role " + myRole); |
128 | | - return ""; |
129 | | - } |
130 | 132 | } |
0 commit comments