Minor cleanup in Java model: fix warnings, remove unused

This commit is contained in:
Alexander Udalov
2016-03-17 20:39:12 +03:00
parent 0a54464420
commit 0d74fc2290
10 changed files with 23 additions and 63 deletions
@@ -24,40 +24,19 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.load.java.structure.JavaAnnotation; import org.jetbrains.kotlin.load.java.structure.JavaAnnotation;
import org.jetbrains.kotlin.load.java.structure.JavaAnnotationOwner; import org.jetbrains.kotlin.load.java.structure.JavaAnnotationOwner;
import org.jetbrains.kotlin.load.java.structure.impl.JavaAnnotationImpl; import org.jetbrains.kotlin.load.java.structure.impl.JavaAnnotationImpl;
import org.jetbrains.kotlin.load.java.structure.impl.JavaElementCollectionFromPsiArrayUtil;
import org.jetbrains.kotlin.load.java.structure.impl.JavaModifierListOwnerImpl; import org.jetbrains.kotlin.load.java.structure.impl.JavaModifierListOwnerImpl;
import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.FqName;
import java.util.Collection;
import java.util.Collections;
public class PsiBasedExternalAnnotationResolver implements ExternalAnnotationResolver { public class PsiBasedExternalAnnotationResolver implements ExternalAnnotationResolver {
@Nullable @Nullable
@Override @Override
public JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName) { public JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName) {
if (owner instanceof JavaModifierListOwnerImpl) { if (owner instanceof JavaModifierListOwnerImpl) {
JavaModifierListOwnerImpl modifierListOwner = (JavaModifierListOwnerImpl) owner; PsiModifierListOwner psiOwner = ((JavaModifierListOwnerImpl) owner).getPsi();
PsiAnnotation psiAnnotation = findExternalAnnotation(modifierListOwner.getPsi(), fqName); PsiAnnotation psiAnnotation =
ExternalAnnotationsManager.getInstance(psiOwner.getProject()).findExternalAnnotation(psiOwner, fqName.asString());
return psiAnnotation == null ? null : new JavaAnnotationImpl(psiAnnotation); return psiAnnotation == null ? null : new JavaAnnotationImpl(psiAnnotation);
} }
return null; return null;
} }
@NotNull
@Override
public Collection<JavaAnnotation> findExternalAnnotations(@NotNull JavaAnnotationOwner owner) {
if (owner instanceof JavaModifierListOwnerImpl) {
PsiModifierListOwner psiOwner = ((JavaModifierListOwnerImpl) owner).getPsi();
PsiAnnotation[] annotations = ExternalAnnotationsManager.getInstance(psiOwner.getProject()).findExternalAnnotations(psiOwner);
return annotations == null
? Collections.<JavaAnnotation>emptyList()
: JavaElementCollectionFromPsiArrayUtil.annotations(annotations);
}
return Collections.emptyList();
}
@Nullable
public static PsiAnnotation findExternalAnnotation(@NotNull PsiModifierListOwner owner, @NotNull FqName fqName) {
return ExternalAnnotationsManager.getInstance(owner.getProject()).findExternalAnnotation(owner, fqName.asString());
}
} }
@@ -16,7 +16,10 @@
package org.jetbrains.kotlin.load.java.structure.impl; package org.jetbrains.kotlin.load.java.structure.impl;
import com.intellij.psi.*; import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiJavaCodeReferenceElement;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.load.java.structure.JavaAnnotation; import org.jetbrains.kotlin.load.java.structure.JavaAnnotation;
@@ -35,13 +38,6 @@ public class JavaAnnotationImpl extends JavaElementImpl<PsiAnnotation> implement
super(psiAnnotation); super(psiAnnotation);
} }
@Override
@Nullable
public JavaAnnotationArgument findArgument(@NotNull Name name) {
PsiAnnotationMemberValue attribute = getPsi().findAttributeValue(name.asString());
return attribute == null ? null : JavaAnnotationArgumentImpl.Factory.create(attribute, name);
}
@Override @Override
@NotNull @NotNull
public Collection<JavaAnnotationArgument> getArguments() { public Collection<JavaAnnotationArgument> getArguments() {
@@ -74,7 +70,8 @@ public class JavaAnnotationImpl extends JavaElementImpl<PsiAnnotation> implement
PsiClass container = psiClass.getContainingClass(); PsiClass container = psiClass.getContainingClass();
if (container != null) { if (container != null) {
ClassId parentClassId = computeClassId(container); ClassId parentClassId = computeClassId(container);
return parentClassId == null ? null : parentClassId.createNestedClassId(Name.identifier(psiClass.getName())); String name = psiClass.getName();
return parentClassId == null || name == null ? null : parentClassId.createNestedClassId(Name.identifier(name));
} }
String fqName = psiClass.getQualifiedName(); String fqName = psiClass.getQualifiedName();
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.descriptors.Visibility;
import org.jetbrains.kotlin.load.java.structure.*; import org.jetbrains.kotlin.load.java.structure.*;
import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.name.SpecialNames;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@@ -55,7 +56,7 @@ public class JavaClassImpl extends JavaClassifierImpl<PsiClass> implements JavaC
@NotNull @NotNull
@Override @Override
public Name getName() { public Name getName() {
return Name.identifier(getPsi().getName()); return SpecialNames.safeIdentifier(getPsi().getName());
} }
@Override @Override
@@ -20,7 +20,6 @@ import com.intellij.psi.PsiAnnotationMethod;
import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiType; import com.intellij.psi.PsiType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.load.java.structure.JavaMethod; import org.jetbrains.kotlin.load.java.structure.JavaMethod;
import org.jetbrains.kotlin.load.java.structure.JavaType; import org.jetbrains.kotlin.load.java.structure.JavaType;
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter; import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter;
@@ -23,6 +23,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.load.java.structure.JavaClassifierType; import org.jetbrains.kotlin.load.java.structure.JavaClassifierType;
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter; import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter;
import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.name.SpecialNames;
import java.util.Collection; import java.util.Collection;
@@ -36,7 +37,7 @@ public class JavaTypeParameterImpl extends JavaClassifierImpl<PsiTypeParameter>
@NotNull @NotNull
@Override @Override
public Name getName() { public Name getName() {
return Name.identifier(getPsi().getName()); return SpecialNames.safeIdentifier(getPsi().getName());
} }
@Override @Override
@@ -22,9 +22,6 @@ import org.jetbrains.kotlin.load.java.structure.JavaAnnotation;
import org.jetbrains.kotlin.load.java.structure.JavaAnnotationOwner; import org.jetbrains.kotlin.load.java.structure.JavaAnnotationOwner;
import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.FqName;
import java.util.Collection;
import java.util.Collections;
public interface ExternalAnnotationResolver { public interface ExternalAnnotationResolver {
ExternalAnnotationResolver EMPTY = new ExternalAnnotationResolver() { ExternalAnnotationResolver EMPTY = new ExternalAnnotationResolver() {
@Nullable @Nullable
@@ -32,17 +29,8 @@ public interface ExternalAnnotationResolver {
public JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName) { public JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName) {
return null; return null;
} }
@NotNull
@Override
public Collection<JavaAnnotation> findExternalAnnotations(@NotNull JavaAnnotationOwner owner) {
return Collections.emptyList();
}
}; };
@Nullable @Nullable
JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName); JavaAnnotation findExternalAnnotation(@NotNull JavaAnnotationOwner owner, @NotNull FqName fqName);
@NotNull
Collection<JavaAnnotation> findExternalAnnotations(@NotNull JavaAnnotationOwner owner);
} }
@@ -19,14 +19,10 @@ package org.jetbrains.kotlin.load.java.structure;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.name.ClassId; import org.jetbrains.kotlin.name.ClassId;
import org.jetbrains.kotlin.name.Name;
import java.util.Collection; import java.util.Collection;
public interface JavaAnnotation extends JavaElement { public interface JavaAnnotation extends JavaElement {
@Nullable
JavaAnnotationArgument findArgument(@NotNull Name name);
@NotNull @NotNull
Collection<JavaAnnotationArgument> getArguments(); Collection<JavaAnnotationArgument> getArguments();
@@ -19,19 +19,12 @@ package org.jetbrains.kotlin.load.java.structure.reflect
import org.jetbrains.kotlin.load.java.structure.JavaAnnotation import org.jetbrains.kotlin.load.java.structure.JavaAnnotation
import org.jetbrains.kotlin.load.java.structure.JavaAnnotationArgument import org.jetbrains.kotlin.load.java.structure.JavaAnnotationArgument
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import java.lang.reflect.Method
class ReflectJavaAnnotation(val annotation: Annotation) : ReflectJavaElement(), JavaAnnotation { class ReflectJavaAnnotation(val annotation: Annotation) : ReflectJavaElement(), JavaAnnotation {
override fun findArgument(name: Name): JavaAnnotationArgument? {
return getArgumentValue(annotation.annotationClass.java.getDeclaredMethod(name.asString()))
}
override fun getArguments(): Collection<JavaAnnotationArgument> { override fun getArguments(): Collection<JavaAnnotationArgument> {
return annotation.annotationClass.java.declaredMethods.map { getArgumentValue(it) } return annotation.annotationClass.java.declaredMethods.map { method ->
} ReflectJavaAnnotationArgument.create(method.invoke(annotation), Name.identifier(method.name))
}
private fun getArgumentValue(argument: Method): ReflectJavaAnnotationArgument {
return argument.invoke(annotation).let { ReflectJavaAnnotationArgument.create(it, Name.identifier(argument.name)) }
} }
override fun resolve() = ReflectJavaClass(annotation.annotationClass.java) override fun resolve() = ReflectJavaClass(annotation.annotationClass.java)
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.load.java.structure.reflect package org.jetbrains.kotlin.load.java.structure.reflect
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.load.java.JavaVisibilities import org.jetbrains.kotlin.load.java.JavaVisibilities
import org.jetbrains.kotlin.load.java.structure.JavaModifierListOwner import org.jetbrains.kotlin.load.java.structure.JavaModifierListOwner
import java.lang.reflect.Modifier import java.lang.reflect.Modifier
@@ -28,7 +29,7 @@ interface ReflectJavaModifierListOwner : JavaModifierListOwner {
override fun isStatic() = Modifier.isStatic(modifiers) override fun isStatic() = Modifier.isStatic(modifiers)
override fun isFinal() = Modifier.isFinal(modifiers) override fun isFinal() = Modifier.isFinal(modifiers)
override fun getVisibility() = modifiers.let { modifiers -> override fun getVisibility(): Visibility = modifiers.let { modifiers ->
when { when {
Modifier.isPublic(modifiers) -> Visibilities.PUBLIC Modifier.isPublic(modifiers) -> Visibilities.PUBLIC
Modifier.isPrivate(modifiers) -> Visibilities.PRIVATE Modifier.isPrivate(modifiers) -> Visibilities.PRIVATE
@@ -36,6 +36,11 @@ public class SpecialNames {
return name != null && !name.isSpecial() ? name : SAFE_IDENTIFIER_FOR_NO_NAME; return name != null && !name.isSpecial() ? name : SAFE_IDENTIFIER_FOR_NO_NAME;
} }
@NotNull
public static Name safeIdentifier(@Nullable String name) {
return safeIdentifier(name == null ? null : Name.identifier(name));
}
public static boolean isSafeIdentifier(@NotNull Name name) { public static boolean isSafeIdentifier(@NotNull Name name) {
return !name.asString().isEmpty() && !name.isSpecial(); return !name.asString().isEmpty() && !name.isSpecial();
} }