Use DescriptorRenderer to render annotations everywhere

This commit is contained in:
Andrey Breslav
2014-01-14 15:40:38 +04:00
parent 9fa29535b4
commit 65d90e18a2
60 changed files with 272 additions and 102 deletions
@@ -20,9 +20,9 @@ import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.renderer.DescriptorRenderer;
import java.util.Collections;
import java.util.Map;
@@ -59,6 +59,6 @@ public class AnnotationDescriptorImpl implements AnnotationDescriptor {
@Override
public String toString() {
return annotationType.toString() + DescriptorUtils.getSortedValueArguments(this, null);
return DescriptorRenderer.TEXT.renderAnnotation(this);
}
}
@@ -0,0 +1,105 @@
/*
* 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.lang.descriptors.annotations;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.constants.*;
import org.jetbrains.jet.lang.resolve.constants.StringValue;
public abstract class DefaultAnnotationArgumentVisitor<R, D> implements AnnotationArgumentVisitor<R, D> {
public abstract R visitValue(@NotNull CompileTimeConstant<?> value, D data);
@Override
public R visitLongValue(@NotNull LongValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitIntValue(IntValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitShortValue(ShortValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitByteValue(ByteValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitDoubleValue(DoubleValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitFloatValue(FloatValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitBooleanValue(BooleanValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitCharValue(CharValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitStringValue(StringValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitNullValue(NullValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitErrorValue(ErrorValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitEnumValue(EnumValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitArrayValue(ArrayValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitAnnotationValue(AnnotationValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitJavaClassValue(JavaClassValue value, D data) {
return visitValue(value, data);
}
@Override
public R visitNumberTypeValue(IntegerValueTypeConstant value, D data) {
return visitValue(value, data);
}
}
@@ -23,9 +23,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.AnonymousFunctionDescriptor;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -35,9 +33,11 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.renderer.DescriptorRenderer;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
@@ -345,23 +345,6 @@ public class DescriptorUtils {
return Visibilities.PUBLIC;
}
@NotNull
public static List<String> getSortedValueArguments(
@NotNull AnnotationDescriptor descriptor,
@Nullable DescriptorRenderer rendererForTypesIfNecessary
) {
List<String> resultList = Lists.newArrayList();
for (Map.Entry<ValueParameterDescriptor, CompileTimeConstant<?>> entry : descriptor.getAllValueArguments().entrySet()) {
CompileTimeConstant<?> value = entry.getValue();
String typeSuffix = rendererForTypesIfNecessary == null
? ""
: ": " + rendererForTypesIfNecessary.renderType(value.getType(KotlinBuiltIns.getInstance()));
resultList.add(entry.getKey().getName().asString() + " = " + value.toString() + typeSuffix);
}
Collections.sort(resultList);
return resultList;
}
@Nullable
public static ClassDescriptor getInnerClassByName(@NotNull ClassDescriptor classDescriptor, @NotNull String innerClassName) {
ClassifierDescriptor classifier = classDescriptor.getDefaultType().getMemberScope().getClassifier(Name.identifier(innerClassName));
@@ -28,6 +28,14 @@ public class AnnotationValue extends CompileTimeConstant<AnnotationDescriptor> {
super(value);
}
@NotNull
@Override
public AnnotationDescriptor getValue() {
AnnotationDescriptor value = super.getValue();
assert value != null : "Guaranteed by constructor";
return value;
}
@Override
@NotNull
public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) {
@@ -17,7 +17,6 @@
package org.jetbrains.jet.lang.resolve.constants;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationArgumentVisitor;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
@@ -33,6 +32,14 @@ public class ArrayValue extends CompileTimeConstant<List<CompileTimeConstant<?>>
this.type = type;
}
@NotNull
@Override
public List<CompileTimeConstant<?>> getValue() {
List<CompileTimeConstant<?>> value = super.getValue();
assert value != null : "Guaranteed by constructor";
return value;
}
@NotNull
@Override
public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) {
@@ -27,6 +27,7 @@ public class JavaClassValue extends CompileTimeConstant<JetType> {
super(value);
}
@NotNull
@Override
public JetType getValue() {
return value.getArguments().iterator().next().getType();
@@ -20,6 +20,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeProjection;
@@ -67,6 +68,9 @@ public interface DescriptorRenderer extends Renderer<DeclarationDescriptor> {
@NotNull
String renderTypeArguments(@NotNull List<TypeProjection> typeArguments);
@NotNull
String renderAnnotation(@NotNull AnnotationDescriptor annotation);
@NotNull
@Override
String render(@NotNull DeclarationDescriptor declarationDescriptor);
@@ -19,12 +19,15 @@ package org.jetbrains.jet.renderer;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.Annotated;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.DefaultAnnotationArgumentVisitor;
import org.jetbrains.jet.lang.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.constants.*;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.FqNameBase;
import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
@@ -34,9 +37,7 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.util.*;
import static org.jetbrains.jet.lang.types.TypeUtils.CANT_INFER_LAMBDA_PARAM_TYPE;
import static org.jetbrains.jet.lang.types.TypeUtils.CANT_INFER_TYPE_PARAMETER;
import static org.jetbrains.jet.lang.types.TypeUtils.DONT_CARE;
import static org.jetbrains.jet.lang.types.TypeUtils.*;
public class DescriptorRendererImpl implements DescriptorRenderer {
private final boolean shortNames;
@@ -90,7 +91,6 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
this.excludedAnnotationClasses = Sets.newHashSet(excludedAnnotationClasses);
}
/* FORMATTING */
@NotNull
private String renderKeyword(@NotNull String keyword) {
@@ -317,15 +317,72 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
assert annotationClass != null;
if (!excludedAnnotationClasses.contains(DescriptorUtils.getFqNameSafe(annotationClass))) {
builder.append(renderType(annotation.getType()));
if (verbose) {
builder.append("(").append(StringUtil.join(DescriptorUtils.getSortedValueArguments(annotation, this), ", ")).append(")");
}
builder.append(" ");
builder.append(renderAnnotation(annotation)).append(" ");
}
}
}
@Override
@NotNull
public String renderAnnotation(@NotNull AnnotationDescriptor annotation) {
StringBuilder sb = new StringBuilder();
sb.append(renderType(annotation.getType()));
if (verbose) {
sb.append("(").append(StringUtil.join(renderAndSortAnnotationArguments(annotation), ", ")).append(")");
}
return sb.toString();
}
@NotNull
private List<String> renderAndSortAnnotationArguments(@NotNull AnnotationDescriptor descriptor) {
List<String> resultList = Lists.newArrayList();
for (Map.Entry<ValueParameterDescriptor, CompileTimeConstant<?>> entry : descriptor.getAllValueArguments().entrySet()) {
CompileTimeConstant<?> value = entry.getValue();
String typeSuffix = ": " + renderType(value.getType(KotlinBuiltIns.getInstance()));
resultList.add(entry.getKey().getName().asString() + " = " + renderConstant(value) + typeSuffix);
}
Collections.sort(resultList);
return resultList;
}
@NotNull
private String renderConstant(@NotNull CompileTimeConstant<?> value) {
return value.accept(
new DefaultAnnotationArgumentVisitor<String, Void>() {
@Override
public String visitValue(@NotNull CompileTimeConstant<?> value, Void data) {
return value.toString();
}
@Override
public String visitArrayValue(ArrayValue value, Void data) {
return "{" +
StringUtil.join(
value.getValue(),
new Function<CompileTimeConstant<?>, String>() {
@Override
public String fun(CompileTimeConstant<?> constant) {
return renderConstant(constant);
}
},
", ") +
"}";
}
@Override
public String visitAnnotationValue(AnnotationValue value, Void data) {
return renderAnnotation(value.getValue());
}
@Override
public String visitJavaClassValue(JavaClassValue value, Void data) {
return renderType(value.getValue()) + ".class";
}
},
null
);
}
private void renderVisibility(@NotNull Visibility visibility, @NotNull StringBuilder builder) {
if (!modifiers.contains(Modifier.VISIBILITY)) return;
if (normalizedVisibilities) {