[java] usages migrated to PsiType.annotate()

This commit is contained in:
Roman Shevchenko
2016-04-22 20:48:59 +02:00
parent 8627ab0832
commit 6ea3b7a197
18 changed files with 113 additions and 88 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -137,8 +137,8 @@ class ParameterObjectBuilder {
out.append(CodeStyleSettingsManager.getSettings(myProject).GENERATE_FINAL_PARAMETERS ? " final " : "");
final String parameterName = parameter.getName();
final PsiType type = field.getType();
final PsiType fieldType = parameter.isVarArgs() && type instanceof PsiArrayType ?
PsiEllipsisType.createEllipsis(((PsiArrayType)type).getComponentType(), PsiAnnotation.EMPTY_ARRAY) : type;
final PsiType fieldType = parameter.isVarArgs() && type instanceof PsiArrayType ?
new PsiEllipsisType(((PsiArrayType)type).getComponentType()) : type;
out.append(' ' + fieldType.getCanonicalText() + ' ' + parameterName);
if (iterator.hasNext()) {
out.append(", ");
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -376,11 +376,14 @@ public class GenericsUtil {
LOG.assertTrue(toPut == null || toPut.isValid(), toPut);
substitutor = substitutor.put(typeParameter, toPut);
}
final PsiAnnotation[] applicableAnnotations = classType.getApplicableAnnotations();
if (substitutor == PsiSubstitutor.EMPTY && !toExtend && applicableAnnotations.length == 0 && !(aClass instanceof PsiTypeParameter)) return classType;
PsiAnnotation[] applicableAnnotations = classType.getApplicableAnnotations();
if (substitutor == PsiSubstitutor.EMPTY && !toExtend && applicableAnnotations.length == 0 && !(aClass instanceof PsiTypeParameter)) {
return classType;
}
PsiManager manager = aClass.getManager();
PsiType result = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory()
.createType(aClass, substitutor, PsiUtil.getLanguageLevel(aClass), applicableAnnotations);
.createType(aClass, substitutor, PsiUtil.getLanguageLevel(aClass))
.annotate(TypeAnnotationProvider.Static.create(applicableAnnotations));
if (toExtend) result = PsiWildcardType.createExtends(manager, result);
return result;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -34,7 +34,7 @@ public interface JVMElementFactory {
*
* @param name the name of the class to create.
* @return the created class instance.
* @throws com.intellij.util.IncorrectOperationException
* @throws IncorrectOperationException
* if <code>name</code> is not a valid Java identifier.
*/
@NotNull
@@ -175,7 +175,7 @@ public interface JVMElementFactory {
*
* @param name the name of the annotation type to create.
* @return the created annotation type instance.
* @throws com.intellij.util.IncorrectOperationException if <code>name</code> is not a valid Java identifier.
* @throws IncorrectOperationException if <code>name</code> is not a valid Java identifier.
*/
@NotNull
PsiClass createAnnotationType(@NotNull @NonNls String name) throws IncorrectOperationException;
@@ -201,13 +201,20 @@ public interface JVMElementFactory {
@NotNull
PsiClassType createType(@NotNull PsiClass resolve, @NotNull PsiSubstitutor substitutor);
/*
additional languageLevel parameter to memorize language level for allowing/prohibiting boxing/unboxing
*/
/**
* Creates a class type for the specified class, using the specified substitutor
* to replace generic type parameters on the class.
*
* @param resolve the class for which the class type is created.
* @param substitutor the substitutor to use.
* @param languageLevel to memorize language level for allowing/prohibiting boxing/unboxing.
* @return the class type instance.
*/
@NotNull
PsiClassType createType(@NotNull PsiClass resolve, @NotNull PsiSubstitutor substitutor, @NotNull LanguageLevel languageLevel);
@NotNull
/** @deprecated use {@link PsiType#annotate(TypeAnnotationProvider)} (to be removed in IDEA 18) */
@SuppressWarnings("unused")
PsiClassType createType(@NotNull PsiClass resolve,
@NotNull PsiSubstitutor substitutor,
@NotNull LanguageLevel languageLevel,
@@ -55,6 +55,12 @@ public abstract class PsiClassType extends PsiType {
myLanguageLevel = languageLevel;
}
@NotNull
@Override
public PsiClassType annotate(@NotNull TypeAnnotationProvider provider) {
return (PsiClassType)super.annotate(provider);
}
/**
* Resolves the class reference and returns the resulting class.
*
@@ -178,12 +178,6 @@ public interface PsiElementFactory extends PsiJavaParserFacade, JVMElementFactor
@Override
@NotNull PsiClassType createType(@NotNull PsiClass resolve, @NotNull PsiSubstitutor substitutor, @Nullable LanguageLevel languageLevel);
@Override
@NotNull PsiClassType createType(@NotNull PsiClass resolve,
@NotNull PsiSubstitutor substitutor,
@Nullable LanguageLevel languageLevel,
@NotNull PsiAnnotation[] annotations);
/**
* Creates a class type for the specified reference pointing to a class.
*
@@ -41,7 +41,8 @@ public class PsiEllipsisType extends PsiArrayType {
return provider == getAnnotationProvider() ? this : new PsiEllipsisType(getComponentType(), provider);
}
@NotNull
/** @deprecated use {@link #annotate(TypeAnnotationProvider)} (to be removed in IDEA 18) */
@SuppressWarnings("unused")
public static PsiType createEllipsis(@NotNull PsiType componentType, @NotNull PsiAnnotation[] annotations) {
return new PsiEllipsisType(componentType, annotations);
}
@@ -235,11 +235,14 @@ public interface PsiJavaParserFacade {
/**
* Creates a Java type from the specified text.
*
* @param text the text of the type to create (a primitive type keyword).
* @param annotations array (possible empty) of annotations to annotate the created type.
* @param text the text of the type to create (a primitive type keyword).
* @return the created type instance.
* @throws IncorrectOperationException if some of the parameters are not valid.
*/
@NotNull
PsiType createPrimitiveTypeFromText(@NotNull String text) throws IncorrectOperationException;
/** @deprecated use {@link PsiType#annotate(TypeAnnotationProvider)} (to be removed in IDEA 18) */
@SuppressWarnings("unused")
PsiType createPrimitiveType(@NotNull String text, @NotNull PsiAnnotation[] annotations) throws IncorrectOperationException;
}
@@ -125,19 +125,18 @@ public class PsiPrimitiveType extends PsiType.Stub {
@Nullable
public static PsiPrimitiveType getUnboxedType(PsiType type) {
if (!(type instanceof PsiClassType)) return null;
assert type.isValid() : type;
LanguageLevel languageLevel = ((PsiClassType)type).getLanguageLevel();
if (!languageLevel.isAtLeast(LanguageLevel.JDK_1_5)) return null;
assert type.isValid() : type;
PsiClass psiClass = ((PsiClassType)type).resolve();
if (psiClass == null) return null;
PsiPrimitiveType unboxed = ourQNameToUnboxed.get(psiClass.getQualifiedName());
PsiAnnotation[] annotations = type.getAnnotations();
if (unboxed != null && annotations.length > 0) {
unboxed = new PsiPrimitiveType(unboxed.myName, annotations);
}
return unboxed;
if (unboxed == null) return null;
return unboxed.annotate(type.getAnnotationProvider());
}
public String getBoxedTypeName() {
@@ -152,31 +151,30 @@ public class PsiPrimitiveType extends PsiType.Stub {
* it was not possible to resolve the reference to the class.
*/
@Nullable
public PsiClassType getBoxedType(PsiElement context) {
public PsiClassType getBoxedType(@NotNull PsiElement context) {
PsiFile file = context.getContainingFile();
LanguageLevel languageLevel = PsiUtil.getLanguageLevel(file);
if (!languageLevel.isAtLeast(LanguageLevel.JDK_1_5)) return null;
String boxedQName = getBoxedTypeName();
//[ven]previous call returns null for NULL, VOID
if (boxedQName == null) return null;
JavaPsiFacade facade = JavaPsiFacade.getInstance(file.getProject());
PsiClass aClass = facade.findClass(boxedQName, file.getResolveScope());
if (aClass == null) return null;
PsiElementFactory factory = facade.getElementFactory();
return factory.createType(aClass, PsiSubstitutor.EMPTY, languageLevel, getAnnotations());
return factory.createType(aClass, PsiSubstitutor.EMPTY, languageLevel).annotate(getAnnotationProvider());
}
@Nullable
public PsiClassType getBoxedType(final PsiManager manager, final GlobalSearchScope resolveScope) {
final String boxedQName = getBoxedTypeName();
//[ven]previous call returns null for NULL, VOID
public PsiClassType getBoxedType(@NotNull PsiManager manager, @NotNull GlobalSearchScope resolveScope) {
String boxedQName = getBoxedTypeName();
if (boxedQName == null) return null;
final PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(boxedQName, resolveScope);
PsiClass aClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(boxedQName, resolveScope);
if (aClass == null) return null;
return JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().createType(aClass);
}
@@ -56,13 +56,7 @@ public abstract class PsiType implements PsiAnnotationOwner {
* Constructs a PsiType with given annotations
*/
protected PsiType(@NotNull final PsiAnnotation[] annotations) {
this(annotations.length == 0 ? TypeAnnotationProvider.EMPTY : new TypeAnnotationProvider() {
@NotNull
@Override
public PsiAnnotation[] getAnnotations() {
return annotations;
}
});
this(TypeAnnotationProvider.Static.create(annotations));
}
/**
@@ -85,10 +79,8 @@ public abstract class PsiType implements PsiAnnotationOwner {
return new PsiArrayType(this);
}
/**
* Creates array type with this type as a component.
*/
@NotNull
/** @deprecated use {@link #annotate(TypeAnnotationProvider)} (to be removed in IDEA 18) */
@SuppressWarnings("unused")
public PsiArrayType createArrayType(@NotNull PsiAnnotation... annotations) {
return new PsiArrayType(this, annotations);
}
@@ -75,15 +75,10 @@ public class PsiWildcardType extends PsiType.Stub {
return new PsiWildcardType(manager, false, bound);
}
@NotNull
/** @deprecated use {@link #annotate(TypeAnnotationProvider)} (to be removed in IDEA 18) */
@SuppressWarnings("unused")
public PsiWildcardType annotate(@NotNull final PsiAnnotation[] annotations) {
return annotations.length == 0 ? this : new PsiWildcardType(this, new TypeAnnotationProvider() {
@NotNull
@Override
public PsiAnnotation[] getAnnotations() {
return annotations;
}
});
return annotations.length == 0 ? this : new PsiWildcardType(this, TypeAnnotationProvider.Static.create(annotations));
}
@NotNull
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -36,4 +36,24 @@ public interface TypeAnnotationProvider {
@NotNull
PsiAnnotation[] getAnnotations();
}
class Static implements TypeAnnotationProvider {
private final PsiAnnotation[] myAnnotations;
private Static(PsiAnnotation[] annotations) {
myAnnotations = annotations;
}
@NotNull
@Override
public PsiAnnotation[] getAnnotations() {
return myAnnotations;
}
@NotNull
public static TypeAnnotationProvider create(@NotNull PsiAnnotation[] annotations) {
return annotations.length == 0 ? EMPTY : new Static(annotations);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -383,13 +383,18 @@ public class PsiJavaParserFacadeImpl implements PsiJavaParserFacade {
}
@NotNull
@Override
public PsiType createPrimitiveType(@NotNull final String text, @NotNull final PsiAnnotation[] annotations) throws IncorrectOperationException {
final PsiPrimitiveType primitiveType = getPrimitiveType(text);
public PsiType createPrimitiveTypeFromText(@NotNull String text) throws IncorrectOperationException {
PsiPrimitiveType primitiveType = getPrimitiveType(text);
if (primitiveType == null) {
throw new IncorrectOperationException("Incorrect primitive type '" + text + "'");
}
return annotations.length == 0 ? primitiveType : new PsiPrimitiveType(text, annotations);
return primitiveType;
}
@NotNull
@Override
public PsiType createPrimitiveType(@NotNull String text, @NotNull PsiAnnotation[] annotations) throws IncorrectOperationException {
return createPrimitiveTypeFromText(text).annotate(TypeAnnotationProvider.Static.create(annotations));
}
public static PsiPrimitiveType getPrimitiveType(final String text) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -48,7 +48,7 @@ public class JavaSharedImplUtil {
List<PsiAnnotation[]> allAnnotations = collectAnnotations(anchor, stopAt);
if (allAnnotations == null) return null;
for (PsiAnnotation[] annotations : allAnnotations) {
type = type.createArrayType(annotations);
type = type.createArrayType().annotate(TypeAnnotationProvider.Static.create(annotations));
}
return type;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -73,20 +73,22 @@ public class PsiNewExpressionImpl extends ExpressionPsiElement implements PsiNew
else if (ElementType.PRIMITIVE_TYPE_BIT_SET.contains(elementType)) {
assert type == null : this;
PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
type = factory.createPrimitiveType(child.getText(), ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true));
PsiAnnotation[] copy = ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true);
type = factory.createPrimitiveTypeFromText(child.getText()).annotate(TypeAnnotationProvider.Static.create(copy));
if (stop) return type;
}
else if (elementType == JavaTokenType.LBRACKET) {
assert type != null : this;
type = type.createArrayType(ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true));
PsiAnnotation[] copy = ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true);
type = type.createArrayType().annotate(TypeAnnotationProvider.Static.create(copy));
if (stop) return type;
}
else if (elementType == JavaElementType.ANONYMOUS_CLASS) {
PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
PsiClass aClass = (PsiClass)child.getPsi();
PsiSubstitutor substitutor = aClass instanceof PsiTypeParameter ? PsiSubstitutor.EMPTY : factory.createRawSubstitutor(aClass);
type = factory.createType(aClass, substitutor, PsiUtil.getLanguageLevel(aClass),
ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true));
PsiAnnotation[] copy = ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true);
type = factory.createType(aClass, substitutor, PsiUtil.getLanguageLevel(aClass)).annotate(TypeAnnotationProvider.Static.create(copy));
if (stop) return type;
}
}
@@ -50,10 +50,10 @@ public class CanonicalTypes {
}
private abstract static class AnnotatedType extends Type {
protected final PsiAnnotation[] myAnnotations;
protected final TypeAnnotationProvider myProvider;
protected AnnotatedType(@NotNull PsiAnnotation[] annotations) {
myAnnotations = annotations;
public AnnotatedType(@NotNull TypeAnnotationProvider provider) {
myProvider = TypeAnnotationProvider.Static.create(provider.getAnnotations());
}
}
@@ -61,14 +61,14 @@ public class CanonicalTypes {
private final PsiPrimitiveType myType;
private Primitive(@NotNull PsiPrimitiveType type) {
super(type.getAnnotations());
super(type.getAnnotationProvider());
myType = type;
}
@NotNull
@Override
public PsiType getType(PsiElement context, PsiManager manager) {
return myAnnotations.length == 0 ? myType : new PsiPrimitiveType(myType.getCanonicalText(false), myAnnotations);
return myType.annotate(myProvider);
}
@Override
@@ -81,14 +81,14 @@ public class CanonicalTypes {
protected final Type myComponentType;
private Array(@NotNull PsiType original, @NotNull Type componentType) {
super(original.getAnnotations());
super(original.getAnnotationProvider());
myComponentType = componentType;
}
@NotNull
@Override
public PsiType getType(PsiElement context, PsiManager manager) throws IncorrectOperationException {
return myComponentType.getType(context, manager).createArrayType(myAnnotations);
return myComponentType.getType(context, manager).createArrayType().annotate(myProvider);
}
@Override
@@ -115,7 +115,7 @@ public class CanonicalTypes {
@NotNull
@Override
public PsiType getType(PsiElement context, PsiManager manager) throws IncorrectOperationException {
return new PsiEllipsisType(myComponentType.getType(context, manager), myAnnotations);
return new PsiEllipsisType(myComponentType.getType(context, manager)).annotate(myProvider);
}
@Override
@@ -129,7 +129,7 @@ public class CanonicalTypes {
private final Type myBound;
private WildcardType(@NotNull PsiType original, boolean isExtending, @Nullable Type bound) {
super(original.getAnnotations());
super(original.getAnnotationProvider());
myIsExtending = isExtending;
myBound = bound;
}
@@ -147,7 +147,7 @@ public class CanonicalTypes {
else {
type = PsiWildcardType.createSuper(manager, myBound.getType(context, manager));
}
return type.annotate(myAnnotations);
return type.annotate(myProvider);
}
@Override
@@ -205,7 +205,7 @@ public class CanonicalTypes {
private final Map<String, Type> mySubstitutor;
private ClassType(@NotNull PsiType original, @NotNull String classQName, @NotNull Map<String, Type> substitutor) {
super(original.getAnnotations());
super(original.getAnnotationProvider());
myPresentableText = original.getPresentableText();
myClassQName = classQName;
mySubstitutor = substitutor;
@@ -227,7 +227,7 @@ public class CanonicalTypes {
Type substitute = mySubstitutor.get(typeParameter.getName());
substitutionMap.put(typeParameter, substitute != null ? substitute.getType(context, manager) : null);
}
return factory.createType(aClass, factory.createSubstitutor(substitutionMap), null, myAnnotations);
return factory.createType(aClass, factory.createSubstitutor(substitutionMap), null).annotate(myProvider);
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -20,8 +20,6 @@ import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.Query;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.InspectionGadgetsFix;
import org.jetbrains.annotations.NotNull;
@@ -90,7 +88,7 @@ public class ConvertToVarargsMethodFix extends InspectionGadgetsFix {
final PsiArrayType arrayType = (PsiArrayType)type;
final PsiType componentType = arrayType.getComponentType();
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(method.getProject());
final PsiType ellipsisType = PsiEllipsisType.createEllipsis(componentType, type.getAnnotations());
final PsiType ellipsisType = new PsiEllipsisType(componentType, TypeAnnotationProvider.Static.create(type.getAnnotations()));
final PsiTypeElement newTypeElement = factory.createTypeElement(ellipsisType);
final PsiTypeElement typeElement = lastParameter.getTypeElement();
if (typeElement != null) {
@@ -48,7 +48,7 @@ public class MakeMethodVarargsIntention extends Intention {
final PsiElementFactory factory = JavaPsiFacade.getInstance(element.getProject()).getElementFactory();
final PsiTypeElement typeElement = lastParameter.getTypeElement();
LOG.assertTrue(typeElement != null);
final PsiType ellipsisType = PsiEllipsisType.createEllipsis(((PsiArrayType)type).getComponentType(), type.getAnnotations());
final PsiType ellipsisType = new PsiEllipsisType(((PsiArrayType)type).getComponentType(), TypeAnnotationProvider.Static.create(type.getAnnotations()));
typeElement.replace(factory.createTypeElement(ellipsisType));
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -1131,6 +1131,7 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
return JavaPsiFacade.getElementFactory(myProject).createType(resolve, substitutor, languageLevel);
}
@SuppressWarnings("deprecation")
@NotNull
@Override
public PsiClassType createType(@NotNull PsiClass resolve,