Allow rendering Any in all positions and using Function0 etc instead of "() -> ..."

This commit is contained in:
Andrey Breslav
2013-05-31 16:32:44 +04:00
committed by Alexander Udalov
parent 5b8d41cc36
commit af415d160f
3 changed files with 51 additions and 19 deletions
@@ -33,6 +33,8 @@ public class DescriptorRendererBuilder {
private boolean unitReturnType = true; private boolean unitReturnType = true;
private boolean normalizedVisibilities = false; private boolean normalizedVisibilities = false;
private boolean showInternalKeyword = true; private boolean showInternalKeyword = true;
private boolean alwaysRenderAny = false;
private boolean prettyFunctionTypes = true;
@NotNull @NotNull
private DescriptorRenderer.OverrideRenderingPolicy overrideRenderingPolicy = DescriptorRenderer.OverrideRenderingPolicy.RENDER_OPEN; private DescriptorRenderer.OverrideRenderingPolicy overrideRenderingPolicy = DescriptorRenderer.OverrideRenderingPolicy.RENDER_OPEN;
@NotNull @NotNull
@@ -115,10 +117,20 @@ public class DescriptorRendererBuilder {
return this; return this;
} }
public DescriptorRendererBuilder setAlwaysRenderAny(boolean alwaysRenderAny) {
this.alwaysRenderAny = alwaysRenderAny;
return this;
}
public DescriptorRendererBuilder setPrettyFunctionTypes(boolean prettyFunctionTypes) {
this.prettyFunctionTypes = prettyFunctionTypes;
return this;
}
public DescriptorRenderer build() { public DescriptorRenderer build() {
return new DescriptorRendererImpl(shortNames, withDefinedIn, modifiers, startFromName, debugMode, classWithPrimaryConstructor, return new DescriptorRendererImpl(shortNames, withDefinedIn, modifiers, startFromName, debugMode, classWithPrimaryConstructor,
verbose, unitReturnType, normalizedVisibilities, showInternalKeyword, overrideRenderingPolicy, verbose, unitReturnType, normalizedVisibilities, showInternalKeyword, alwaysRenderAny, prettyFunctionTypes,
valueParametersHandler, textFormat, excludedAnnotationClasses); overrideRenderingPolicy, valueParametersHandler, textFormat, excludedAnnotationClasses);
} }
} }
@@ -58,6 +58,8 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
private final boolean unitReturnType; private final boolean unitReturnType;
private final boolean normalizedVisibilities; private final boolean normalizedVisibilities;
private final boolean showInternalKeyword; private final boolean showInternalKeyword;
private final boolean alwaysRenderAny;
private final boolean prettyFunctionTypes;
@NotNull @NotNull
private final OverrideRenderingPolicy overrideRenderingPolicy; private final OverrideRenderingPolicy overrideRenderingPolicy;
@NotNull @NotNull
@@ -78,6 +80,8 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
boolean unitReturnType, boolean unitReturnType,
boolean normalizedVisibilities, boolean normalizedVisibilities,
boolean showInternalKeyword, boolean showInternalKeyword,
boolean alwaysRenderAny,
boolean prettyFunctionTypes,
@NotNull OverrideRenderingPolicy overrideRenderingPolicy, @NotNull OverrideRenderingPolicy overrideRenderingPolicy,
@NotNull ValueParametersHandler handler, @NotNull ValueParametersHandler handler,
@NotNull TextFormat textFormat, @NotNull TextFormat textFormat,
@@ -97,6 +101,8 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
this.debugMode = debugMode; this.debugMode = debugMode;
this.textFormat = textFormat; this.textFormat = textFormat;
this.excludedAnnotationClasses = Sets.newHashSet(excludedAnnotationClasses); this.excludedAnnotationClasses = Sets.newHashSet(excludedAnnotationClasses);
this.alwaysRenderAny = alwaysRenderAny;
this.prettyFunctionTypes = prettyFunctionTypes;
} }
@@ -218,7 +224,7 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
if (ErrorUtils.isErrorType(type)) { if (ErrorUtils.isErrorType(type)) {
return type.toString(); return type.toString();
} }
if (KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(type)) { if (KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(type) && prettyFunctionTypes) {
return renderFunctionType(type); return renderFunctionType(type);
} }
return renderDefaultType(type); return renderDefaultType(type);
@@ -411,7 +417,7 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
int upperBoundsCount = typeParameter.getUpperBounds().size(); int upperBoundsCount = typeParameter.getUpperBounds().size();
if ((upperBoundsCount > 1 && !topLevel) || upperBoundsCount == 1) { if ((upperBoundsCount > 1 && !topLevel) || upperBoundsCount == 1) {
JetType upperBound = typeParameter.getUpperBounds().iterator().next(); JetType upperBound = typeParameter.getUpperBounds().iterator().next();
if (!KotlinBuiltIns.getInstance().getDefaultBound().equals(upperBound)) { if (!KotlinBuiltIns.getInstance().getDefaultBound().equals(upperBound) || alwaysRenderAny) {
builder.append(" : ").append(renderType(upperBound)); builder.append(" : ").append(renderType(upperBound));
} }
} }
@@ -640,7 +646,7 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
if (!klass.equals(KotlinBuiltIns.getInstance().getNothing())) { if (!klass.equals(KotlinBuiltIns.getInstance().getNothing())) {
Collection<JetType> supertypes = klass.getTypeConstructor().getSupertypes(); Collection<JetType> supertypes = klass.getTypeConstructor().getSupertypes();
if (supertypes.isEmpty() || supertypes.size() == 1 && KotlinBuiltIns.getInstance().isAny(supertypes.iterator().next())) { if (supertypes.isEmpty() || !alwaysRenderAny && supertypes.size() == 1 && KotlinBuiltIns.getInstance().isAny(supertypes.iterator().next())) {
} }
else { else {
builder.append(" : "); builder.append(" : ");
@@ -47,17 +47,23 @@ import java.util.List;
import static org.jetbrains.jet.test.util.DescriptorValidator.ValidationVisitor.FORBID_ERROR_TYPES; import static org.jetbrains.jet.test.util.DescriptorValidator.ValidationVisitor.FORBID_ERROR_TYPES;
public class NamespaceComparator { public class NamespaceComparator {
public static final Configuration DONT_INCLUDE_METHODS_OF_OBJECT = new Configuration(false, false, false, Predicates.<FqNameUnsafe>alwaysTrue(), private static final DescriptorRenderer DEFAULT_RENDERER = new DescriptorRendererBuilder()
FORBID_ERROR_TYPES);
public static final Configuration RECURSIVE = new Configuration(false, false, true, Predicates.<FqNameUnsafe>alwaysTrue(), FORBID_ERROR_TYPES);
public static final Configuration RECURSIVE_ALL = new Configuration(true, true, true, Predicates.<FqNameUnsafe>alwaysTrue(), FORBID_ERROR_TYPES);
private static final DescriptorRenderer RENDERER = new DescriptorRendererBuilder()
.setWithDefinedIn(false) .setWithDefinedIn(false)
.setExcludedAnnotationClasses(Arrays.asList(new FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME))) .setExcludedAnnotationClasses(Arrays.asList(new FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME)))
.setOverrideRenderingPolicy(DescriptorRenderer.OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE) .setOverrideRenderingPolicy(DescriptorRenderer.OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE)
.setVerbose(true).build(); .setVerbose(true).build();
public static final Configuration DONT_INCLUDE_METHODS_OF_OBJECT = new Configuration(false, false, false,
Predicates.<FqNameUnsafe>alwaysTrue(),
FORBID_ERROR_TYPES, DEFAULT_RENDERER);
public static final Configuration RECURSIVE = new Configuration(false, false, true,
Predicates.<FqNameUnsafe>alwaysTrue(),
FORBID_ERROR_TYPES, DEFAULT_RENDERER);
public static final Configuration RECURSIVE_ALL = new Configuration(true, true, true,
Predicates.<FqNameUnsafe>alwaysTrue(),
FORBID_ERROR_TYPES, DEFAULT_RENDERER);
private static final ImmutableSet<String> JAVA_OBJECT_METHOD_NAMES = ImmutableSet.of( private static final ImmutableSet<String> JAVA_OBJECT_METHOD_NAMES = ImmutableSet.of(
"equals", "hashCode", "finalize", "wait", "notify", "notifyAll", "toString", "clone", "getClass"); "equals", "hashCode", "finalize", "wait", "notify", "notifyAll", "toString", "clone", "getClass");
@@ -79,7 +85,7 @@ public class NamespaceComparator {
} }
boolean isPrimaryConstructor = descriptor instanceof ConstructorDescriptor && ((ConstructorDescriptor) descriptor).isPrimary(); boolean isPrimaryConstructor = descriptor instanceof ConstructorDescriptor && ((ConstructorDescriptor) descriptor).isPrimary();
printer.print(isPrimaryConstructor && conf.checkPrimaryConstructors ? "/*primary*/ " : "", RENDERER.render(descriptor)); printer.print(isPrimaryConstructor && conf.checkPrimaryConstructors ? "/*primary*/ " : "", conf.renderer.render(descriptor));
if (descriptor instanceof ClassOrNamespaceDescriptor) { if (descriptor instanceof ClassOrNamespaceDescriptor) {
if (!topLevel) { if (!topLevel) {
@@ -109,12 +115,12 @@ public class NamespaceComparator {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor; PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
PropertyGetterDescriptor getter = propertyDescriptor.getGetter(); PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
if (getter != null) { if (getter != null) {
printer.println(RENDERER.render(getter)); printer.println(conf.renderer.render(getter));
} }
PropertySetterDescriptor setter = propertyDescriptor.getSetter(); PropertySetterDescriptor setter = propertyDescriptor.getSetter();
if (setter != null) { if (setter != null) {
printer.println(RENDERER.render(setter)); printer.println(conf.renderer.render(setter));
} }
printer.popIndent(); printer.popIndent();
@@ -241,6 +247,7 @@ public class NamespaceComparator {
private final boolean checkPropertyAccessors; private final boolean checkPropertyAccessors;
private final boolean includeMethodsOfJavaObject; private final boolean includeMethodsOfJavaObject;
private final Predicate<FqNameUnsafe> recurseIntoPackage; private final Predicate<FqNameUnsafe> recurseIntoPackage;
private final DescriptorRenderer renderer;
private final DescriptorValidator.ValidationVisitor validationStrategy; private final DescriptorValidator.ValidationVisitor validationStrategy;
@@ -249,33 +256,40 @@ public class NamespaceComparator {
boolean checkPropertyAccessors, boolean checkPropertyAccessors,
boolean includeMethodsOfJavaObject, boolean includeMethodsOfJavaObject,
Predicate<FqNameUnsafe> recurseIntoPackage, Predicate<FqNameUnsafe> recurseIntoPackage,
DescriptorValidator.ValidationVisitor validationStrategy DescriptorValidator.ValidationVisitor validationStrategy,
DescriptorRenderer renderer
) { ) {
this.checkPrimaryConstructors = checkPrimaryConstructors; this.checkPrimaryConstructors = checkPrimaryConstructors;
this.checkPropertyAccessors = checkPropertyAccessors; this.checkPropertyAccessors = checkPropertyAccessors;
this.includeMethodsOfJavaObject = includeMethodsOfJavaObject; this.includeMethodsOfJavaObject = includeMethodsOfJavaObject;
this.recurseIntoPackage = recurseIntoPackage; this.recurseIntoPackage = recurseIntoPackage;
this.validationStrategy = validationStrategy; this.validationStrategy = validationStrategy;
this.renderer = renderer;
} }
public Configuration filterRecursion(@NotNull Predicate<FqNameUnsafe> recurseIntoPackage) { public Configuration filterRecursion(@NotNull Predicate<FqNameUnsafe> recurseIntoPackage) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage,
validationStrategy); validationStrategy, renderer);
} }
public Configuration checkPrimaryConstructors(boolean checkPrimaryConstructors) { public Configuration checkPrimaryConstructors(boolean checkPrimaryConstructors) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage,
validationStrategy); validationStrategy, renderer);
} }
public Configuration checkPropertyAccessors(boolean checkPropertyAccessors) { public Configuration checkPropertyAccessors(boolean checkPropertyAccessors) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage,
validationStrategy); validationStrategy, renderer);
} }
public Configuration withValidationStrategy(@NotNull DescriptorValidator.ValidationVisitor validationStrategy) { public Configuration withValidationStrategy(@NotNull DescriptorValidator.ValidationVisitor validationStrategy) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage,
validationStrategy); validationStrategy, renderer);
}
public Configuration withRenderer(@NotNull DescriptorRenderer renderer) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfJavaObject, recurseIntoPackage,
validationStrategy, renderer);
} }
} }
} }