001 /* 002 * Copyright 2010-2015 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.kotlin.load.java.components; 018 019 import org.jetbrains.annotations.NotNull; 020 import org.jetbrains.annotations.Nullable; 021 import org.jetbrains.kotlin.descriptors.*; 022 import org.jetbrains.kotlin.load.java.structure.JavaField; 023 import org.jetbrains.kotlin.load.java.structure.JavaMember; 024 import org.jetbrains.kotlin.load.java.structure.JavaMethod; 025 import org.jetbrains.kotlin.types.JetType; 026 027 import java.util.Collections; 028 import java.util.List; 029 030 public interface ExternalSignatureResolver { 031 ExternalSignatureResolver DO_NOTHING = new ExternalSignatureResolver() { 032 @NotNull 033 @Override 034 public PropagatedMethodSignature resolvePropagatedSignature( 035 @NotNull JavaMethod method, 036 @NotNull ClassDescriptor owner, 037 @NotNull JetType returnType, 038 @Nullable JetType receiverType, 039 @NotNull List<ValueParameterDescriptor> valueParameters, 040 @NotNull List<TypeParameterDescriptor> typeParameters 041 ) { 042 return new PropagatedMethodSignature( 043 returnType, receiverType, valueParameters, typeParameters, Collections.<String>emptyList(), false, 044 Collections.<FunctionDescriptor>emptyList() 045 ); 046 } 047 048 @NotNull 049 @Override 050 public AlternativeMethodSignature resolveAlternativeMethodSignature( 051 @NotNull JavaMember methodOrConstructor, 052 boolean hasSuperMethods, 053 @Nullable JetType returnType, 054 @Nullable JetType receiverType, 055 @NotNull List<ValueParameterDescriptor> valueParameters, 056 @NotNull List<TypeParameterDescriptor> typeParameters, 057 boolean hasStableParameterNames 058 ) { 059 return new AlternativeMethodSignature( 060 returnType, receiverType, valueParameters, typeParameters, Collections.<String>emptyList(), hasStableParameterNames 061 ); 062 } 063 064 @NotNull 065 @Override 066 public AlternativeFieldSignature resolveAlternativeFieldSignature( 067 @NotNull JavaField field, @NotNull JetType returnType, boolean isVar 068 ) { 069 return new AlternativeFieldSignature(returnType, null); 070 } 071 072 @Override 073 public void reportSignatureErrors(@NotNull CallableMemberDescriptor descriptor, @NotNull List<String> signatureErrors) { 074 throw new UnsupportedOperationException("Should not be called"); 075 } 076 }; 077 078 abstract class MemberSignature { 079 private final List<String> signatureErrors; 080 081 protected MemberSignature(@NotNull List<String> signatureErrors) { 082 this.signatureErrors = signatureErrors; 083 } 084 085 @NotNull 086 public List<String> getErrors() { 087 return signatureErrors; 088 } 089 } 090 091 class AlternativeMethodSignature extends MemberSignature { 092 private final JetType returnType; 093 private final JetType receiverType; 094 private final List<ValueParameterDescriptor> valueParameters; 095 private final List<TypeParameterDescriptor> typeParameters; 096 private final boolean hasStableParameterNames; 097 098 public AlternativeMethodSignature( 099 @Nullable JetType returnType, 100 @Nullable JetType receiverType, 101 @NotNull List<ValueParameterDescriptor> valueParameters, 102 @NotNull List<TypeParameterDescriptor> typeParameters, 103 @NotNull List<String> signatureErrors, 104 boolean hasStableParameterNames 105 ) { 106 super(signatureErrors); 107 this.returnType = returnType; 108 this.receiverType = receiverType; 109 this.valueParameters = valueParameters; 110 this.typeParameters = typeParameters; 111 this.hasStableParameterNames = hasStableParameterNames; 112 } 113 114 @Nullable 115 public JetType getReturnType() { 116 return returnType; 117 } 118 119 @Nullable 120 public JetType getReceiverType() { 121 return receiverType; 122 } 123 124 @NotNull 125 public List<ValueParameterDescriptor> getValueParameters() { 126 return valueParameters; 127 } 128 129 @NotNull 130 public List<TypeParameterDescriptor> getTypeParameters() { 131 return typeParameters; 132 } 133 134 public boolean hasStableParameterNames() { 135 return hasStableParameterNames; 136 } 137 } 138 139 class AlternativeFieldSignature extends MemberSignature { 140 private final JetType returnType; 141 142 public AlternativeFieldSignature(@NotNull JetType returnType, @Nullable String signatureError) { 143 super(signatureError == null ? Collections.<String>emptyList() : Collections.singletonList(signatureError)); 144 this.returnType = returnType; 145 } 146 147 @NotNull 148 public JetType getReturnType() { 149 return returnType; 150 } 151 } 152 153 class PropagatedMethodSignature extends AlternativeMethodSignature { 154 private final List<FunctionDescriptor> superMethods; 155 156 public PropagatedMethodSignature( 157 @Nullable JetType returnType, 158 @Nullable JetType receiverType, 159 @NotNull List<ValueParameterDescriptor> valueParameters, 160 @NotNull List<TypeParameterDescriptor> typeParameters, 161 @NotNull List<String> signatureErrors, 162 boolean hasStableParameterNames, 163 @NotNull List<FunctionDescriptor> superMethods 164 ) { 165 super(returnType, receiverType, valueParameters, typeParameters, signatureErrors, hasStableParameterNames); 166 this.superMethods = superMethods; 167 } 168 169 @NotNull 170 public List<FunctionDescriptor> getSuperMethods() { 171 return superMethods; 172 } 173 } 174 175 @NotNull 176 PropagatedMethodSignature resolvePropagatedSignature( 177 @NotNull JavaMethod method, 178 @NotNull ClassDescriptor owner, 179 @NotNull JetType returnType, 180 @Nullable JetType receiverType, 181 @NotNull List<ValueParameterDescriptor> valueParameters, 182 @NotNull List<TypeParameterDescriptor> typeParameters 183 ); 184 185 @NotNull 186 AlternativeMethodSignature resolveAlternativeMethodSignature( 187 @NotNull JavaMember methodOrConstructor, 188 boolean hasSuperMethods, 189 @Nullable JetType returnType, 190 @Nullable JetType receiverType, 191 @NotNull List<ValueParameterDescriptor> valueParameters, 192 @NotNull List<TypeParameterDescriptor> typeParameters, 193 boolean hasStableParameterNames 194 ); 195 196 @NotNull 197 AlternativeFieldSignature resolveAlternativeFieldSignature( 198 @NotNull JavaField field, 199 @NotNull JetType returnType, 200 boolean isVar 201 ); 202 203 void reportSignatureErrors(@NotNull CallableMemberDescriptor descriptor, @NotNull List<String> signatureErrors); 204 }