Synthetic accessor refactoring: extract general interface

This commit is contained in:
Michael Bogdanov
2014-09-09 12:34:08 +04:00
parent e26d635633
commit 7aaae8a2f7
4 changed files with 65 additions and 8 deletions
@@ -0,0 +1,27 @@
/*
* Copyright 2010-2014 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.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor;
public interface AccessorForCallableDescriptor<T extends CallableMemberDescriptor> {
@NotNull
T getCalleeDescriptor();
}
@@ -30,7 +30,10 @@ import java.util.List;
import static org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
public class AccessorForFunctionDescriptor extends SimpleFunctionDescriptorImpl {
public class AccessorForFunctionDescriptor extends SimpleFunctionDescriptorImpl implements AccessorForCallableDescriptor<FunctionDescriptor> {
private final FunctionDescriptor calleeDescriptor;
public AccessorForFunctionDescriptor(
@NotNull FunctionDescriptor descriptor,
@NotNull DeclarationDescriptor containingDeclaration,
@@ -39,6 +42,7 @@ public class AccessorForFunctionDescriptor extends SimpleFunctionDescriptorImpl
super(containingDeclaration, null, Annotations.EMPTY,
Name.identifier((descriptor instanceof ConstructorDescriptor ? "$init" : descriptor.getName()) + "$b$" + index),
Kind.DECLARATION, SourceElement.NO_SOURCE);
this.calleeDescriptor = descriptor;
initialize(DescriptorUtils.getReceiverParameterType(descriptor.getReceiverParameter()),
descriptor instanceof ConstructorDescriptor ? NO_RECEIVER_PARAMETER : descriptor.getExpectedThisObject(),
@@ -76,4 +80,10 @@ public class AccessorForFunctionDescriptor extends SimpleFunctionDescriptorImpl
}
return result;
}
@NotNull
@Override
public FunctionDescriptor getCalleeDescriptor() {
return calleeDescriptor;
}
}
@@ -30,7 +30,9 @@ import org.jetbrains.jet.lang.types.JetType;
import java.util.Collections;
public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl {
public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implements AccessorForCallableDescriptor<PropertyDescriptor> {
private final PropertyDescriptor calleeDescriptor;
public AccessorForPropertyDescriptor(@NotNull PropertyDescriptor pd, @NotNull DeclarationDescriptor containingDeclaration, int index) {
this(pd, pd.getType(), DescriptorUtils.getReceiverParameterType(pd.getReceiverParameter()), pd.getExpectedThisObject(), containingDeclaration, index);
}
@@ -47,23 +49,44 @@ public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl {
original.isVar(), Name.identifier(original.getName() + "$b$" + index),
Kind.DECLARATION, SourceElement.NO_SOURCE);
this.calleeDescriptor = original;
setType(propertyType, Collections.<TypeParameterDescriptorImpl>emptyList(), expectedThisObject, receiverType);
initialize(new Getter(this), new Setter(this));
}
public static class Getter extends PropertyGetterDescriptorImpl {
public static class Getter extends PropertyGetterDescriptorImpl implements AccessorForCallableDescriptor<PropertyGetterDescriptor> {
public Getter(AccessorForPropertyDescriptor property) {
super(property, Annotations.EMPTY, Modality.FINAL, Visibilities.LOCAL,
false, false, Kind.DECLARATION, null, SourceElement.NO_SOURCE);
initialize(property.getType());
}
@NotNull
@Override
public PropertyGetterDescriptor getCalleeDescriptor() {
//noinspection ConstantConditions
return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getCalleeDescriptor().getGetter();
}
}
public static class Setter extends PropertySetterDescriptorImpl {
public static class Setter extends PropertySetterDescriptorImpl implements AccessorForCallableDescriptor<PropertySetterDescriptor>{
public Setter(AccessorForPropertyDescriptor property) {
super(property, Annotations.EMPTY, Modality.FINAL, Visibilities.LOCAL,
false, false, Kind.DECLARATION, null, SourceElement.NO_SOURCE);
initializeDefault();
}
@NotNull
@Override
public PropertySetterDescriptor getCalleeDescriptor() {
//noinspection ConstantConditions
return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getCalleeDescriptor().getSetter();
}
}
@NotNull
@Override
public PropertyDescriptor getCalleeDescriptor() {
return calleeDescriptor;
}
}
@@ -500,10 +500,7 @@ public class JetTypeMapper {
}
public static boolean isAccessor(@NotNull CallableMemberDescriptor descriptor) {
return descriptor instanceof AccessorForFunctionDescriptor ||
descriptor instanceof AccessorForPropertyDescriptor ||
descriptor instanceof AccessorForPropertyDescriptor.Getter ||
descriptor instanceof AccessorForPropertyDescriptor.Setter;
return descriptor instanceof AccessorForCallableDescriptor<?>;
}
@NotNull