Refactor and comment JvmCodegenUtil#couldUseDirectAccessToProperty()
Logic hasn't changed except the deleted isTopLevelDeclaration() call which always returned false in this case because we checked that it's a class property earlier
This commit is contained in:
@@ -55,6 +55,7 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jetbrains.jet.lang.descriptors.Modality.ABSTRACT;
|
import static org.jetbrains.jet.lang.descriptors.Modality.ABSTRACT;
|
||||||
|
import static org.jetbrains.jet.lang.descriptors.Modality.FINAL;
|
||||||
|
|
||||||
public class JvmCodegenUtil {
|
public class JvmCodegenUtil {
|
||||||
|
|
||||||
@@ -246,32 +247,36 @@ public class JvmCodegenUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean couldUseDirectAccessToProperty(
|
public static boolean couldUseDirectAccessToProperty(
|
||||||
@NotNull PropertyDescriptor propertyDescriptor,
|
@NotNull PropertyDescriptor property,
|
||||||
boolean forGetter,
|
boolean forGetter,
|
||||||
boolean isInsideClass,
|
boolean isInsideClass,
|
||||||
boolean isDelegated,
|
boolean isDelegated,
|
||||||
MethodContext context
|
@NotNull MethodContext context
|
||||||
) {
|
) {
|
||||||
if (context.isInlineFunction()) {
|
if (JetTypeMapper.isAccessor(property)) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
PropertyAccessorDescriptor accessorDescriptor = forGetter ? propertyDescriptor.getGetter() : propertyDescriptor.getSetter();
|
|
||||||
boolean isExtensionProperty = propertyDescriptor.getReceiverParameter() != null;
|
|
||||||
boolean specialTypeProperty = isDelegated ||
|
|
||||||
isExtensionProperty ||
|
|
||||||
DescriptorUtils.isClassObject(propertyDescriptor.getContainingDeclaration()) ||
|
|
||||||
JetTypeMapper.isAccessor(propertyDescriptor);
|
|
||||||
return isInsideClass &&
|
|
||||||
!specialTypeProperty &&
|
|
||||||
(accessorDescriptor == null ||
|
|
||||||
accessorDescriptor.isDefault() &&
|
|
||||||
(!isExternallyAccessible(propertyDescriptor) || accessorDescriptor.getModality() == Modality.FINAL));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isExternallyAccessible(@NotNull PropertyDescriptor propertyDescriptor) {
|
// Inline functions can't use direct access because a field may not be visible at the call site
|
||||||
return propertyDescriptor.getVisibility() != Visibilities.PRIVATE ||
|
if (context.isInlineFunction()) return false;
|
||||||
DescriptorUtils.isClassObject(propertyDescriptor.getContainingDeclaration()) ||
|
|
||||||
DescriptorUtils.isTopLevelDeclaration(propertyDescriptor);
|
// Only properties of the same class can be directly accessed
|
||||||
|
if (!isInsideClass) return false;
|
||||||
|
|
||||||
|
// Delegated and extension properties have no backing fields
|
||||||
|
if (isDelegated || property.getReceiverParameter() != null) return false;
|
||||||
|
|
||||||
|
// Class object properties cannot be accessed directly because their backing fields are stored in the containing class
|
||||||
|
if (DescriptorUtils.isClassObject(property.getContainingDeclaration())) return false;
|
||||||
|
|
||||||
|
PropertyAccessorDescriptor accessor = forGetter ? property.getGetter() : property.getSetter();
|
||||||
|
|
||||||
|
// If there's no accessor declared we can use direct access
|
||||||
|
if (accessor == null) return true;
|
||||||
|
|
||||||
|
// If the accessor is non-default (i.e. it has some code) we should call that accessor and not use direct access
|
||||||
|
if (!accessor.isDefault()) return false;
|
||||||
|
|
||||||
|
// If the accessor is private or final, it can't be overridden in the subclass and thus we can use direct access
|
||||||
|
return property.getVisibility() == Visibilities.PRIVATE || accessor.getModality() == FINAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
Reference in New Issue
Block a user