Only create lazy annotations for descriptors from code

This commit is contained in:
Andrey Breslav
2014-04-02 13:53:08 +04:00
parent c57243035e
commit bb87a6bb59
4 changed files with 45 additions and 77 deletions
@@ -359,9 +359,10 @@ public class ConstantExpressionEvaluator private (val trace: BindingTrace) : Jet
if (resultingDescriptor is ConstructorDescriptor) {
val classDescriptor: ClassDescriptor = resultingDescriptor.getContainingDeclaration()
if (DescriptorUtils.isAnnotationClass(classDescriptor)) {
val descriptor = AnnotationDescriptorImpl()
descriptor.setAnnotationType(classDescriptor.getDefaultType())
AnnotationResolver.resolveAnnotationArguments(descriptor, call, trace)
val descriptor = AnnotationDescriptorImpl(
classDescriptor.getDefaultType(),
AnnotationResolver.resolveAnnotationArguments(call, trace)
)
return AnnotationValue(descriptor)
}
}
@@ -23,7 +23,10 @@ import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.*;
import org.jetbrains.jet.lang.descriptors.annotations.Annotated;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationsImpl;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.evaluate.ConstantExpressionEvaluator;
import org.jetbrains.jet.lang.psi.*;
@@ -48,6 +51,7 @@ import org.jetbrains.jet.storage.StorageManager;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -128,24 +132,17 @@ public class AnnotationResolver {
for (JetAnnotationEntry entryElement : annotationEntryElements) {
AnnotationDescriptor descriptor = trace.get(BindingContext.ANNOTATION, entryElement);
if (descriptor == null) {
if (TopDownAnalyzer.LAZY) {
descriptor = new LazyAnnotationDescriptor(
new LazyAnnotationsContext(this, storageManager, trace) {
descriptor = new LazyAnnotationDescriptor(
new LazyAnnotationsContext(this, storageManager, trace) {
@NotNull
@Override
public JetScope getScope() {
return scope;
}
},
entryElement
);
}
else {
descriptor = new AnnotationDescriptorImpl();
((AnnotationDescriptorImpl) descriptor).setAnnotationType(resolveAnnotationType(scope, entryElement));
trace.record(BindingContext.ANNOTATION, entryElement, descriptor);
}
@NotNull
@Override
public JetScope getScope() {
return scope;
}
},
entryElement
);
}
if (shouldResolveArguments) {
resolveAnnotationArguments(entryElement, scope, trace);
@@ -220,7 +217,7 @@ public class AnnotationResolver {
}
}
private void resolveAnnotationArguments(
private static void resolveAnnotationArguments(
@NotNull JetAnnotationEntry annotationEntry,
@NotNull JetScope scope,
@NotNull BindingTrace trace
@@ -228,47 +225,26 @@ public class AnnotationResolver {
AnnotationDescriptor annotationDescriptor = trace.getBindingContext().get(BindingContext.ANNOTATION, annotationEntry);
assert annotationDescriptor != null : "Annotation descriptor should be created before resolving arguments for " + annotationEntry.getText();
if (annotationDescriptor instanceof LazyAnnotationDescriptor) {
// TopDownAnalyzer.LAZY
((LazyAnnotationDescriptor) annotationDescriptor).forceResolveAllContents();
return;
}
AnnotationDescriptorImpl annotationDescriptorImpl = (AnnotationDescriptorImpl) annotationDescriptor;
if (annotationDescriptorImpl.areValueArgumentsResolved()) return;
OverloadResolutionResults<FunctionDescriptor> results = resolveAnnotationCall(annotationEntry, scope, trace);
if (results.isSingleResult()) {
checkAnnotationType(annotationEntry, trace, results);
resolveAnnotationArguments(annotationDescriptor, results.getResultingCall(), trace);
}
else {
annotationDescriptorImpl.markValueArgumentsResolved();
}
}
public static void resolveAnnotationArguments(
@NotNull AnnotationDescriptor annotationDescriptor,
@NotNull
public static Map<ValueParameterDescriptor, CompileTimeConstant<?>> resolveAnnotationArguments(
@NotNull ResolvedCall<?> resolvedCall,
@NotNull BindingTrace trace
) {
if (TopDownAnalyzer.LAZY && annotationDescriptor instanceof LazyAnnotationDescriptor) {
((LazyAnnotationDescriptor) annotationDescriptor).forceResolveAllContents();
return;
}
AnnotationDescriptorImpl annotationDescriptorImpl = (AnnotationDescriptorImpl) annotationDescriptor;
Map<ValueParameterDescriptor, CompileTimeConstant<?>> arguments = new HashMap<ValueParameterDescriptor, CompileTimeConstant<?>>();
for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> descriptorToArgument : resolvedCall.getValueArguments().entrySet()) {
ValueParameterDescriptor parameterDescriptor = descriptorToArgument.getKey();
ResolvedValueArgument resolvedArgument = descriptorToArgument.getValue();
CompileTimeConstant<?> value = getAnnotationArgumentValue(trace, parameterDescriptor, resolvedArgument);
if (value != null) {
annotationDescriptorImpl.setValueArgument(parameterDescriptor, value);
arguments.put(parameterDescriptor, value);
}
}
annotationDescriptorImpl.markValueArgumentsResolved();
return arguments;
}
@Nullable
@@ -42,7 +42,9 @@ import org.jetbrains.jet.lang.types.ErrorUtils;
import javax.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.jetbrains.jet.descriptors.serialization.descriptors.Deserializers.AnnotatedCallableKind;
import static org.jetbrains.jet.lang.resolve.kotlin.DescriptorDeserializersStorage.MemberSignature;
@@ -119,10 +121,10 @@ public class AnnotationDescriptorDeserializer extends BaseDescriptorDeserializer
if (JvmAnnotationNames.isSpecialAnnotation(className)) return null;
final ClassDescriptor annotationClass = resolveClass(className, classResolver);
final AnnotationDescriptorImpl annotation = new AnnotationDescriptorImpl();
annotation.setAnnotationType(annotationClass.getDefaultType());
return new KotlinJvmBinaryClass.AnnotationArgumentVisitor() {
private final Map<ValueParameterDescriptor, CompileTimeConstant<?>> arguments = new HashMap<ValueParameterDescriptor, CompileTimeConstant<?>>();
@Override
public void visit(@Nullable Name name, @Nullable Object value) {
if (name != null) {
@@ -157,14 +159,16 @@ public class AnnotationDescriptorDeserializer extends BaseDescriptorDeserializer
@Override
public void visitEnd() {
annotation.markValueArgumentsResolved();
result.add(annotation);
result.add(new AnnotationDescriptorImpl(
annotationClass.getDefaultType(),
arguments
));
}
private void setArgumentValueByName(@NotNull Name name, @NotNull CompileTimeConstant<?> argumentValue) {
ValueParameterDescriptor parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass);
if (parameter != null) {
annotation.setValueArgument(parameter, argumentValue);
arguments.put(parameter, argumentValue);
}
}
};
@@ -16,7 +16,6 @@
package org.jetbrains.jet.lang.descriptors.annotations;
import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
@@ -28,9 +27,16 @@ import java.util.Collections;
import java.util.Map;
public class AnnotationDescriptorImpl implements AnnotationDescriptor {
private JetType annotationType;
private final Map<ValueParameterDescriptor, CompileTimeConstant<?>> valueArguments = Maps.newHashMap();
private boolean valueArgumentsResolved = false;
private final JetType annotationType;
private final Map<ValueParameterDescriptor, CompileTimeConstant<?>> valueArguments;
public AnnotationDescriptorImpl(
@NotNull JetType annotationType,
@NotNull Map<ValueParameterDescriptor, CompileTimeConstant<?>> valueArguments
) {
this.annotationType = annotationType;
this.valueArguments = Collections.unmodifiableMap(valueArguments);
}
@Override
@NotNull
@@ -47,26 +53,7 @@ public class AnnotationDescriptorImpl implements AnnotationDescriptor {
@Override
@NotNull
public Map<ValueParameterDescriptor, CompileTimeConstant<?>> getAllValueArguments() {
// TODO: this assertion does not hold now, but this whole class will be gone before long, so I'm not fixing it
//assert valueArgumentsResolved : "Value arguments are not resolved yet for [" + getType() + "]";
return Collections.unmodifiableMap(valueArguments);
}
public void setAnnotationType(@NotNull JetType annotationType) {
this.annotationType = annotationType;
}
public void setValueArgument(@NotNull ValueParameterDescriptor name, @NotNull CompileTimeConstant<?> value) {
assert !valueArgumentsResolved : "Value arguments are already resolved for " + this;
valueArguments.put(name, value);
}
public void markValueArgumentsResolved() {
this.valueArgumentsResolved = true;
}
public boolean areValueArgumentsResolved() {
return valueArgumentsResolved;
return valueArguments;
}
@Override