kotlin.reflect.jvm.internal.impl.load.java.descriptors.JavaMethodDescriptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-reflect Show documentation
Show all versions of kotlin-reflect Show documentation
Kotlin Full Reflection Library
/*
* Copyright 2010-2015 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 kotlin.reflect.jvm.internal.impl.load.java.descriptors;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import kotlin.reflect.jvm.internal.impl.descriptors.*;
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.Annotations;
import kotlin.reflect.jvm.internal.impl.descriptors.impl.SimpleFunctionDescriptorImpl;
import kotlin.reflect.jvm.internal.impl.name.Name;
import kotlin.reflect.jvm.internal.impl.resolve.DescriptorFactory;
import kotlin.reflect.jvm.internal.impl.types.KotlinType;
import kotlin.reflect.jvm.internal.impl.util.OperatorChecks;
import java.util.List;
import java.util.Map;
public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implements JavaCallableMemberDescriptor {
// TODO: It's only used to retrieve annotations from the original value parameter and can be removed when
// kotlin.reflect.jvm.internal.impl.descriptors.impl.FunctionDescriptorImpl.initialize accepts extension parameter descriptor
// instead of type
public static final UserDataKey ORIGINAL_VALUE_PARAMETER_FOR_EXTENSION_RECEIVER =
new UserDataKey() {};
private enum ParameterNamesStatus {
NON_STABLE_DECLARED(false, false),
STABLE_DECLARED(true, false),
NON_STABLE_SYNTHESIZED(false, true),
STABLE_SYNTHESIZED(true, true), // TODO: this makes no sense
;
public final boolean isStable;
public final boolean isSynthesized;
ParameterNamesStatus(boolean isStable, boolean isSynthesized) {
this.isStable = isStable;
this.isSynthesized = isSynthesized;
}
@NotNull
public static ParameterNamesStatus get(boolean stable, boolean synthesized) {
return stable ? (synthesized ? STABLE_SYNTHESIZED : STABLE_DECLARED) :
(synthesized ? NON_STABLE_SYNTHESIZED : NON_STABLE_DECLARED);
}
}
private ParameterNamesStatus parameterNamesStatus = null;
protected JavaMethodDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@Nullable SimpleFunctionDescriptor original,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull Kind kind,
@NotNull SourceElement source
) {
super(containingDeclaration, original, annotations, name, kind, source);
}
@NotNull
public static JavaMethodDescriptor createJavaMethod(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull SourceElement source
) {
return new JavaMethodDescriptor(containingDeclaration, null, annotations, name, Kind.DECLARATION, source);
}
@NotNull
@Override
public SimpleFunctionDescriptorImpl initialize(
@Nullable ReceiverParameterDescriptor extensionReceiverParameter,
@Nullable ReceiverParameterDescriptor dispatchReceiverParameter,
@NotNull List typeParameters,
@NotNull List unsubstitutedValueParameters,
@Nullable KotlinType unsubstitutedReturnType,
@Nullable Modality modality,
@NotNull DescriptorVisibility visibility,
@Nullable Map, ?> userData
) {
SimpleFunctionDescriptorImpl descriptor = super.initialize(
extensionReceiverParameter, dispatchReceiverParameter, typeParameters, unsubstitutedValueParameters,
unsubstitutedReturnType, modality, visibility, userData
);
setOperator(OperatorChecks.INSTANCE.check(descriptor).isSuccess());
return descriptor;
}
@Override
public boolean hasStableParameterNames() {
assert parameterNamesStatus != null : "Parameter names status was not set: " + this;
return parameterNamesStatus.isStable;
}
@Override
public boolean hasSynthesizedParameterNames() {
assert parameterNamesStatus != null : "Parameter names status was not set: " + this;
return parameterNamesStatus.isSynthesized;
}
public void setParameterNamesStatus(boolean hasStableParameterNames, boolean hasSynthesizedParameterNames) {
this.parameterNamesStatus = ParameterNamesStatus.get(hasStableParameterNames, hasSynthesizedParameterNames);
}
@NotNull
@Override
protected JavaMethodDescriptor createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind,
@Nullable Name newName,
@NotNull Annotations annotations,
@NotNull SourceElement source
) {
JavaMethodDescriptor result = new JavaMethodDescriptor(
newOwner,
(SimpleFunctionDescriptor) original,
annotations,
newName != null ? newName : getName(),
kind,
source
);
result.setParameterNamesStatus(hasStableParameterNames(), hasSynthesizedParameterNames());
return result;
}
@Override
@NotNull
public JavaMethodDescriptor enhance(
@Nullable KotlinType enhancedReceiverType,
@NotNull List enhancedValueParametersData,
@NotNull KotlinType enhancedReturnType,
@Nullable Pair, ?> additionalUserData
) {
List enhancedValueParameters =
UtilKt.copyValueParameters(enhancedValueParametersData, getValueParameters(), this);
ReceiverParameterDescriptor enhancedReceiver =
enhancedReceiverType == null ? null : DescriptorFactory.createExtensionReceiverParameterForCallable(
this, enhancedReceiverType, Annotations.Companion.getEMPTY()
);
JavaMethodDescriptor enhancedMethod =
(JavaMethodDescriptor) newCopyBuilder()
.setValueParameters(enhancedValueParameters)
.setReturnType(enhancedReturnType)
.setExtensionReceiverParameter(enhancedReceiver)
.setDropOriginalInContainingParts()
.setPreserveSourceElement()
.build();
assert enhancedMethod != null : "null after substitution while enhancing " + toString();
if (additionalUserData != null) {
enhancedMethod.putInUserDataMap(additionalUserData.getFirst(), additionalUserData.getSecond());
}
return enhancedMethod;
}
}