cleanup, notnull

GitOrigin-RevId: a66c9d2c162e016544602e12891669b6b64a050b
This commit is contained in:
Alexey Kudravtsev
2023-09-06 17:41:40 +02:00
committed by intellij-monorepo-bot
parent f6ed4df632
commit 56ebddcc41
3 changed files with 80 additions and 92 deletions

View File

@@ -51,13 +51,14 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
// null means uninitialized
private volatile List<String> ADDITIONAL_ANNOS;
@NotNull
public Collection<String> getAdditionalAnnotations() {
List<String> annos = ADDITIONAL_ANNOS;
if (annos == null) {
annos = new ArrayList<>();
Collections.addAll(annos, STANDARD_ANNOS);
for (EntryPoint extension : DEAD_CODE_EP_NAME.getExtensionList()) {
final String[] ignoredAnnotations = extension.getIgnoreAnnotations();
String[] ignoredAnnotations = extension.getIgnoreAnnotations();
if (ignoredAnnotations != null) {
ContainerUtil.addAll(annos, ignoredAnnotations);
}
@@ -68,9 +69,9 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
public JDOMExternalizableStringList ADDITIONAL_ANNOTATIONS = new JDOMExternalizableStringList();
protected List<String> myWriteAnnotations = new ArrayList<>();
private final Map<String, SmartRefElementPointer> myPersistentEntryPoints;
private final LinkedHashSet<ClassPattern> myPatterns = new LinkedHashSet<>();
private final Set<RefElement> myTemporaryEntryPoints;
private final Map<String, SmartRefElementPointer> myPersistentEntryPoints = new LinkedHashMap<>();
private final Set<ClassPattern> myPatterns = new LinkedHashSet<>(); // To keep the order between readExternal to writeExternal
private final Set<RefElement> myTemporaryEntryPoints = Collections.synchronizedSet(new HashSet<>());
private static final String VERSION = "2.0";
@NonNls private static final String VERSION_ATTR = "version";
@NonNls private static final String ENTRY_POINT_ATTR = "entry_point";
@@ -81,8 +82,6 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
public EntryPointsManagerBase(@NotNull Project project) {
myProject = project;
myTemporaryEntryPoints = Collections.synchronizedSet(new HashSet<>());
myPersistentEntryPoints = new LinkedHashMap<>(); // To keep the order between readExternal to writeExternal
DEAD_CODE_EP_NAME.addChangeListener(() -> {
if (ADDITIONAL_ANNOS != null) {
ADDITIONAL_ANNOS = null;
@@ -99,13 +98,13 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
public void loadState(@NotNull Element element) {
Element entryPointsElement = element.getChild("entry_points");
if (entryPointsElement != null) {
final String version = entryPointsElement.getAttributeValue(VERSION_ATTR);
String version = entryPointsElement.getAttributeValue(VERSION_ATTR);
if (!Comparing.strEqual(version, VERSION)) {
convert(entryPointsElement, myPersistentEntryPoints);
}
else {
List<Element> content = entryPointsElement.getChildren();
for (final Element entryElement : content) {
for (Element entryElement : content) {
if (ENTRY_POINT_ATTR.equals(entryElement.getName())) {
SmartRefElementPointerImpl entryPoint = new SmartRefElementPointerImpl(entryElement);
myPersistentEntryPoints.put(entryPoint.getFQName(), entryPoint);
@@ -121,16 +120,16 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
getPatterns().clear();
for (Element pattern : element.getChildren("pattern")) {
final ClassPattern classPattern = new ClassPattern();
ClassPattern classPattern = new ClassPattern();
XmlSerializer.deserializeInto(pattern, classPattern);
getPatterns().add(classPattern);
}
myWriteAnnotations.clear();
final Element writeAnnotations = element.getChild("writeAnnotations");
Element writeAnnotations = element.getChild("writeAnnotations");
if (writeAnnotations != null) {
for (Element annoElement : writeAnnotations.getChildren("writeAnnotation")) {
final String value = annoElement.getAttributeValue("name");
String value = annoElement.getAttributeValue("name");
if (value != null) {
myWriteAnnotations.add(value);
}
@@ -149,7 +148,7 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
if (!myWriteAnnotations.isEmpty()) {
final Element writeAnnotations = new Element("writeAnnotations");
Element writeAnnotations = new Element("writeAnnotations");
for (String writeAnnotation : myWriteAnnotations) {
writeAnnotations.addContent(new Element("writeAnnotation").setAttribute("name", writeAnnotation));
}
@@ -158,9 +157,9 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
return element;
}
public static void writeExternal(final Element element,
final Map<String, SmartRefElementPointer> persistentEntryPoints,
final JDOMExternalizableStringList additional_annotations) {
public static void writeExternal(@NotNull Element element,
@NotNull Map<String, SmartRefElementPointer> persistentEntryPoints,
@NotNull JDOMExternalizableStringList additional_annotations) {
Collection<SmartRefElementPointer> elementPointers = persistentEntryPoints.values();
if (!elementPointers.isEmpty()) {
Element entryPointsElement = new Element("entry_points");
@@ -178,7 +177,7 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
@Override
public void resolveEntryPoints(@NotNull final RefManager manager) {
public void resolveEntryPoints(@NotNull RefManager manager) {
if (!myResolved) {
myResolved = true;
cleanup();
@@ -211,10 +210,11 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
myTemporaryEntryPoints.clear();
}
private List<RefElementImpl> getPatternEntryPoints(RefManager manager) {
@NotNull
private List<RefElementImpl> getPatternEntryPoints(@NotNull RefManager manager) {
List<RefElementImpl> entries = new ArrayList<>();
for (ClassPattern pattern : myPatterns) {
final RefEntity refClass = ReadAction.compute(() -> manager.getReference(RefJavaManager.CLASS, pattern.pattern));
RefEntity refClass = ReadAction.compute(() -> manager.getReference(RefJavaManager.CLASS, pattern.pattern));
if (refClass != null) {
if (pattern.method.isEmpty()) {
for (RefMethod refMethod : ((RefClass)refClass).getConstructors()) {
@@ -242,14 +242,14 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
RefClass refClass = newEntryPoint instanceof RefMethod ? ((RefMethod)newEntryPoint).getOwnerClass()
: (RefClass)newEntryPoint;
if (refClass != null && !refClass.isAnonymous()) {
final ClassPattern classPattern = new ClassPattern();
ClassPattern classPattern = new ClassPattern();
classPattern.pattern = new SmartRefElementPointerImpl(refClass, true).getFQName();
if (newEntryPoint instanceof RefMethod && !(newEntryPoint instanceof RefImplicitConstructor)) {
classPattern.method = getMethodName(newEntryPoint);
}
getPatterns().add(classPattern);
final EntryPointsManager entryPointsManager = getInstance(newEntryPoint.getRefManager().getProject());
EntryPointsManager entryPointsManager = getInstance(newEntryPoint.getRefManager().getProject());
if (this != entryPointsManager) {
entryPointsManager.addEntryPoint(newEntryPoint, true);
}
@@ -259,9 +259,7 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
}
if (newEntryPoint instanceof RefClass) {
RefClass refClass = (RefClass)newEntryPoint;
if (newEntryPoint instanceof RefClass refClass) {
if (refClass.isAnonymous()) {
// Anonymous class cannot be an entry point.
return;
@@ -285,12 +283,12 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
else {
if (myPersistentEntryPoints.get(newEntryPoint.getExternalName()) == null) {
final SmartRefElementPointerImpl entry = new SmartRefElementPointerImpl(newEntryPoint, true);
SmartRefElementPointerImpl entry = new SmartRefElementPointerImpl(newEntryPoint, true);
myPersistentEntryPoints.put(entry.getFQName(), entry);
((RefElementImpl)newEntryPoint).setEntry(true);
((RefElementImpl)newEntryPoint).setPermanentEntry(true);
if (entry.isPersistent()) { //do save entry points
final EntryPointsManager entryPointsManager = getInstance(newEntryPoint.getRefManager().getProject());
EntryPointsManager entryPointsManager = getInstance(newEntryPoint.getRefManager().getProject());
if (this != entryPointsManager) {
entryPointsManager.addEntryPoint(newEntryPoint, true);
}
@@ -299,9 +297,10 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
}
@NotNull
private static String getMethodName(@NotNull RefElement newEntryPoint) {
String methodSignature = newEntryPoint.getName();
int indexOf = methodSignature.indexOf("(");
int indexOf = methodSignature.indexOf('(');
return indexOf > 0 ? methodSignature.substring(0, indexOf) : methodSignature;
}
@@ -325,17 +324,17 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
((RefElementImpl)anEntryPoint).setEntry(false);
if (anEntryPoint.isPermanentEntry() && anEntryPoint.isValid()) {
final Project project = anEntryPoint.getPsiElement().getProject();
final EntryPointsManager entryPointsManager = getInstance(project);
Project project = anEntryPoint.getPsiElement().getProject();
EntryPointsManager entryPointsManager = getInstance(project);
if (this != entryPointsManager) {
entryPointsManager.removeEntryPoint(anEntryPoint);
}
}
if (anEntryPoint instanceof RefMethod || anEntryPoint instanceof RefClass) {
final RefClass aClass = anEntryPoint instanceof RefClass ? (RefClass)anEntryPoint : ((RefMethod)anEntryPoint).getOwnerClass();
RefClass aClass = anEntryPoint instanceof RefClass ? (RefClass)anEntryPoint : ((RefMethod)anEntryPoint).getOwnerClass();
if (aClass != null) {
final String qualifiedName = aClass.getQualifiedName();
String qualifiedName = aClass.getQualifiedName();
for (Iterator<ClassPattern> iterator = getPatterns().iterator(); iterator.hasNext(); ) {
ClassPattern classPattern = iterator.next();
if (Objects.equals(classPattern.pattern, qualifiedName)) {
@@ -358,12 +357,12 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
@Override
public RefElement @NotNull [] getEntryPoints(RefManager refManager) {
public RefElement @NotNull [] getEntryPoints(@NotNull RefManager refManager) {
validateEntryPoints();
List<RefElement> entries = new ArrayList<>();
Collection<SmartRefElementPointer> collection = myPersistentEntryPoints.values();
for (SmartRefElementPointer refElementPointer : collection) {
final RefEntity elt = refElementPointer.getRefElement();
RefEntity elt = refElementPointer.getRefElement();
if (elt instanceof RefElement) {
entries.add((RefElement)elt);
}
@@ -394,13 +393,7 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
synchronized (myTemporaryEntryPoints) {
final Iterator<RefElement> it = myTemporaryEntryPoints.iterator();
while (it.hasNext()) {
RefElement refElement = it.next();
if (!refElement.isValid()) {
it.remove();
}
}
myTemporaryEntryPoints.removeIf(refElement -> !refElement.isValid());
}
}
}
@@ -424,12 +417,12 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
myPatterns.addAll(manager.getPatterns());
}
static void convert(@NotNull Element element, final Map<? super String, ? super SmartRefElementPointer> persistentEntryPoints) {
static void convert(@NotNull Element element, @NotNull Map<? super String, ? super SmartRefElementPointer> persistentEntryPoints) {
List<Element> content = element.getChildren();
for (final Element entryElement : content) {
for (Element entryElement : content) {
if (ENTRY_POINT_ATTR.equals(entryElement.getName())) {
String fqName = entryElement.getAttributeValue(SmartRefElementPointerImpl.FQNAME_ATTR);
final String type = entryElement.getAttributeValue(SmartRefElementPointerImpl.TYPE_ATTR);
String type = entryElement.getAttributeValue(SmartRefElementPointerImpl.TYPE_ATTR);
if (Comparing.strEqual(type, RefJavaManager.METHOD)) {
int spaceIdx = fqName.indexOf(' ');
@@ -439,19 +432,16 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
while (lastDotIdx > parenIndex) lastDotIdx = fqName.lastIndexOf('.', lastDotIdx - 1);
boolean notype = false;
if (spaceIdx < 0 || spaceIdx + 1 > lastDotIdx) {
notype = true;
}
boolean noType = spaceIdx < 0 || spaceIdx + 1 > lastDotIdx;
final String className = fqName.substring(notype ? 0 : spaceIdx + 1, lastDotIdx);
final String methodSignature =
notype ? fqName.substring(lastDotIdx + 1) : fqName.substring(0, spaceIdx) + ' ' + fqName.substring(lastDotIdx + 1);
String className = fqName.substring(noType ? 0 : spaceIdx + 1, lastDotIdx);
String methodSignature =
noType ? fqName.substring(lastDotIdx + 1) : fqName.substring(0, spaceIdx) + ' ' + fqName.substring(lastDotIdx + 1);
fqName = className + " " + methodSignature;
}
else if (Comparing.strEqual(type, RefJavaManager.FIELD)) {
final int lastDotIdx = fqName.lastIndexOf('.');
int lastDotIdx = fqName.lastIndexOf('.');
if (lastDotIdx > 0 && lastDotIdx < fqName.length() - 2) {
String className = fqName.substring(0, lastDotIdx);
String fieldName = fqName.substring(lastDotIdx + 1);
@@ -467,12 +457,12 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
}
public void setAddNonJavaEntries(final boolean addNonJavaEntries) {
public void setAddNonJavaEntries(boolean addNonJavaEntries) {
myAddNonJavaEntries = addNonJavaEntries;
}
@Override
public boolean isImplicitWrite(PsiElement element) {
public boolean isImplicitWrite(@NotNull PsiElement element) {
return element instanceof PsiField && AnnotationUtil.isAnnotated((PsiModifierListOwner)element, myWriteAnnotations, 0);
}
@@ -485,7 +475,7 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
if (element instanceof PsiClass) {
final String qualifiedName = ((PsiClass)element).getQualifiedName();
String qualifiedName = ((PsiClass)element).getQualifiedName();
if (qualifiedName != null) {
for (ClassPattern pattern : getPatterns()) {
if (pattern.method.isEmpty() && isAcceptedByPattern((PsiClass)element, qualifiedName, pattern, new HashSet<>())) {
@@ -496,16 +486,16 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
if (element instanceof PsiMethod) {
final PsiClass containingClass = ((PsiMethod)element).getContainingClass();
PsiClass containingClass = ((PsiMethod)element).getContainingClass();
if (containingClass != null) {
final String qualifiedName = containingClass.getQualifiedName();
String qualifiedName = containingClass.getQualifiedName();
if (qualifiedName != null) {
final String name = ((PsiMethod)element).getName();
String name = ((PsiMethod)element).getName();
for (ClassPattern pattern : getPatterns()) {
if (pattern.method.isEmpty()) continue;
boolean acceptedName = name.equals(pattern.method);
if (!acceptedName) {
final Pattern methodRegexp = pattern.getMethodRegexp();
Pattern methodRegexp = pattern.getMethodRegexp();
acceptedName = methodRegexp != null && methodRegexp.matcher(name).matches();
}
if (acceptedName && isAcceptedByPattern(containingClass, qualifiedName, pattern, new HashSet<>())) {
@@ -515,14 +505,14 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
}
}
final Collection<String> defaultAdditionalAnnotations = getAdditionalAnnotations();
Collection<String> defaultAdditionalAnnotations = getAdditionalAnnotations();
return AnnotationUtil.checkAnnotatedUsingPatterns(owner, ADDITIONAL_ANNOTATIONS) ||
AnnotationUtil.checkAnnotatedUsingPatterns(owner, defaultAdditionalAnnotations) ||
MetaAnnotationUtil.isMetaAnnotated(owner, ADDITIONAL_ANNOTATIONS) ||
MetaAnnotationUtil.isMetaAnnotated(owner, defaultAdditionalAnnotations);
}
private static boolean isAcceptedByPattern(@NotNull PsiClass element, String qualifiedName, ClassPattern pattern, Set<? super PsiClass> visited) {
private static boolean isAcceptedByPattern(@NotNull PsiClass element, @Nullable String qualifiedName, @NotNull ClassPattern pattern, @NotNull Set<? super PsiClass> visited) {
if (qualifiedName == null) {
return false;
}
@@ -531,7 +521,7 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
return true;
}
final Pattern regexp = pattern.getRegexp();
Pattern regexp = pattern.getRegexp();
if (regexp != null) {
try {
if (regexp.matcher(qualifiedName).matches()) {
@@ -543,7 +533,7 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
if (pattern.hierarchically) {
for (PsiClass superClass : element.getSupers()) {
final String superClassQualifiedName = superClass.getQualifiedName();
String superClassQualifiedName = superClass.getQualifiedName();
if (visited.add(superClass) && isAcceptedByPattern(superClass, superClassQualifiedName, pattern, visited)) {
return true;
}
@@ -552,15 +542,18 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
return false;
}
@NotNull
public List<String> getCustomAdditionalAnnotations() {
return List.copyOf(ADDITIONAL_ANNOTATIONS);
}
@NotNull
public List<String> getWriteAnnotations() {
return List.copyOf(myWriteAnnotations);
}
public LinkedHashSet<ClassPattern> getPatterns() {
@NotNull
public Set<ClassPattern> getPatterns() {
return myPatterns;
}
@@ -569,7 +562,7 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
@Attribute("value")
public @NlsSafe String pattern = "";
@Attribute("hierarchically")
public boolean hierarchically = false;
public boolean hierarchically;
@Attribute("method")
public String method = "";
@@ -616,15 +609,15 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
return result;
}
public Pattern getMethodRegexp() {
Pattern getMethodRegexp() {
if (methodRegexp == null && method.contains("*")) {
methodRegexp = createRegexp(method);
}
return methodRegexp;
}
private static Pattern createRegexp(final String pattern) {
final String replace = pattern.replace(".", "\\.").replace("*", ".*");
private static Pattern createRegexp(@NotNull String pattern) {
String replace = pattern.replace(".", "\\.").replace("*", ".*");
try {
return Pattern.compile(replace);
}
@@ -635,9 +628,10 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
}
public class AddImplicitlyWriteAnnotation implements IntentionAction, LocalQuickFix {
@NotNull
private final String myQualifiedName;
public AddImplicitlyWriteAnnotation(String qualifiedName) {myQualifiedName = qualifiedName;}
public AddImplicitlyWriteAnnotation(@NotNull String qualifiedName) {myQualifiedName = qualifiedName;}
@Override
@NotNull
@@ -700,7 +694,7 @@ public abstract class EntryPointsManagerBase extends EntryPointsManager implemen
});
}
private void doAddAnnotation(Project project) {
private void doAddAnnotation(@NotNull Project project) {
if (!myWriteAnnotations.contains(myQualifiedName)) {
myWriteAnnotations.add(myQualifiedName);
ProjectInspectionProfileManager.getInstance(project).fireProfileChanged();