Extract interfaces out of Java*Type

This commit is contained in:
Alexander Udalov
2013-08-19 17:51:26 +04:00
parent 08bf7f8eee
commit f6b43bb7b2
12 changed files with 265 additions and 115 deletions
@@ -19,19 +19,11 @@ package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiArrayType;
import org.jetbrains.annotations.NotNull;
public class JavaArrayType extends JavaTypeImpl {
public JavaArrayType(@NotNull PsiArrayType psiArrayType) {
super(psiArrayType);
}
public interface JavaArrayType extends JavaType {
@NotNull
@Override
public PsiArrayType getPsi() {
return (PsiArrayType) super.getPsi();
}
PsiArrayType getPsi();
@NotNull
public JavaType getComponentType() {
return JavaTypeImpl.create(getPsi().getComponentType());
}
JavaType getComponentType();
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiArrayType;
import org.jetbrains.annotations.NotNull;
public class JavaArrayTypeImpl extends JavaTypeImpl implements JavaArrayType {
public JavaArrayTypeImpl(@NotNull PsiArrayType psiArrayType) {
super(psiArrayType);
}
@NotNull
@Override
public PsiArrayType getPsi() {
return (PsiArrayType) super.getPsi();
}
@Override
@NotNull
public JavaType getComponentType() {
return JavaTypeImpl.create(getPsi().getComponentType());
}
}
@@ -171,7 +171,7 @@ public class JavaClass extends JavaClassifierImpl
@NotNull
public JavaClassifierType getDefaultType() {
return new JavaClassifierType(JavaPsiFacade.getElementFactory(getPsi().getProject()).createType(getPsi()));
return new JavaClassifierTypeImpl(JavaPsiFacade.getElementFactory(getPsi().getProject()).createType(getPsi()));
}
@NotNull
@@ -16,90 +16,31 @@
package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.jet.lang.resolve.java.structure.JavaElementCollectionFromPsiArrayUtil.types;
public class JavaClassifierType extends JavaTypeImpl {
private static class ResolutionResult {
private final JavaClassifier classifier;
private final JavaTypeSubstitutor substitutor;
private ResolutionResult(@Nullable JavaClassifier classifier, @NotNull JavaTypeSubstitutor substitutor) {
this.classifier = classifier;
this.substitutor = substitutor;
}
}
private ResolutionResult resolutionResult;
public JavaClassifierType(@NotNull PsiClassType psiClassType) {
super(psiClassType);
}
public interface JavaClassifierType extends JavaType {
@NotNull
@Override
public PsiClassType getPsi() {
return (PsiClassType) super.getPsi();
}
PsiClassType getPsi();
@Nullable
public JavaClassifier getClassifier() {
resolve();
return resolutionResult.classifier;
}
JavaClassifier getClassifier();
@NotNull
public JavaTypeSubstitutor getSubstitutor() {
resolve();
return resolutionResult.substitutor;
}
private void resolve() {
if (resolutionResult == null) {
PsiClassType.ClassResolveResult result = getPsi().resolveGenerics();
PsiClass psiClass = result.getElement();
resolutionResult = new ResolutionResult(
psiClass == null ? null : JavaClassifierImpl.create(psiClass),
new JavaTypeSubstitutor(result.getSubstitutor())
);
}
}
JavaTypeSubstitutor getSubstitutor();
@NotNull
public Collection<JavaClassifierType> getSupertypes() {
PsiType[] psiTypes = getPsi().getSuperTypes();
if (psiTypes.length == 0) return Collections.emptyList();
List<JavaClassifierType> result = new ArrayList<JavaClassifierType>(psiTypes.length);
for (PsiType psiType : psiTypes) {
if (!(psiType instanceof PsiClassType)) {
throw new IllegalStateException("Supertype should be a class: " + psiType + ", type: " + getPsi());
}
result.add(new JavaClassifierType((PsiClassType) psiType));
}
return result;
}
Collection<JavaClassifierType> getSupertypes();
@NotNull
public String getPresentableText() {
return getPsi().getPresentableText();
}
String getPresentableText();
public boolean isRaw() {
return getPsi().isRaw();
}
boolean isRaw();
@NotNull
public Collection<JavaType> getTypeArguments() {
return types(getPsi().getParameters());
}
Collection<JavaType> getTypeArguments();
}
@@ -0,0 +1,111 @@
/*
* Copyright 2010-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.jet.lang.resolve.java.structure.JavaElementCollectionFromPsiArrayUtil.types;
public class JavaClassifierTypeImpl extends JavaTypeImpl implements JavaClassifierType {
private static class ResolutionResult {
private final JavaClassifier classifier;
private final JavaTypeSubstitutor substitutor;
private ResolutionResult(@Nullable JavaClassifier classifier, @NotNull JavaTypeSubstitutor substitutor) {
this.classifier = classifier;
this.substitutor = substitutor;
}
}
private ResolutionResult resolutionResult;
public JavaClassifierTypeImpl(@NotNull PsiClassType psiClassType) {
super(psiClassType);
}
@NotNull
@Override
public PsiClassType getPsi() {
return (PsiClassType) super.getPsi();
}
@Override
@Nullable
public JavaClassifier getClassifier() {
resolve();
return resolutionResult.classifier;
}
@Override
@NotNull
public JavaTypeSubstitutor getSubstitutor() {
resolve();
return resolutionResult.substitutor;
}
private void resolve() {
if (resolutionResult == null) {
PsiClassType.ClassResolveResult result = getPsi().resolveGenerics();
PsiClass psiClass = result.getElement();
resolutionResult = new ResolutionResult(
psiClass == null ? null : JavaClassifierImpl.create(psiClass),
new JavaTypeSubstitutor(result.getSubstitutor())
);
}
}
@Override
@NotNull
public Collection<JavaClassifierType> getSupertypes() {
PsiType[] psiTypes = getPsi().getSuperTypes();
if (psiTypes.length == 0) return Collections.emptyList();
List<JavaClassifierType> result = new ArrayList<JavaClassifierType>(psiTypes.length);
for (PsiType psiType : psiTypes) {
if (!(psiType instanceof PsiClassType)) {
throw new IllegalStateException("Supertype should be a class: " + psiType + ", type: " + getPsi());
}
result.add(new JavaClassifierTypeImpl((PsiClassType) psiType));
}
return result;
}
@Override
@NotNull
public String getPresentableText() {
return getPsi().getPresentableText();
}
@Override
public boolean isRaw() {
return getPsi().isRaw();
}
@Override
@NotNull
public Collection<JavaType> getTypeArguments() {
return types(getPsi().getParameters());
}
}
@@ -104,7 +104,7 @@ public class JavaElementCollectionFromPsiArrayUtil {
if (classTypes.length == 0) return Collections.emptyList();
List<JavaClassifierType> result = new ArrayList<JavaClassifierType>(classTypes.length);
for (PsiClassType psiClassType : classTypes) {
result.add(new JavaClassifierType(psiClassType));
result.add(new JavaClassifierTypeImpl(psiClassType));
}
return result;
}
@@ -22,6 +22,6 @@ public class JavaElementFactoryImpl extends JavaElementFactory {
@NotNull
@Override
public JavaArrayType createArrayType(@NotNull JavaType elementType) {
return new JavaArrayType(elementType.getPsi().createArrayType());
return new JavaArrayTypeImpl(elementType.getPsi().createArrayType());
}
}
@@ -19,19 +19,11 @@ package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiPrimitiveType;
import org.jetbrains.annotations.NotNull;
public class JavaPrimitiveType extends JavaTypeImpl {
public JavaPrimitiveType(@NotNull PsiPrimitiveType psiPrimitiveType) {
super(psiPrimitiveType);
}
public interface JavaPrimitiveType extends JavaType {
@NotNull
@Override
public PsiPrimitiveType getPsi() {
return (PsiPrimitiveType) super.getPsi();
}
PsiPrimitiveType getPsi();
@NotNull
public String getCanonicalText() {
return getPsi().getCanonicalText();
}
String getCanonicalText();
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiPrimitiveType;
import org.jetbrains.annotations.NotNull;
public class JavaPrimitiveTypeImpl extends JavaTypeImpl implements JavaPrimitiveType {
public JavaPrimitiveTypeImpl(@NotNull PsiPrimitiveType psiPrimitiveType) {
super(psiPrimitiveType);
}
@NotNull
@Override
public PsiPrimitiveType getPsi() {
return (PsiPrimitiveType) super.getPsi();
}
@Override
@NotNull
public String getCanonicalText() {
return getPsi().getCanonicalText();
}
}
@@ -45,25 +45,25 @@ public abstract class JavaTypeImpl implements JavaType {
@Nullable
@Override
public JavaType visitPrimitiveType(PsiPrimitiveType primitiveType) {
return new JavaPrimitiveType(primitiveType);
return new JavaPrimitiveTypeImpl(primitiveType);
}
@Nullable
@Override
public JavaType visitArrayType(PsiArrayType arrayType) {
return new JavaArrayType(arrayType);
return new JavaArrayTypeImpl(arrayType);
}
@Nullable
@Override
public JavaType visitClassType(PsiClassType classType) {
return new JavaClassifierType(classType);
return new JavaClassifierTypeImpl(classType);
}
@Nullable
@Override
public JavaType visitWildcardType(PsiWildcardType wildcardType) {
return new JavaWildcardType(wildcardType);
return new JavaWildcardTypeImpl(wildcardType);
}
});
}
@@ -16,34 +16,20 @@
package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiWildcardType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class JavaWildcardType extends JavaTypeImpl {
public JavaWildcardType(@NotNull PsiWildcardType psiWildcardType) {
super(psiWildcardType);
}
public interface JavaWildcardType extends JavaType {
@NotNull
@Override
public PsiWildcardType getPsi() {
return (PsiWildcardType) super.getPsi();
}
PsiWildcardType getPsi();
@Nullable
public JavaType getBound() {
PsiType bound = getPsi().getBound();
return bound == null ? null : JavaTypeImpl.create(bound);
}
JavaType getBound();
public boolean isExtends() {
return getPsi().isExtends();
}
boolean isExtends();
@NotNull
public JavaTypeProvider getTypeProvider() {
return new JavaTypeProvider(getPsi().getManager());
}
JavaTypeProvider getTypeProvider();
}
@@ -0,0 +1,52 @@
/*
* Copyright 2010-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiWildcardType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class JavaWildcardTypeImpl extends JavaTypeImpl implements JavaWildcardType {
public JavaWildcardTypeImpl(@NotNull PsiWildcardType psiWildcardType) {
super(psiWildcardType);
}
@NotNull
@Override
public PsiWildcardType getPsi() {
return (PsiWildcardType) super.getPsi();
}
@Override
@Nullable
public JavaType getBound() {
PsiType bound = getPsi().getBound();
return bound == null ? null : JavaTypeImpl.create(bound);
}
@Override
public boolean isExtends() {
return getPsi().isExtends();
}
@Override
@NotNull
public JavaTypeProvider getTypeProvider() {
return new JavaTypeProvider(getPsi().getManager());
}
}