Get rid of PsiMemberWrapper

Continue migration, use the new Java* classes instead everywhere
This commit is contained in:
Alexander Udalov
2013-08-23 19:01:29 +04:00
parent 245b9d1ab1
commit 78f9cb7203
25 changed files with 476 additions and 522 deletions
@@ -22,8 +22,12 @@ import com.intellij.psi.impl.compiled.ClsClassImpl;
import com.intellij.psi.util.PsiFormatUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassOrNamespaceDescriptor;
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClass;
import org.jetbrains.jet.lang.resolve.java.structure.JavaMember;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
@@ -72,22 +76,6 @@ public final class DescriptorResolverUtils {
return Collections.emptyList();
}
public static Visibility resolveVisibility(@NotNull PsiModifierListOwner modifierListOwner) {
if (modifierListOwner.hasModifierProperty(PsiModifier.PUBLIC)) {
return Visibilities.PUBLIC;
}
if (modifierListOwner.hasModifierProperty(PsiModifier.PRIVATE)) {
return Visibilities.PRIVATE;
}
if (modifierListOwner.hasModifierProperty(PsiModifier.PROTECTED)) {
if (modifierListOwner.hasModifierProperty(PsiModifier.STATIC)) {
return JavaVisibilities.PROTECTED_STATIC_VISIBILITY;
}
return JavaVisibilities.PROTECTED_AND_PACKAGE;
}
return JavaVisibilities.PACKAGE_VISIBILITY;
}
@Nullable
public static ValueParameterDescriptor getValueParameterDescriptorForAnnotationParameter(
Name argumentName,
@@ -124,11 +112,8 @@ public final class DescriptorResolverUtils {
"valueOf(java.lang.String)".equals(signature);
}
public static boolean isCorrectOwnerForEnumMember(
@NotNull ClassOrNamespaceDescriptor ownerDescriptor,
@NotNull PsiMember member
) {
return isEnumClassObject(ownerDescriptor) == shouldBeInEnumClassObject(member);
public static boolean isCorrectOwnerForEnumMember(@NotNull ClassOrNamespaceDescriptor ownerDescriptor, @NotNull JavaMember member) {
return isEnumClassObject(ownerDescriptor) == shouldBeInEnumClassObject(member.getPsi());
}
public static boolean isObjectMethodInInterface(@NotNull PsiMember member) {
@@ -23,25 +23,24 @@ import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiFieldWrapper;
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
import org.jetbrains.jet.lang.types.JetType;
import java.util.HashMap;
public class AlternativeFieldSignatureData extends ElementAlternativeSignatureData {
private JetType altReturnType;
public AlternativeFieldSignatureData(@NotNull PsiFieldWrapper field, @NotNull JetType originalReturnType, boolean isVar) {
String signature = field.getSignatureAnnotation().signature();
public AlternativeFieldSignatureData(@NotNull JavaField field, @NotNull JetType originalReturnType, boolean isVar) {
String signature = SignaturesUtil.getKotlinSignature(field);
if (signature.isEmpty()) {
if (signature == null) {
setAnnotated(false);
return;
}
setAnnotated(true);
Project project = field.getPsiMember().getProject();
Project project = field.getPsi().getProject();
JetProperty altPropertyDeclaration = JetPsiFactory.createProperty(project, signature);
try {
@@ -61,10 +60,10 @@ public class AlternativeFieldSignatureData extends ElementAlternativeSignatureDa
return altReturnType;
}
private static void checkFieldAnnotation(JetProperty altProperty, PsiFieldWrapper fieldWrapper, boolean isVar) {
if (!ComparatorUtil.equalsNullable(fieldWrapper.getName(), altProperty.getName())) {
private static void checkFieldAnnotation(@NotNull JetProperty altProperty, @NotNull JavaField field, boolean isVar) {
if (!ComparatorUtil.equalsNullable(field.getName().asString(), altProperty.getName())) {
throw new AlternativeSignatureMismatchException("Field name mismatch, original: %s, alternative: %s",
fieldWrapper.getName(), altProperty.getName());
field.getName().asString(), altProperty.getName());
}
if (altProperty.getTypeRef() == null) {
@@ -22,12 +22,12 @@ import com.intellij.util.containers.ComparatorUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.ValueParameterDescriptorImpl;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMethodWrapper;
import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
@@ -53,21 +53,22 @@ public class AlternativeMethodSignatureData extends ElementAlternativeSignatureD
private Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> originalToAltTypeParameters;
public AlternativeMethodSignatureData(
@NotNull PsiMethodWrapper method,
@NotNull JavaMethod method,
@NotNull JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors,
@Nullable JetType originalReturnType,
@NotNull List<TypeParameterDescriptor> methodTypeParameters,
boolean hasSuperMethods
) {
String signature = method.getSignatureAnnotation().signature();
if (signature.isEmpty()) {
String signature = SignaturesUtil.getKotlinSignature(method);
if (signature == null) {
setAnnotated(false);
altFunDeclaration = null;
return;
}
setAnnotated(true);
Project project = method.getPsiMethod().getProject();
Project project = method.getPsi().getProject();
altFunDeclaration = JetPsiFactory.createFunction(project, signature);
originalToAltTypeParameters = SignaturesUtil.recreateTypeParametersAndReturnMapping(methodTypeParameters, null);
@@ -287,10 +288,10 @@ public class AlternativeMethodSignatureData extends ElementAlternativeSignatureD
return null;
}
private static void checkEqualFunctionNames(PsiNamedElement namedElement, PsiMethodWrapper method) {
if (!ComparatorUtil.equalsNullable(method.getName(), namedElement.getName())) {
private static void checkEqualFunctionNames(@NotNull PsiNamedElement namedElement, @NotNull JavaMethod method) {
if (!ComparatorUtil.equalsNullable(method.getName().asString(), namedElement.getName())) {
throw new AlternativeSignatureMismatchException("Function names mismatch, original: %s, alternative: %s",
method.getName(), namedElement.getName());
method.getName().asString(), namedElement.getName());
}
}
}
@@ -1,72 +0,0 @@
/*
* 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.kotlinSignature;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiAnnotationMemberValue;
import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.PsiMember;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames;
import org.jetbrains.jet.lang.resolve.java.resolver.JavaAnnotationResolver;
public class KotlinSignatureAnnotation {
private static final KotlinSignatureAnnotation NULL_ANNOTATION = new KotlinSignatureAnnotation(null);
static {
NULL_ANNOTATION.computeSignature();
}
@Nullable
private final PsiAnnotation psiAnnotation;
private String signature;
private KotlinSignatureAnnotation(@Nullable PsiAnnotation psiAnnotation) {
this.psiAnnotation = psiAnnotation;
}
@NotNull
public static KotlinSignatureAnnotation get(@NotNull PsiMember member) {
PsiAnnotation annotation = JavaAnnotationResolver.findAnnotationWithExternal(member, JvmAnnotationNames.KOTLIN_SIGNATURE);
return annotation != null ? new KotlinSignatureAnnotation(annotation) : NULL_ANNOTATION;
}
@NotNull
private String computeSignature() {
if (psiAnnotation != null) {
PsiAnnotationMemberValue attribute = psiAnnotation.findAttributeValue(JvmAnnotationNames.KOTLIN_SIGNATURE_VALUE_FIELD_NAME);
if (attribute instanceof PsiLiteralExpression) {
Object value = ((PsiLiteralExpression) attribute).getValue();
if (value instanceof String) {
return StringUtil.unescapeStringCharacters((String) value);
}
}
}
return "";
}
@NotNull
public String signature() {
if (signature == null) {
signature = computeSignature();
}
return signature;
}
}
@@ -35,9 +35,12 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.*;
import org.jetbrains.jet.lang.resolve.java.DescriptorResolverUtils;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.java.JetClsMethod;
import org.jetbrains.jet.lang.resolve.java.TypeUsage;
import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMap;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMethodWrapper;
import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -68,7 +71,7 @@ public class SignaturesPropagationData {
@NotNull JetType autoReturnType, // type built by JavaTypeTransformer from Java signature and @NotNull annotations
@NotNull JavaDescriptorResolver.ValueParameterDescriptors autoValueParameters, // descriptors built by parameters resolver
@NotNull List<TypeParameterDescriptor> autoTypeParameters, // descriptors built by signature resolver
@NotNull PsiMethodWrapper method,
@NotNull JavaMethod method,
@NotNull BindingTrace trace
) {
this.containingClass = containingClass;
@@ -206,7 +209,7 @@ public class SignaturesPropagationData {
}
private static List<FunctionDescriptor> getSuperFunctionsForMethod(
@NotNull PsiMethodWrapper method,
@NotNull JavaMethod method,
@NotNull BindingTrace trace,
@NotNull ClassDescriptor containingClass
) {
@@ -217,7 +220,7 @@ public class SignaturesPropagationData {
Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> superclassToFunctions =
getSuperclassToFunctionsMultimap(method, trace.getBindingContext(), containingClass);
for (PsiMethod superMethod : PropagationHeuristics.getSuperMethods(method.getPsiMethod())) {
for (PsiMethod superMethod : PropagationHeuristics.getSuperMethods(method.getPsi())) {
PsiClass psiClass = superMethod.getContainingClass();
assert psiClass != null;
String classFqNameString = psiClass.getQualifiedName();
@@ -237,7 +240,7 @@ public class SignaturesPropagationData {
if (superFun == null) {
// Super methods which are Object methods in interfaces are not loaded by JDR.
if (!DescriptorResolverUtils.isObjectMethodInInterface(superMethod)) {
reportCantFindSuperFunction(method);
reportCantFindSuperFunction(method.getPsi());
}
continue;
}
@@ -262,14 +265,14 @@ public class SignaturesPropagationData {
@NotNull
private static Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> getSuperclassToFunctionsMultimap(
@NotNull PsiMethodWrapper method,
@NotNull JavaMethod method,
@NotNull BindingContext bindingContext,
@NotNull ClassDescriptor containingClass
) {
Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> result = HashMultimap.create();
Name functionName = Name.identifier(method.getName());
int parameterCount = method.getParameters().size();
Name functionName = method.getName();
int parameterCount = method.getValueParameters().size();
for (JetType supertype : TypeUtils.getAllSupertypes(containingClass.getDefaultType())) {
ClassifierDescriptor klass = supertype.getConstructor().getDeclarationDescriptor();
@@ -690,9 +693,8 @@ public class SignaturesPropagationData {
return builtIns.isArray(type) || builtIns.isPrimitiveArray(type);
}
private static void reportCantFindSuperFunction(PsiMethodWrapper method) {
String errorMessage = "Can't find super function for " + method.getPsiMethod() +
" defined in " + method.getPsiMethod().getContainingClass();
private static void reportCantFindSuperFunction(@NotNull PsiMethod psiMethod) {
String errorMessage = "Can't find super function for " + psiMethod + " defined in " + psiMethod.getContainingClass();
if (SystemInfo.isMac) {
LOG.error("Remove duplicates from your JDK definition\n" + errorMessage);
}
@@ -17,11 +17,18 @@
package org.jetbrains.jet.lang.resolve.java.kotlinSignature;
import com.google.common.collect.Maps;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiAnnotationMemberValue;
import com.intellij.psi.PsiLiteralExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames;
import org.jetbrains.jet.lang.resolve.java.resolver.JavaAnnotationResolver;
import org.jetbrains.jet.lang.resolve.java.structure.JavaMember;
import org.jetbrains.jet.lang.types.TypeConstructor;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
@@ -30,6 +37,9 @@ import java.util.List;
import java.util.Map;
public class SignaturesUtil {
private SignaturesUtil() {
}
public static Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> recreateTypeParametersAndReturnMapping(
@NotNull List<TypeParameterDescriptor> originalParameters,
@Nullable DeclarationDescriptor newOwner
@@ -60,6 +70,20 @@ public class SignaturesUtil {
return TypeSubstitutor.create(typeSubstitutionContext);
}
private SignaturesUtil() {
@Nullable
public static String getKotlinSignature(@NotNull JavaMember member) {
PsiAnnotation annotation = JavaAnnotationResolver.findAnnotationWithExternal(member.getPsi(), JvmAnnotationNames.KOTLIN_SIGNATURE);
if (annotation != null) {
PsiAnnotationMemberValue attribute = annotation.findAttributeValue(JvmAnnotationNames.KOTLIN_SIGNATURE_VALUE_FIELD_NAME);
if (attribute instanceof PsiLiteralExpression) {
Object value = ((PsiLiteralExpression) attribute).getValue();
if (value instanceof String) {
return StringUtil.unescapeStringCharacters((String) value);
}
}
}
return null;
}
}
@@ -18,20 +18,14 @@ package org.jetbrains.jet.lang.resolve.java.provider;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMember;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.Visibilities;
import org.jetbrains.jet.lang.resolve.java.DescriptorResolverUtils;
import org.jetbrains.jet.lang.resolve.java.JetJavaMirrorMarker;
import org.jetbrains.jet.lang.resolve.java.sam.SingleAbstractMethodUtils;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClass;
import org.jetbrains.jet.lang.resolve.java.structure.JavaPackage;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiFieldWrapper;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMemberWrapper;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMethodWrapper;
import org.jetbrains.jet.lang.resolve.java.structure.*;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.Collection;
@@ -65,8 +59,8 @@ public final class MembersCache {
return r;
}
private void addTask(@NotNull PsiMember member, @NotNull RunOnce task) {
addTask(member.getName(), task);
private void addTask(@NotNull JavaNamedElement member, @NotNull RunOnce task) {
addTask(member.getName().asString(), task);
}
private void addTask(@Nullable String name, @NotNull RunOnce task) {
@@ -123,29 +117,27 @@ public final class MembersCache {
private void process() {
for (JavaClass javaClass : javaClasses) {
PsiClass psiClass = javaClass.getPsi();
if (!(psiClass instanceof JetJavaMirrorMarker)) { // to filter out JetLightClasses
if (SingleAbstractMethodUtils.isSamInterface(psiClass)) {
processSamInterface(psiClass);
if (!(javaClass.getPsi() instanceof JetJavaMirrorMarker)) { // to filter out JetLightClasses
if (SingleAbstractMethodUtils.isSamInterface(javaClass)) {
processSamInterface(javaClass);
}
}
}
}
private void processSamInterface(@NotNull PsiClass psiClass) {
NamedMembers namedMembers = getOrCreateEmpty(Name.identifier(psiClass.getName()));
namedMembers.setSamInterface(psiClass);
private void processSamInterface(@NotNull JavaClass javaClass) {
NamedMembers namedMembers = getOrCreateEmpty(javaClass.getName());
namedMembers.setSamInterface(javaClass);
}
}
private class ClassMemberProcessor {
@NotNull
private final PsiClass psiClass;
private final JavaClass javaClass;
private final boolean staticMembers;
private ClassMemberProcessor(@NotNull JavaClass javaClass, boolean staticMembers) {
this.psiClass = javaClass.getPsi();
this.javaClass = javaClass;
this.staticMembers = staticMembers;
}
@@ -156,7 +148,7 @@ public final class MembersCache {
}
private void processFields() {
for (final PsiField field : psiClass.getAllFields()) {
for (final JavaField field : javaClass.getAllFields()) {
addTask(field, new RunOnce() {
@Override
public void doRun() {
@@ -172,13 +164,13 @@ public final class MembersCache {
}
private void createEntriesForAllMethods() {
for (PsiMethod method : psiClass.getAllMethods()) {
getOrCreateEmpty(Name.identifier(method.getName()));
for (JavaMethod method : javaClass.getAllMethods()) {
getOrCreateEmpty(method.getName());
}
}
private void processOwnMethods() {
for (final PsiMethod method : psiClass.getMethods()) {
for (final JavaMethod method : javaClass.getMethods()) {
addTask(method, new RunOnce() {
@Override
public void doRun() {
@@ -192,7 +184,7 @@ public final class MembersCache {
if (!staticMembers) {
return;
}
for (final PsiClass nested : psiClass.getInnerClasses()) {
for (final JavaClass nested : javaClass.getInnerClasses()) {
addTask(nested, new RunOnce() {
@Override
public void doRun() {
@@ -202,8 +194,8 @@ public final class MembersCache {
}
}
private boolean includeMember(PsiMemberWrapper member) {
if (psiClass.isEnum() && staticMembers) {
private boolean includeMember(@NotNull JavaMember member) {
if (javaClass.isEnum() && staticMembers) {
return member.isStatic();
}
@@ -211,48 +203,47 @@ public final class MembersCache {
return false;
}
if (member.getPsiMember().getContainingClass() != psiClass) {
if (!isInCurrentClass(member)) {
return false;
}
if (member.isPrivate()) {
if (member.getVisibility() == Visibilities.PRIVATE) {
return false;
}
if (DescriptorResolverUtils.isObjectMethodInInterface(member.getPsiMember())) {
if (DescriptorResolverUtils.isObjectMethodInInterface(member.getPsi())) {
return false;
}
return true;
}
private void processField(PsiField field) {
PsiFieldWrapper fieldWrapper = new PsiFieldWrapper(field);
private boolean isInCurrentClass(@NotNull JavaMember member) {
JavaClass containingClass = member.getContainingClass();
if (containingClass == null) return false;
FqName fqName = containingClass.getFqName();
return fqName != null && fqName.equals(javaClass.getFqName());
}
private void processField(@NotNull JavaField field) {
// group must be created even for excluded field
NamedMembers namedMembers = getOrCreateEmpty(Name.identifier(fieldWrapper.getName()));
NamedMembers namedMembers = getOrCreateEmpty(field.getName());
if (!includeMember(fieldWrapper)) {
return;
if (includeMember(field)) {
namedMembers.addField(field);
}
namedMembers.addField(fieldWrapper);
}
private void processOwnMethod(PsiMethod ownMethod) {
PsiMethodWrapper method = new PsiMethodWrapper(ownMethod);
if (!includeMember(method)) {
return;
private void processOwnMethod(@NotNull JavaMethod ownMethod) {
if (includeMember(ownMethod)) {
NamedMembers namedMembers = getOrCreateEmpty(ownMethod.getName());
namedMembers.addMethod(ownMethod);
}
NamedMembers namedMembers = getOrCreateEmpty(Name.identifier(method.getName()));
namedMembers.addMethod(method);
}
private void processNestedClass(PsiClass nested) {
private void processNestedClass(@NotNull JavaClass nested) {
if (SingleAbstractMethodUtils.isSamInterface(nested)) {
NamedMembers namedMembers = getOrCreateEmpty(Name.identifier(nested.getName()));
NamedMembers namedMembers = getOrCreateEmpty(nested.getName());
namedMembers.setSamInterface(nested);
}
}
@@ -17,11 +17,11 @@
package org.jetbrains.jet.lang.resolve.java.provider;
import com.google.common.collect.Lists;
import com.intellij.psi.PsiClass;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiFieldWrapper;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMethodWrapper;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClass;
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.List;
@@ -36,23 +36,23 @@ public final class NamedMembers {
private final Name name;
@NotNull
private final List<PsiMethodWrapper> methods = Lists.newArrayList();
private final List<JavaMethod> methods = Lists.newArrayList();
@NotNull
private final List<PsiFieldWrapper> fields = Lists.newArrayList();
private final List<JavaField> fields = Lists.newArrayList();
@Nullable
private PsiClass samInterface;
private JavaClass samInterface;
void addMethod(@NotNull PsiMethodWrapper method) {
void addMethod(@NotNull JavaMethod method) {
methods.add(method);
}
void addField(@NotNull PsiFieldWrapper field) {
void addField(@NotNull JavaField field) {
fields.add(field);
}
void setSamInterface(@NotNull PsiClass samInterface) {
void setSamInterface(@NotNull JavaClass samInterface) {
this.samInterface = samInterface;
}
@@ -62,17 +62,17 @@ public final class NamedMembers {
}
@NotNull
public List<PsiMethodWrapper> getMethods() {
public List<JavaMethod> getMethods() {
return methods;
}
@NotNull
public List<PsiFieldWrapper> getFields() {
public List<JavaField> getFields() {
return fields;
}
@Nullable
public PsiClass getSamInterface() {
public JavaClass getSamInterface() {
return samInterface;
}
}
@@ -31,7 +31,7 @@ import org.jetbrains.jet.lang.resolve.DescriptorResolver;
import org.jetbrains.jet.lang.resolve.java.*;
import org.jetbrains.jet.lang.resolve.java.kotlinSignature.AlternativeMethodSignatureData;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClass;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMethodWrapper;
import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
@@ -71,22 +71,21 @@ public final class JavaConstructorResolver {
public Collection<ConstructorDescriptor> resolveConstructors(@NotNull JavaClass javaClass, @NotNull ClassDescriptor containingClass) {
PsiClass psiClass = javaClass.getPsi();
Collection<ConstructorDescriptor> constructors = Lists.newArrayList();
Collection<ConstructorDescriptor> result = Lists.newArrayList();
List<TypeParameterDescriptor> typeParameters = containingClass.getTypeConstructor().getParameters();
TypeVariableResolver typeVariableResolver =
new TypeVariableResolver(typeParameters, containingClass, "class " + psiClass.getQualifiedName());
new TypeVariableResolver(typeParameters, containingClass, "class " + javaClass.getFqName());
PsiMethod[] psiConstructors = psiClass.getConstructors();
Collection<JavaMethod> constructors = javaClass.getConstructors();
boolean isStatic = psiClass.hasModifierProperty(PsiModifier.STATIC);
if (containingClass.getKind() == ClassKind.OBJECT || containingClass.getKind() == ClassKind.CLASS_OBJECT) {
constructors.add(DescriptorResolver.createPrimaryConstructorForObject(containingClass));
result.add(DescriptorResolver.createPrimaryConstructorForObject(containingClass));
}
else if (psiConstructors.length == 0) {
else if (constructors.isEmpty()) {
if (trace.get(BindingContext.CONSTRUCTOR, psiClass) != null) {
constructors.add(trace.get(BindingContext.CONSTRUCTOR, psiClass));
result.add(trace.get(BindingContext.CONSTRUCTOR, psiClass));
}
else {
Visibility constructorVisibility = getConstructorVisibility(containingClass);
@@ -94,17 +93,18 @@ public final class JavaConstructorResolver {
// Example:
// class Kotlin() : Java() {}
// abstract public class Java {}
if (!psiClass.isInterface()) {
if (!javaClass.isInterface()) {
ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
containingClass,
Collections.<AnnotationDescriptor>emptyList(),
true);
constructorDescriptor
.initialize(typeParameters, Collections.<ValueParameterDescriptor>emptyList(), constructorVisibility, isStatic);
constructors.add(constructorDescriptor);
.initialize(typeParameters, Collections.<ValueParameterDescriptor>emptyList(), constructorVisibility,
javaClass.isStatic());
result.add(constructorDescriptor);
trace.record(BindingContext.CONSTRUCTOR, psiClass, constructorDescriptor);
}
if (psiClass.isAnnotationType()) {
if (javaClass.isAnnotationType()) {
// A constructor for an annotation type takes all the "methods" in the @interface as parameters
ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
containingClass,
@@ -141,27 +141,25 @@ public final class JavaConstructorResolver {
}
}
constructorDescriptor.initialize(typeParameters, valueParameters, constructorVisibility, isStatic);
constructors.add(constructorDescriptor);
constructorDescriptor.initialize(typeParameters, valueParameters, constructorVisibility, javaClass.isStatic());
result.add(constructorDescriptor);
trace.record(BindingContext.CONSTRUCTOR, psiClass, constructorDescriptor);
}
}
}
else {
for (PsiMethod psiConstructor : psiConstructors) {
ConstructorDescriptor constructor = resolveConstructor(psiClass, isStatic, psiConstructor, containingClass);
if (constructor != null) {
constructors.add(constructor);
ContainerUtil.addIfNotNull(constructors, resolveSamAdapter(constructor));
}
for (JavaMethod constructor : constructors) {
ConstructorDescriptor descriptor = resolveConstructor(javaClass, constructor, containingClass);
result.add(descriptor);
ContainerUtil.addIfNotNull(result, resolveSamAdapter(descriptor));
}
}
for (ConstructorDescriptor constructor : constructors) {
for (ConstructorDescriptor constructor : result) {
((ConstructorDescriptorImpl) constructor).setReturnType(containingClass.getDefaultType());
}
return constructors;
return result;
}
@NotNull
@@ -173,17 +171,15 @@ public final class JavaConstructorResolver {
return visibility;
}
@Nullable
@NotNull
private ConstructorDescriptor resolveConstructor(
PsiClass psiClass,
boolean aStatic,
PsiMethod psiConstructor,
ClassDescriptor classDescriptor
@NotNull JavaClass javaClass,
@NotNull JavaMethod constructor,
@NotNull ClassDescriptor classDescriptor
) {
PsiMethodWrapper constructor = new PsiMethodWrapper(psiConstructor);
if (trace.get(BindingContext.CONSTRUCTOR, psiConstructor) != null) {
return trace.get(BindingContext.CONSTRUCTOR, psiConstructor);
ConstructorDescriptor alreadyResolved = trace.get(BindingContext.CONSTRUCTOR, constructor.getPsi());
if (alreadyResolved != null) {
return alreadyResolved;
}
ConstructorDescriptorImpl constructorDescriptor = new ConstructorDescriptorImpl(
@@ -194,8 +190,8 @@ public final class JavaConstructorResolver {
List<TypeParameterDescriptor> typeParameters = classDescriptor.getTypeConstructor().getParameters();
JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors = valueParameterResolver.resolveParameterDescriptors(
constructorDescriptor, constructor.getParameters(),
new TypeVariableResolver(typeParameters, classDescriptor, "constructor of class " + psiClass.getQualifiedName())
constructorDescriptor, constructor.getValueParameters(),
new TypeVariableResolver(typeParameters, classDescriptor, "constructor of class " + javaClass.getFqName())
);
if (valueParameterDescriptors.getReceiverType() != null) {
@@ -215,9 +211,9 @@ public final class JavaConstructorResolver {
constructorDescriptor.initialize(typeParameters,
valueParameterDescriptors.getDescriptors(),
DescriptorResolverUtils.resolveVisibility(psiConstructor),
aStatic);
trace.record(BindingContext.CONSTRUCTOR, psiConstructor, constructorDescriptor);
constructor.getVisibility(),
javaClass.isStatic());
trace.record(BindingContext.CONSTRUCTOR, constructor.getPsi(), constructorDescriptor);
return constructorDescriptor;
}
@@ -19,7 +19,6 @@ package org.jetbrains.jet.lang.resolve.java.resolver;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiType;
import com.intellij.psi.util.PsiFormatUtil;
@@ -34,9 +33,9 @@ import org.jetbrains.jet.lang.resolve.java.*;
import org.jetbrains.jet.lang.resolve.java.descriptor.ClassDescriptorFromJvmBytecode;
import org.jetbrains.jet.lang.resolve.java.kotlinSignature.AlternativeMethodSignatureData;
import org.jetbrains.jet.lang.resolve.java.kotlinSignature.SignaturesPropagationData;
import org.jetbrains.jet.lang.resolve.java.kotlinSignature.SignaturesUtil;
import org.jetbrains.jet.lang.resolve.java.provider.NamedMembers;
import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMethodWrapper;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.*;
@@ -96,41 +95,40 @@ public final class JavaFunctionResolver {
@Nullable
private SimpleFunctionDescriptor resolveMethodToFunctionDescriptor(
@NotNull JavaMethod javaMethod,
@NotNull JavaMethod method,
@NotNull ClassOrNamespaceDescriptor ownerDescriptor,
boolean record
) {
PsiMethodWrapper method = new PsiMethodWrapper(javaMethod.getPsi());
if (!DescriptorResolverUtils.isCorrectOwnerForEnumMember(ownerDescriptor, method.getPsiMember())) {
if (!DescriptorResolverUtils.isCorrectOwnerForEnumMember(ownerDescriptor, method)) {
return null;
}
PsiType returnPsiType = method.getReturnType();
PsiMethod psiMethod = method.getPsi();
PsiType returnPsiType = psiMethod.getReturnType();
if (returnPsiType == null) {
return null;
}
PsiMethod psiMethod = method.getPsiMethod();
if (trace.get(BindingContext.FUNCTION, psiMethod) != null) {
return trace.get(BindingContext.FUNCTION, psiMethod);
SimpleFunctionDescriptor alreadyResolved = trace.get(BindingContext.FUNCTION, psiMethod);
if (alreadyResolved != null) {
return alreadyResolved;
}
SimpleFunctionDescriptorImpl functionDescriptorImpl = new SimpleFunctionDescriptorImpl(
ownerDescriptor,
annotationResolver.resolveAnnotations(psiMethod),
Name.identifier(method.getName()),
method.getName(),
CallableMemberDescriptor.Kind.DECLARATION
);
List<TypeParameterDescriptor> methodTypeParameters = signatureResolver.resolveMethodTypeParameters(method, functionDescriptorImpl);
TypeVariableResolver methodTypeVariableResolver = new TypeVariableResolver(
methodTypeParameters, functionDescriptorImpl, "method " + method.getName() + " in class " + psiMethod.getContainingClass());
methodTypeParameters, functionDescriptorImpl, "method " + method.getName() + " in class " + method.getContainingClass());
JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors = parameterResolver
.resolveParameterDescriptors(functionDescriptorImpl, method.getParameters(), methodTypeVariableResolver);
.resolveParameterDescriptors(functionDescriptorImpl, method.getValueParameters(), methodTypeVariableResolver);
JetType returnType = makeReturnType(returnPsiType, method, methodTypeVariableResolver);
List<String> signatureErrors = Lists.newArrayList();
@@ -170,7 +168,7 @@ public final class JavaFunctionResolver {
valueParameterDescriptors.getDescriptors(),
returnType,
Modality.convertFromFlags(method.isAbstract(), !method.isFinal()),
DescriptorResolverUtils.resolveVisibility(psiMethod),
method.getVisibility(),
/*isInline = */ false
);
@@ -199,9 +197,9 @@ public final class JavaFunctionResolver {
}
private static void checkFunctionsOverrideCorrectly(
PsiMethodWrapper method,
List<FunctionDescriptor> superFunctions,
FunctionDescriptor functionDescriptor
@NotNull JavaMethod method,
@NotNull List<FunctionDescriptor> superFunctions,
@NotNull FunctionDescriptor functionDescriptor
) {
for (FunctionDescriptor superFunction : superFunctions) {
ClassDescriptor klass = (ClassDescriptor) functionDescriptor.getContainingDeclaration();
@@ -230,8 +228,8 @@ public final class JavaFunctionResolver {
+ "super class = " + superFunction.getContainingDeclaration() + "\n"
+ "sub function = " + functionDescriptor + "\n"
+ "sub class = " + functionDescriptor.getContainingDeclaration() + "\n"
+ "sub method = " + PsiFormatUtil.getExternalName(method.getPsiMethod()) + "\n"
+ "@KotlinSignature = " + method.getSignatureAnnotation().signature());
+ "sub method = " + PsiFormatUtil.getExternalName(method.getPsi()) + "\n"
+ "@KotlinSignature = " + SignaturesUtil.getKotlinSignature(method));
}
}
}
@@ -246,8 +244,8 @@ public final class JavaFunctionResolver {
}
Set<SimpleFunctionDescriptor> functionsFromCurrent = Sets.newHashSet();
for (PsiMethodWrapper method : members.getMethods()) {
SimpleFunctionDescriptor function = resolveMethodToFunctionDescriptor(new JavaMethod(method.getPsiMethod()), owner, true);
for (JavaMethod method : members.getMethods()) {
SimpleFunctionDescriptor function = resolveMethodToFunctionDescriptor(method, owner, true);
if (function != null) {
functionsFromCurrent.add(function);
ContainerUtil.addIfNotNull(functionsFromCurrent, resolveSamAdapter(function));
@@ -336,15 +334,11 @@ public final class JavaFunctionResolver {
}
@Nullable
private SimpleFunctionDescriptor resolveSamConstructor(
@NotNull NamespaceDescriptor ownerDescriptor,
@NotNull NamedMembers namedMembers
) {
PsiClass samInterface = namedMembers.getSamInterface();
if (samInterface != null) {
ClassDescriptorFromJvmBytecode klass = findClassInNamespace(ownerDescriptor, namedMembers.getName());
private SimpleFunctionDescriptor resolveSamConstructor(@NotNull NamespaceDescriptor owner, @NotNull NamedMembers namedMembers) {
if (namedMembers.getSamInterface() != null) {
ClassDescriptorFromJvmBytecode klass = findClassInNamespace(owner, namedMembers.getName());
if (klass != null) {
return recordSamConstructor(klass, createSamConstructorFunction(ownerDescriptor, klass), trace);
return recordSamConstructor(klass, createSamConstructorFunction(owner, klass), trace);
}
}
return null;
@@ -368,15 +362,16 @@ public final class JavaFunctionResolver {
@NotNull
private JetType makeReturnType(
PsiType returnType, PsiMethodWrapper method,
@NotNull PsiType returnType,
@NotNull JavaMethod method,
@NotNull TypeVariableResolver typeVariableResolver
) {
PsiMethod psiMethod = method.getPsi();
TypeUsage typeUsage = JavaTypeTransformer
.adjustTypeUsageWithMutabilityAnnotations(method.getPsiMethod(), TypeUsage.MEMBER_SIGNATURE_COVARIANT);
TypeUsage typeUsage = JavaTypeTransformer.adjustTypeUsageWithMutabilityAnnotations(psiMethod, TypeUsage.MEMBER_SIGNATURE_COVARIANT);
JetType transformedType = typeTransformer.transformToType(returnType, typeUsage, typeVariableResolver);
if (JavaAnnotationResolver.findAnnotationWithExternal(method.getPsiMethod(), JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION) != null) {
if (JavaAnnotationResolver.findAnnotationWithExternal(psiMethod, JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION) != null) {
return TypeUtils.makeNullableAsSpecified(transformedType, false);
}
else {
@@ -238,7 +238,7 @@ public final class JavaNamespaceResolver {
}
for (PsiClass nestedClass : psiClass.getInnerClasses()) {
if (SingleAbstractMethodUtils.isSamInterface(nestedClass)) {
if (SingleAbstractMethodUtils.isSamInterface(new JavaClass(nestedClass))) {
return true;
}
if (nestedClass.hasModifierProperty(PsiModifier.STATIC) && hasStaticMembers(nestedClass)) {
@@ -17,7 +17,6 @@
package org.jetbrains.jet.lang.resolve.java.resolver;
import com.google.common.collect.Sets;
import com.intellij.psi.PsiEnumConstant;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLiteralExpression;
import org.jetbrains.annotations.NotNull;
@@ -31,7 +30,7 @@ import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.java.*;
import org.jetbrains.jet.lang.resolve.java.kotlinSignature.AlternativeFieldSignatureData;
import org.jetbrains.jet.lang.resolve.java.provider.NamedMembers;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiFieldWrapper;
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.JetType;
@@ -70,13 +69,13 @@ public final class JavaPropertyResolver {
public Set<VariableDescriptor> resolveFieldGroup(@NotNull NamedMembers members, @NotNull ClassOrNamespaceDescriptor ownerDescriptor) {
Name propertyName = members.getName();
List<PsiFieldWrapper> fields = members.getFields();
List<JavaField> fields = members.getFields();
Set<PropertyDescriptor> propertiesFromCurrent = new HashSet<PropertyDescriptor>(1);
assert fields.size() <= 1;
if (fields.size() == 1) {
PsiFieldWrapper field = fields.iterator().next();
if (DescriptorResolverUtils.isCorrectOwnerForEnumMember(ownerDescriptor, field.getPsiField())) {
JavaField field = fields.iterator().next();
if (DescriptorResolverUtils.isCorrectOwnerForEnumMember(ownerDescriptor, field)) {
propertiesFromCurrent.add(resolveProperty(ownerDescriptor, propertyName,
"class or namespace " + DescriptorUtils.getFQName(ownerDescriptor), field));
}
@@ -128,14 +127,11 @@ public final class JavaPropertyResolver {
@NotNull ClassOrNamespaceDescriptor owner,
@NotNull Name propertyName,
@NotNull String context,
@NotNull PsiFieldWrapper field
@NotNull JavaField field
) {
boolean isVar = !field.isFinal();
Visibility visibility = DescriptorResolverUtils.resolveVisibility(field.getPsiField());
PropertyDescriptorImpl propertyDescriptor =
createPropertyDescriptor(owner, propertyName, field, isVar, visibility);
PropertyDescriptorImpl propertyDescriptor = createPropertyDescriptor(owner, propertyName, field, isVar);
propertyDescriptor.initialize(null, null);
@@ -153,12 +149,12 @@ public final class JavaPropertyResolver {
DescriptorUtils.getExpectedThisObjectIfNeeded(owner),
(JetType) null
);
trace.record(BindingContext.VARIABLE, field.getPsiField(), propertyDescriptor);
trace.record(BindingContext.VARIABLE, field.getPsi(), propertyDescriptor);
trace.record(JavaBindingContext.IS_DECLARED_IN_JAVA, propertyDescriptor);
if (AnnotationUtils.isPropertyAcceptableAsAnnotationParameter(propertyDescriptor)) {
PsiExpression initializer = field.getPsiField().getInitializer();
PsiExpression initializer = field.getPsi().getInitializer();
if (initializer instanceof PsiLiteralExpression) {
CompileTimeConstant<?> constant = JavaCompileTimeConstResolver
.resolveCompileTimeConstantValue(((PsiLiteralExpression) initializer).getValue(), propertyType);
@@ -174,12 +170,13 @@ public final class JavaPropertyResolver {
private PropertyDescriptorImpl createPropertyDescriptor(
@NotNull ClassOrNamespaceDescriptor owner,
@NotNull Name propertyName,
@NotNull PsiFieldWrapper field,
boolean isVar,
@NotNull Visibility visibility
@NotNull JavaField field,
boolean isVar
) {
boolean isEnumEntry = field.getPsiField() instanceof PsiEnumConstant;
if (isEnumEntry) {
List<AnnotationDescriptor> annotations = annotationResolver.resolveAnnotations(field.getPsi());
Visibility visibility = field.getVisibility();
if (field.isEnumEntry()) {
assert !isVar : "Enum entries should be immutable.";
assert DescriptorUtils.isEnumClassObject(owner) : "Enum entries should be put into class object of enum only: " + owner;
//TODO: this is a hack to indicate that this enum entry is an object
@@ -194,14 +191,14 @@ public final class JavaPropertyResolver {
false);
return new PropertyDescriptorForObjectImpl(
owner,
annotationResolver.resolveAnnotations(field.getPsiField()),
annotations,
visibility,
propertyName,
dummyClassDescriptorForEnumEntryObject);
}
return new PropertyDescriptorImpl(
owner,
annotationResolver.resolveAnnotations(field.getPsiField()),
annotations,
Modality.FINAL,
visibility,
isVar,
@@ -212,9 +209,9 @@ public final class JavaPropertyResolver {
@NotNull
private JetType getAlternativeSignatureData(
boolean isVar,
PsiFieldWrapper field,
PropertyDescriptor propertyDescriptor,
JetType propertyType
@NotNull JavaField field,
@NotNull PropertyDescriptor propertyDescriptor,
@NotNull JetType propertyType
) {
AlternativeFieldSignatureData signatureData = new AlternativeFieldSignatureData(field, propertyType, isVar);
if (signatureData.hasErrors()) {
@@ -228,11 +225,11 @@ public final class JavaPropertyResolver {
}
@NotNull
private JetType getPropertyType(@NotNull PsiFieldWrapper field, @NotNull TypeVariableResolver typeVariableResolver) {
JetType propertyType = typeTransformer.transformToType(field.getType(), typeVariableResolver);
private JetType getPropertyType(@NotNull JavaField field, @NotNull TypeVariableResolver typeVariableResolver) {
JetType propertyType = typeTransformer.transformToType(field.getPsi().getType(), typeVariableResolver);
boolean hasNotNullAnnotation = JavaAnnotationResolver.findAnnotationWithExternal(
field.getPsiField(),
field.getPsi(),
JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION
) != null;
@@ -255,7 +252,7 @@ public final class JavaPropertyResolver {
return r;
}
private static boolean isStaticFinalField(@NotNull PsiFieldWrapper wrapper) {
return wrapper.isFinal() && wrapper.isStatic();
private static boolean isStaticFinalField(@NotNull JavaField field) {
return field.isFinal() && field.isStatic();
}
}
@@ -17,10 +17,7 @@
package org.jetbrains.jet.lang.resolve.java.resolver;
import com.google.common.collect.Lists;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiTypeParameter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
@@ -32,8 +29,8 @@ import org.jetbrains.jet.lang.resolve.java.TypeUsage;
import org.jetbrains.jet.lang.resolve.java.TypeVariableResolver;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClass;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClassType;
import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod;
import org.jetbrains.jet.lang.resolve.java.structure.JavaTypeParameter;
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMethodWrapper;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.Variance;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
@@ -71,16 +68,14 @@ public final class JavaSignatureResolver {
}
}
@NotNull
private static List<TypeParameterDescriptorInitialization> makeUninitializedTypeParameters(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull PsiTypeParameter[] typeParameters
@NotNull DeclarationDescriptor container,
@NotNull Collection<JavaTypeParameter> typeParameters
) {
List<TypeParameterDescriptorInitialization> result = Lists.newArrayList();
for (PsiTypeParameter typeParameter : typeParameters) {
TypeParameterDescriptorInitialization typeParameterDescriptor =
makeUninitializedTypeParameter(containingDeclaration, new JavaTypeParameter(typeParameter));
result.add(typeParameterDescriptor);
for (JavaTypeParameter typeParameter : typeParameters) {
result.add(makeUninitializedTypeParameter(container, typeParameter));
}
return result;
}
@@ -88,17 +83,17 @@ public final class JavaSignatureResolver {
@NotNull
private static TypeParameterDescriptorInitialization makeUninitializedTypeParameter(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull JavaTypeParameter javaTypeParameter
@NotNull JavaTypeParameter typeParameter
) {
TypeParameterDescriptorImpl typeParameterDescriptor = TypeParameterDescriptorImpl.createForFurtherModification(
containingDeclaration,
Collections.<AnnotationDescriptor>emptyList(), // TODO
false,
Variance.INVARIANT,
javaTypeParameter.getName(),
javaTypeParameter.getIndex()
typeParameter.getName(),
typeParameter.getIndex()
);
return new TypeParameterDescriptorInitialization(typeParameterDescriptor, javaTypeParameter);
return new TypeParameterDescriptorInitialization(typeParameterDescriptor, typeParameter);
}
private void initializeTypeParameter(
@@ -153,19 +148,18 @@ public final class JavaSignatureResolver {
}
@NotNull
public List<TypeParameterDescriptor> resolveMethodTypeParameters(
@NotNull PsiMethodWrapper method,
@NotNull JavaMethod method,
@NotNull DeclarationDescriptor functionDescriptor
) {
PsiMethod psiMethod = method.getPsiMethod();
List<TypeParameterDescriptorInitialization> typeParametersIntialization =
makeUninitializedTypeParameters(functionDescriptor, psiMethod.getTypeParameters());
makeUninitializedTypeParameters(functionDescriptor, method.getTypeParameters());
PsiClass psiMethodContainingClass = psiMethod.getContainingClass();
assert psiMethodContainingClass != null;
String context = "method " + method.getName() + " in class " + psiMethodContainingClass.getQualifiedName();
initializeTypeParameters(typeParametersIntialization, functionDescriptor, context);
JavaClass containingClass = method.getContainingClass();
assert containingClass != null;
initializeTypeParameters(typeParametersIntialization, functionDescriptor,
"method " + method.getName() + " in class " + containingClass.getFqName());
List<TypeParameterDescriptor> typeParameters = Lists.newArrayListWithCapacity(typeParametersIntialization.size());
@@ -26,6 +26,7 @@ import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.ValueParameterDescriptorImpl;
import org.jetbrains.jet.lang.resolve.java.*;
import org.jetbrains.jet.lang.resolve.java.structure.JavaValueParameter;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils;
@@ -33,11 +34,11 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public final class JavaValueParameterResolver {
private JavaTypeTransformer typeTransformer;
public JavaValueParameterResolver() {
@@ -45,18 +46,21 @@ public final class JavaValueParameterResolver {
@NotNull
private ValueParameterDescriptor resolveParameterDescriptor(
DeclarationDescriptor containingDeclaration, int i,
PsiParameter parameter, TypeVariableResolver typeVariableResolver
@NotNull DeclarationDescriptor containingDeclaration,
int i,
@NotNull JavaValueParameter parameter,
@NotNull TypeVariableResolver typeVariableResolver
) {
PsiParameter psiParameter = parameter.getPsi();
PsiType psiType = parameter.getType();
PsiType psiType = psiParameter.getType();
// TODO: must be very slow, make it lazy?
Name name = Name.identifier(getParameterName(i, parameter));
Name name = getParameterName(i, parameter);
TypeUsage typeUsage = JavaTypeTransformer
.adjustTypeUsageWithMutabilityAnnotations(parameter, TypeUsage.MEMBER_SIGNATURE_CONTRAVARIANT);
JetType outType = getTypeTransformer().transformToType(psiType, typeUsage, typeVariableResolver);
.adjustTypeUsageWithMutabilityAnnotations(psiParameter, TypeUsage.MEMBER_SIGNATURE_CONTRAVARIANT);
JetType outType = typeTransformer.transformToType(psiType, typeUsage, typeVariableResolver);
JetType varargElementType;
if (psiType instanceof PsiEllipsisType) {
@@ -69,7 +73,7 @@ public final class JavaValueParameterResolver {
JetType transformedType;
PsiAnnotation notNullAnnotation = JavaAnnotationResolver
.findAnnotationWithExternal(parameter, JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION);
.findAnnotationWithExternal(psiParameter, JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION);
if (notNullAnnotation != null) {
transformedType = TypeUtils.makeNullableAsSpecified(outType, false);
}
@@ -87,33 +91,29 @@ public final class JavaValueParameterResolver {
);
}
@NotNull
private JavaTypeTransformer getTypeTransformer() {
return typeTransformer;
}
@Inject
public void setTypeTransformer(JavaTypeTransformer typeTransformer) {
this.typeTransformer = typeTransformer;
}
@NotNull
private static String getParameterName(int number, @NotNull PsiParameter parameter) {
String psiParameterName = parameter.getName();
return psiParameterName != null ? psiParameterName : "p" + number;
private static Name getParameterName(int number, @NotNull JavaValueParameter parameter) {
Name psiParameterName = parameter.getName();
return psiParameterName != null ? psiParameterName : Name.identifier("p" + number);
}
@NotNull
public JavaDescriptorResolver.ValueParameterDescriptors resolveParameterDescriptors(
DeclarationDescriptor containingDeclaration,
List<PsiParameter> parameters, TypeVariableResolver typeVariableResolver
@NotNull DeclarationDescriptor container,
@NotNull Collection<JavaValueParameter> parameters,
@NotNull TypeVariableResolver typeVariableResolver
) {
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
int indexDelta = 0;
for (int i = 0, parametersLength = parameters.size(); i < parametersLength; i++) {
PsiParameter parameter = parameters.get(i);
ValueParameterDescriptor parameterDescriptor =
resolveParameterDescriptor(containingDeclaration, i + indexDelta, parameter, typeVariableResolver);
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(parameters.size());
int index = 0;
for (JavaValueParameter parameter : parameters) {
ValueParameterDescriptor parameterDescriptor = resolveParameterDescriptor(container, index, parameter, typeVariableResolver);
result.add(parameterDescriptor);
index++;
}
return new JavaDescriptorResolver.ValueParameterDescriptors(null, result);
}
@@ -320,8 +320,8 @@ public class SingleAbstractMethodUtils {
private SingleAbstractMethodUtils() {
}
public static boolean isSamInterface(@NotNull PsiClass psiClass) {
return getSamInterfaceMethod(new JavaClass(psiClass), psiClass.getProject()) != null;
public static boolean isSamInterface(@NotNull JavaClass javaClass) {
return getSamInterfaceMethod(javaClass, javaClass.getPsi().getProject()) != null;
}
// Returns null if not SAM interface
@@ -18,25 +18,20 @@ package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierList;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ClassKind;
import org.jetbrains.jet.lang.descriptors.Modality;
import org.jetbrains.jet.lang.descriptors.Visibilities;
import org.jetbrains.jet.lang.descriptors.Visibility;
import org.jetbrains.jet.lang.resolve.java.JavaVisibilities;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.Collection;
import java.util.Collections;
import static org.jetbrains.jet.lang.resolve.java.structure.JavaElementCollectionFromPsiArrayUtil.*;
public class JavaClass extends JavaElementImpl implements JavaNamedElement, JavaModifierListOwner, JavaTypeParameterListOwner {
public class JavaClass extends JavaModifierListOwnerImpl implements JavaNamedElement, JavaTypeParameterListOwner {
public JavaClass(@NotNull PsiClass psiClass) {
super(psiClass);
}
@@ -107,46 +102,6 @@ public class JavaClass extends JavaElementImpl implements JavaNamedElement, Java
return isAnnotationType() ? Modality.FINAL : Modality.convertFromFlags(isAbstract() || isInterface(), !isFinal());
}
@Override
public boolean isAbstract() {
return getPsi().hasModifierProperty(PsiModifier.ABSTRACT);
}
@Override
public boolean isFinal() {
return getPsi().hasModifierProperty(PsiModifier.FINAL);
}
@Override
public boolean isStatic() {
return getPsi().hasModifierProperty(PsiModifier.STATIC);
}
@NotNull
@Override
public Visibility getVisibility() {
if (getPsi().hasModifierProperty(PsiModifier.PUBLIC)) {
return Visibilities.PUBLIC;
}
if (getPsi().hasModifierProperty(PsiModifier.PRIVATE)) {
return Visibilities.PRIVATE;
}
if (getPsi().hasModifierProperty(PsiModifier.PROTECTED)) {
return isStatic() ? JavaVisibilities.PROTECTED_STATIC_VISIBILITY : JavaVisibilities.PROTECTED_AND_PACKAGE;
}
return JavaVisibilities.PACKAGE_VISIBILITY;
}
@NotNull
@Override
public Collection<JavaAnnotation> getAnnotations() {
PsiModifierList modifierList = getPsi().getModifierList();
if (modifierList != null) {
return annotations(modifierList.getAnnotations());
}
return Collections.emptyList();
}
@NotNull
@Override
public Collection<JavaTypeParameter> getTypeParameters() {
@@ -166,4 +121,19 @@ public class JavaClass extends JavaElementImpl implements JavaNamedElement, Java
public Collection<JavaMethod> getMethods() {
return methods(getPsi().getMethods());
}
@NotNull
public Collection<JavaMethod> getAllMethods() {
return methods(getPsi().getAllMethods());
}
@NotNull
public Collection<JavaField> getAllFields() {
return fields(getPsi().getAllFields());
}
@NotNull
public Collection<JavaMethod> getConstructors() {
return methods(getPsi().getConstructors());
}
}
@@ -58,6 +58,26 @@ import java.util.List;
return result;
}
@NotNull
public static Collection<JavaField> fields(@NotNull PsiField[] fields) {
if (fields.length == 0) return Collections.emptyList();
List<JavaField> result = new ArrayList<JavaField>(fields.length);
for (PsiField psiField : fields) {
result.add(new JavaField(psiField));
}
return result;
}
@NotNull
public static Collection<JavaValueParameter> valueParameters(@NotNull PsiParameter[] parameters) {
if (parameters.length == 0) return Collections.emptyList();
List<JavaValueParameter> result = new ArrayList<JavaValueParameter>(parameters.length);
for (PsiParameter psiParameter : parameters) {
result.add(new JavaValueParameter(psiParameter));
}
return result;
}
@NotNull
public static Collection<JavaTypeParameter> typeParameters(@NotNull PsiTypeParameter[] typeParameters) {
if (typeParameters.length == 0) return Collections.emptyList();
@@ -0,0 +1,46 @@
/*
* 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.PsiEnumConstant;
import com.intellij.psi.PsiField;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class JavaField extends JavaMemberImpl {
public JavaField(@NotNull PsiField psiField) {
super(psiField);
}
@NotNull
@Override
public PsiField getPsi() {
return (PsiField) super.getPsi();
}
@Nullable
@Override
public JavaClass getContainingClass() {
PsiClass psiClass = getPsi().getContainingClass();
return psiClass == null ? null : new JavaClass(psiClass);
}
public boolean isEnumEntry() {
return getPsi() instanceof PsiEnumConstant;
}
}
@@ -16,9 +16,15 @@
package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiMember;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface JavaMember extends JavaElement {
public interface JavaMember extends JavaModifierListOwner, JavaNamedElement {
@NotNull
@Override
PsiMember getPsi();
// TODO: NotNull ?
@Nullable
JavaClass getContainingClass();
@@ -14,28 +14,28 @@
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.java.wrapper;
package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMember;
import com.intellij.psi.PsiType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.name.Name;
public class PsiFieldWrapper extends PsiMemberWrapper {
public PsiFieldWrapper(@NotNull PsiMember psiMember) {
public abstract class JavaMemberImpl extends JavaModifierListOwnerImpl implements JavaMember {
protected JavaMemberImpl(@NotNull PsiMember psiMember) {
super(psiMember);
}
public PsiField getPsiField() {
return (PsiField) psiMember;
}
public PsiType getType() {
return getPsiField().getType();
@NotNull
@Override
public PsiMember getPsi() {
return (PsiMember) super.getPsi();
}
@NotNull
@Override
public boolean isAbstract() {
return false;
public Name getName() {
String name = getPsi().getName();
assert name != null : "Member must have a name: " + getPsi();
return Name.identifier(name);
}
}
@@ -25,8 +25,9 @@ import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.Collection;
import static org.jetbrains.jet.lang.resolve.java.structure.JavaElementCollectionFromPsiArrayUtil.typeParameters;
import static org.jetbrains.jet.lang.resolve.java.structure.JavaElementCollectionFromPsiArrayUtil.valueParameters;
public class JavaMethod extends JavaElementImpl implements JavaTypeParameterListOwner, JavaNamedElement, JavaMember {
public class JavaMethod extends JavaMemberImpl implements JavaTypeParameterListOwner {
public JavaMethod(@NotNull PsiMethod psiMethod) {
super(psiMethod);
}
@@ -56,4 +57,9 @@ public class JavaMethod extends JavaElementImpl implements JavaTypeParameterList
public Collection<JavaTypeParameter> getTypeParameters() {
return typeParameters(getPsi().getTypeParameters());
}
@NotNull
public Collection<JavaValueParameter> getValueParameters() {
return valueParameters(getPsi().getParameterList().getParameters());
}
}
@@ -0,0 +1,82 @@
/*
* 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.PsiModifier;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiModifierListOwner;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.Visibilities;
import org.jetbrains.jet.lang.descriptors.Visibility;
import org.jetbrains.jet.lang.resolve.java.JavaVisibilities;
import java.util.Collection;
import java.util.Collections;
import static org.jetbrains.jet.lang.resolve.java.structure.JavaElementCollectionFromPsiArrayUtil.annotations;
public abstract class JavaModifierListOwnerImpl extends JavaElementImpl implements JavaModifierListOwner {
protected JavaModifierListOwnerImpl(@NotNull PsiModifierListOwner psiModifierListOwner) {
super(psiModifierListOwner);
}
@NotNull
@Override
public PsiModifierListOwner getPsi() {
return (PsiModifierListOwner) super.getPsi();
}
@Override
public boolean isAbstract() {
return getPsi().hasModifierProperty(PsiModifier.ABSTRACT);
}
@Override
public boolean isStatic() {
return getPsi().hasModifierProperty(PsiModifier.STATIC);
}
@Override
public boolean isFinal() {
return getPsi().hasModifierProperty(PsiModifier.FINAL);
}
@NotNull
@Override
public Visibility getVisibility() {
if (getPsi().hasModifierProperty(PsiModifier.PUBLIC)) {
return Visibilities.PUBLIC;
}
if (getPsi().hasModifierProperty(PsiModifier.PRIVATE)) {
return Visibilities.PRIVATE;
}
if (getPsi().hasModifierProperty(PsiModifier.PROTECTED)) {
return isStatic() ? JavaVisibilities.PROTECTED_STATIC_VISIBILITY : JavaVisibilities.PROTECTED_AND_PACKAGE;
}
return JavaVisibilities.PACKAGE_VISIBILITY;
}
@NotNull
@Override
public Collection<JavaAnnotation> getAnnotations() {
PsiModifierList modifierList = getPsi().getModifierList();
if (modifierList != null) {
return annotations(modifierList.getAnnotations());
}
return Collections.emptyList();
}
}
@@ -0,0 +1,40 @@
/*
* 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.PsiParameter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.name.Name;
public class JavaValueParameter extends JavaElementImpl {
public JavaValueParameter(@NotNull PsiParameter psiParameter) {
super(psiParameter);
}
@NotNull
@Override
public PsiParameter getPsi() {
return (PsiParameter) super.getPsi();
}
@Nullable
public Name getName() {
String name = getPsi().getName();
return name == null ? null : Name.identifier(name);
}
}
@@ -1,69 +0,0 @@
/*
* 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.wrapper;
import com.intellij.psi.PsiMember;
import com.intellij.psi.PsiModifier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.java.kotlinSignature.KotlinSignatureAnnotation;
public abstract class PsiMemberWrapper {
@NotNull
protected final PsiMember psiMember;
public PsiMemberWrapper(@NotNull PsiMember psiMember) {
this.psiMember = psiMember;
}
@NotNull
public PsiMember getPsiMember() {
return psiMember;
}
public boolean isStatic() {
return psiMember.hasModifierProperty(PsiModifier.STATIC);
}
public boolean isPrivate() {
return psiMember.hasModifierProperty(PsiModifier.PRIVATE);
}
public boolean isFinal() {
return psiMember.hasModifierProperty(PsiModifier.FINAL);
}
public abstract boolean isAbstract();
public String getName() {
return psiMember.getName();
}
private KotlinSignatureAnnotation signatureAnnotation;
@NotNull
public KotlinSignatureAnnotation getSignatureAnnotation() {
if (signatureAnnotation == null) {
signatureAnnotation = KotlinSignatureAnnotation.get(getPsiMember());
}
return signatureAnnotation;
}
@Override
public String toString() {
return psiMember.getText();
}
}
@@ -1,59 +0,0 @@
/*
* 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.wrapper;
import com.google.common.collect.Lists;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class PsiMethodWrapper extends PsiMemberWrapper {
public PsiMethodWrapper(@NotNull PsiMethod psiMethod) {
super(psiMethod);
}
private List<PsiParameter> parameters;
@NotNull
public List<PsiParameter> getParameters() {
if (parameters == null) {
parameters = Lists.newArrayList(getPsiMethod().getParameterList().getParameters());
}
return parameters;
}
@Override
public boolean isAbstract() {
return psiMember.hasModifierProperty(PsiModifier.ABSTRACT);
}
@NotNull
public PsiMethod getPsiMethod() {
return (PsiMethod) psiMember;
}
@Nullable
public PsiType getReturnType() {
return getPsiMethod().getReturnType();
}
}