Add constructor parameter for compileTimeConstant (can be used in annotation)

This commit is contained in:
Natalia Ukhorskaya
2014-01-23 15:40:11 +04:00
parent fd3f852a93
commit 3f429116e5
37 changed files with 276 additions and 175 deletions
@@ -94,7 +94,7 @@ class LazyJavaAnnotationDescriptor(
private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): CompileTimeConstant<*>? {
return when (argument) {
is JavaLiteralAnnotationArgument -> JavaAnnotationArgumentResolver.resolveCompileTimeConstantValue(argument.getValue(), null)
is JavaLiteralAnnotationArgument -> JavaAnnotationArgumentResolver.resolveCompileTimeConstantValue(argument.getValue(), true, null)
is JavaReferenceAnnotationArgument -> resolveFromReference(argument.resolve())
is JavaArrayAnnotationArgument -> resolveFromArray(argument.getName() ?: DEFAULT_ANNOTATION_MEMBER_NAME, argument.getElements())
is JavaAnnotationAsAnnotationArgument -> resolveFromAnnotation(argument.getAnnotation())
@@ -123,7 +123,7 @@ class LazyJavaAnnotationDescriptor(
val values = elements.map {
argument -> resolveAnnotationArgument(argument) ?: NullValue.NULL
}
return ArrayValue(values, valueParameter.getType())
return ArrayValue(values, valueParameter.getType(), true)
}
private fun resolveFromReference(element: JavaElement?): CompileTimeConstant<*>? {
@@ -111,7 +111,7 @@ public class JavaToKotlinClassMap extends JavaToKotlinClassMapBuilder implements
ValueParameterDescriptor value = DescriptorResolverUtils.getAnnotationParameterByName(
JavaAnnotationResolver.DEFAULT_ANNOTATION_MEMBER_NAME, annotationClass);
assert value != null : "jet.deprecated must have one parameter called value";
annotation.setValueArgument(value, new StringValue("Deprecated in Java"));
annotation.setValueArgument(value, new StringValue("Deprecated in Java", true));
return annotation;
}
@@ -70,7 +70,7 @@ public final class JavaAnnotationArgumentResolver {
@NotNull PostponedTasks postponedTasks
) {
if (argument instanceof JavaLiteralAnnotationArgument) {
return resolveCompileTimeConstantValue(((JavaLiteralAnnotationArgument) argument).getValue(), null);
return resolveCompileTimeConstantValue(((JavaLiteralAnnotationArgument) argument).getValue(), true, null);
}
// Enum
else if (argument instanceof JavaReferenceAnnotationArgument) {
@@ -124,7 +124,7 @@ public final class JavaAnnotationArgumentResolver {
values.add(value == null ? NullValue.NULL : value);
}
return new ArrayValue(values, valueParameter.getType());
return new ArrayValue(values, valueParameter.getType(), true);
}
@Nullable
@@ -167,44 +167,44 @@ public final class JavaAnnotationArgumentResolver {
}
@Nullable
public static CompileTimeConstant<?> resolveCompileTimeConstantValue(@Nullable Object value, @Nullable JetType expectedType) {
public static CompileTimeConstant<?> resolveCompileTimeConstantValue(@Nullable Object value, boolean canBeUseInAnnotation, @Nullable JetType expectedType) {
if (value instanceof String) {
return new StringValue((String) value);
return new StringValue((String) value, canBeUseInAnnotation);
}
else if (value instanceof Byte) {
return new ByteValue((Byte) value);
return new ByteValue((Byte) value, canBeUseInAnnotation);
}
else if (value instanceof Short) {
return new ShortValue((Short) value);
return new ShortValue((Short) value, canBeUseInAnnotation);
}
else if (value instanceof Character) {
return new CharValue((Character) value);
return new CharValue((Character) value, canBeUseInAnnotation);
}
else if (value instanceof Integer) {
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
Integer integer = (Integer) value;
if (builtIns.getShortType().equals(expectedType)) {
return new ShortValue(integer.shortValue());
return new ShortValue(integer.shortValue(), canBeUseInAnnotation);
}
else if (builtIns.getByteType().equals(expectedType)) {
return new ByteValue(integer.byteValue());
return new ByteValue(integer.byteValue(), canBeUseInAnnotation);
}
else if (builtIns.getCharType().equals(expectedType)) {
return new CharValue((char) integer.intValue());
return new CharValue((char) integer.intValue(), canBeUseInAnnotation);
}
return new IntValue(integer);
return new IntValue(integer, canBeUseInAnnotation);
}
else if (value instanceof Long) {
return new LongValue((Long) value);
return new LongValue((Long) value, canBeUseInAnnotation);
}
else if (value instanceof Float) {
return new FloatValue((Float) value);
return new FloatValue((Float) value, canBeUseInAnnotation);
}
else if (value instanceof Double) {
return new DoubleValue((Double) value);
return new DoubleValue((Double) value, canBeUseInAnnotation);
}
else if (value instanceof Boolean) {
return BooleanValue.valueOf((Boolean) value);
return new BooleanValue((Boolean) value, canBeUseInAnnotation);
}
else if (value == null) {
return NullValue.NULL;
@@ -171,7 +171,7 @@ public class AnnotationDescriptorDeserializer implements AnnotationDeserializer
@Override
public void visit(@Nullable Name name, @Nullable Object value) {
if (name != null) {
CompileTimeConstant<?> argument = JavaAnnotationArgumentResolver.resolveCompileTimeConstantValue(value, null);
CompileTimeConstant<?> argument = JavaAnnotationArgumentResolver.resolveCompileTimeConstantValue(value, true, null);
setArgumentValueByName(name, argument != null ? argument : ErrorValue.create("Unsupported annotation argument: " + name));
}
}
@@ -25,7 +25,7 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public class AnnotationValue extends CompileTimeConstant<AnnotationDescriptor> {
public AnnotationValue(@NotNull AnnotationDescriptor value) {
super(value);
super(value, true);
}
@NotNull
@@ -27,8 +27,8 @@ public class ArrayValue extends CompileTimeConstant<List<CompileTimeConstant<?>>
private final JetType type;
public ArrayValue(@NotNull List<CompileTimeConstant<?>> value, @NotNull JetType type) {
super(value);
public ArrayValue(@NotNull List<CompileTimeConstant<?>> value, @NotNull JetType type, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
this.type = type;
}
@@ -23,16 +23,8 @@ import org.jetbrains.jet.lang.types.JetType;
public class BooleanValue extends CompileTimeConstant<Boolean> {
public static final BooleanValue FALSE = new BooleanValue(false);
public static final BooleanValue TRUE = new BooleanValue(true);
private BooleanValue(boolean value) {
super(value);
}
@NotNull
public static BooleanValue valueOf(boolean value) {
return value ? TRUE : FALSE;
public BooleanValue(boolean value, boolean canBeUseInAnnotation) {
super(value, canBeUseInAnnotation);
}
@NotNull
@@ -16,24 +16,15 @@
package org.jetbrains.jet.lang.resolve.constants;
import com.google.common.base.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationArgumentVisitor;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public class ByteValue extends CompileTimeConstant<Byte> {
public static final Function<Long, ByteValue> CREATE = new Function<Long, ByteValue>() {
@Override
public ByteValue apply(@Nullable Long input) {
assert input != null;
return new ByteValue(input.byteValue());
}
};
public ByteValue(byte value) {
super(value);
public ByteValue(byte value, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
}
@NotNull
@@ -23,8 +23,8 @@ import org.jetbrains.jet.lang.types.JetType;
public class CharValue extends CompileTimeConstant<Character> {
public CharValue(char value) {
super(value);
public CharValue(char value, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
}
@NotNull
@@ -24,20 +24,17 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public abstract class CompileTimeConstant<T> {
protected final T value;
private boolean canBeUsedInAnnotations = true;
private final boolean canBeUsedInAnnotations;
protected CompileTimeConstant(T value) {
protected CompileTimeConstant(T value, boolean canBeUsedInAnnotations) {
this.value = value;
this.canBeUsedInAnnotations = canBeUsedInAnnotations;
}
public boolean canBeUsedInAnnotations() {
return canBeUsedInAnnotations;
}
public void setCanBeUsedInAnnotations(boolean canBeUsedInAnnotations) {
this.canBeUsedInAnnotations = canBeUsedInAnnotations;
}
@Nullable
public T getValue() {
return value;
@@ -23,8 +23,8 @@ import org.jetbrains.jet.lang.types.JetType;
public class DoubleValue extends CompileTimeConstant<Double> {
public DoubleValue(double value) {
super(value);
public DoubleValue(double value, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
}
@NotNull
@@ -25,7 +25,7 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public class EnumValue extends CompileTimeConstant<ClassDescriptor> {
public EnumValue(@NotNull ClassDescriptor value) {
super(value);
super(value, true);
}
@NotNull
@@ -25,7 +25,7 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public abstract class ErrorValue extends CompileTimeConstant<Void> {
public ErrorValue() {
super(null);
super(null, true);
}
@Override
@@ -23,8 +23,8 @@ import org.jetbrains.jet.lang.types.JetType;
public class FloatValue extends CompileTimeConstant<Float> {
public FloatValue(float value) {
super(value);
public FloatValue(float value, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
}
@NotNull
@@ -16,24 +16,15 @@
package org.jetbrains.jet.lang.resolve.constants;
import com.google.common.base.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationArgumentVisitor;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public class IntValue extends CompileTimeConstant<Integer> {
public static final Function<Long, IntValue> CREATE = new Function<Long, IntValue>() {
@Override
public IntValue apply(@Nullable Long input) {
assert input != null;
return new IntValue(input.intValue());
}
};
public IntValue(int value) {
super(value);
public IntValue(int value, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
}
@NotNull
@@ -30,8 +30,8 @@ public class IntegerValueTypeConstant extends CompileTimeConstant<Number> {
private final IntegerValueTypeConstructor typeConstructor;
public IntegerValueTypeConstant(@NotNull Number value) {
super(value);
public IntegerValueTypeConstant(@NotNull Number value, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
this.typeConstructor = new IntegerValueTypeConstructor(value.longValue());
}
@@ -23,8 +23,8 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public class JavaClassValue extends CompileTimeConstant<JetType> {
public JavaClassValue(JetType value) {
super(value);
public JavaClassValue(@NotNull JetType value) {
super(value, true);
}
@NotNull
@@ -16,23 +16,15 @@
package org.jetbrains.jet.lang.resolve.constants;
import com.google.common.base.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationArgumentVisitor;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public class LongValue extends CompileTimeConstant<Long> {
public static final Function<Long, LongValue> CREATE = new Function<Long, LongValue>() {
@Override
public LongValue apply(@Nullable Long input) {
return new LongValue(input);
}
};
public LongValue(long value) {
super(value);
public LongValue(long value, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
}
@NotNull
@@ -26,7 +26,7 @@ public class NullValue extends CompileTimeConstant<Void> {
public static final NullValue NULL = new NullValue();
private NullValue() {
super(null);
super(null, false);
}
@NotNull
@@ -16,24 +16,15 @@
package org.jetbrains.jet.lang.resolve.constants;
import com.google.common.base.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationArgumentVisitor;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
public class ShortValue extends CompileTimeConstant<Short> {
public static final Function<Long, ShortValue> CREATE = new Function<Long, ShortValue>() {
@Override
public ShortValue apply(@Nullable Long input) {
assert input != null;
return new ShortValue(input.shortValue());
}
};
public ShortValue(short value) {
super(value);
public ShortValue(short value, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
}
@NotNull
@@ -24,8 +24,8 @@ import org.jetbrains.jet.lang.types.JetType;
public class StringValue extends CompileTimeConstant<String> {
public StringValue(String value) {
super(value);
public StringValue(String value, boolean canBeUsedInAnnotations) {
super(value, canBeUsedInAnnotations);
}
@NotNull