kotlin.reflect.jvm.internal.impl.load.java.components.DescriptorResolverUtils 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.components;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import kotlin.reflect.jvm.internal.impl.descriptors.CallableMemberDescriptor;
import kotlin.reflect.jvm.internal.impl.descriptors.ClassConstructorDescriptor;
import kotlin.reflect.jvm.internal.impl.descriptors.ClassDescriptor;
import kotlin.reflect.jvm.internal.impl.descriptors.ValueParameterDescriptor;
import kotlin.reflect.jvm.internal.impl.load.java.structure.*;
import kotlin.reflect.jvm.internal.impl.name.FqName;
import kotlin.reflect.jvm.internal.impl.name.Name;
import kotlin.reflect.jvm.internal.impl.resolve.NonReportingOverrideStrategy;
import kotlin.reflect.jvm.internal.impl.resolve.OverridingUtil;
import kotlin.reflect.jvm.internal.impl.serialization.deserialization.ErrorReporter;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public final class DescriptorResolverUtils {
private DescriptorResolverUtils() {
}
@NotNull
public static Collection resolveOverridesForNonStaticMembers(
@NotNull Name name, @NotNull Collection membersFromSupertypes, @NotNull Collection membersFromCurrent,
@NotNull ClassDescriptor classDescriptor, @NotNull ErrorReporter errorReporter,
@NotNull OverridingUtil overridingUtil
) {
return resolveOverrides(name, membersFromSupertypes, membersFromCurrent, classDescriptor, errorReporter, overridingUtil, false);
}
@NotNull
public static Collection resolveOverridesForStaticMembers(
@NotNull Name name, @NotNull Collection membersFromSupertypes, @NotNull Collection membersFromCurrent,
@NotNull ClassDescriptor classDescriptor, @NotNull ErrorReporter errorReporter,
@NotNull OverridingUtil overridingUtil
) {
return resolveOverrides(name, membersFromSupertypes, membersFromCurrent, classDescriptor, errorReporter, overridingUtil, true);
}
@NotNull
private static Collection resolveOverrides(
@NotNull Name name,
@NotNull Collection membersFromSupertypes,
@NotNull Collection membersFromCurrent,
@NotNull ClassDescriptor classDescriptor,
@NotNull final ErrorReporter errorReporter,
@NotNull OverridingUtil overridingUtil,
final boolean isStaticContext
) {
final Set result = new LinkedHashSet();
overridingUtil.generateOverridesInFunctionGroup(
name, membersFromSupertypes, membersFromCurrent, classDescriptor,
new NonReportingOverrideStrategy() {
@Override
@SuppressWarnings("unchecked")
public void addFakeOverride(@NotNull CallableMemberDescriptor fakeOverride) {
OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, new Function1() {
@Override
public Unit invoke(@NotNull CallableMemberDescriptor descriptor) {
errorReporter.reportCannotInferVisibility(descriptor);
return Unit.INSTANCE;
}
});
result.add((D) fakeOverride);
}
@Override
public void conflict(@NotNull CallableMemberDescriptor fromSuper, @NotNull CallableMemberDescriptor fromCurrent) {
// nop
}
@Override
public void setOverriddenDescriptors(
@NotNull CallableMemberDescriptor member, @NotNull Collection overridden
) {
// do not set overridden descriptors for declared static fields and methods from java
if (isStaticContext && member.getKind() != CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
return;
}
super.setOverriddenDescriptors(member, overridden);
}
}
);
return result;
}
@Nullable
public static ValueParameterDescriptor getAnnotationParameterByName(@NotNull Name name, @NotNull ClassDescriptor annotationClass) {
Collection constructors = annotationClass.getConstructors();
if (constructors.size() != 1) return null;
for (ValueParameterDescriptor parameter : constructors.iterator().next().getValueParameters()) {
if (parameter.getName().equals(name)) {
return parameter;
}
}
return null;
}
public static boolean isObjectMethodInInterface(@NotNull JavaMember member) {
return member.getContainingClass().isInterface() && member instanceof JavaMethod && isObjectMethod((JavaMethod) member);
}
private static boolean isObjectMethod(@NotNull JavaMethod method) {
String name = method.getName().asString();
if (name.equals("toString") || name.equals("hashCode")) {
return method.getValueParameters().isEmpty();
}
else if (name.equals("equals")) {
return isMethodWithOneObjectParameter(method);
}
return false;
}
private static boolean isMethodWithOneObjectParameter(@NotNull JavaMethod method) {
List parameters = method.getValueParameters();
if (parameters.size() == 1) {
JavaType type = parameters.get(0).getType();
if (type instanceof JavaClassifierType) {
JavaClassifier classifier = ((JavaClassifierType) type).getClassifier();
if (classifier instanceof JavaClass) {
FqName classFqName = ((JavaClass) classifier).getFqName();
return classFqName != null && classFqName.asString().equals("java.lang.Object");
}
}
}
return false;
}
}