Introduce PostponedTasks helper class to replace List<Runnable> passed everywhere in jdr

This commit is contained in:
Pavel V. Talanov
2012-10-15 13:50:25 +04:00
parent 49f9905926
commit 9942b66f40
5 changed files with 71 additions and 34 deletions
@@ -135,8 +135,8 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes
}
@Nullable
public ClassDescriptor resolveClass(FqName name, DescriptorSearchRule searchRule, List<Runnable> list) {
return classResolver.resolveClass(name, searchRule, list);
public ClassDescriptor resolveClass(@NotNull FqName name, @NotNull DescriptorSearchRule searchRule, @NotNull PostponedTasks tasks) {
return classResolver.resolveClass(name, searchRule, tasks);
}
public static class ValueParameterDescriptors {
@@ -53,7 +53,8 @@ public final class JavaAnnotationResolver {
this.compileTimeConstResolver = compileTimeConstResolver;
}
public List<AnnotationDescriptor> resolveAnnotations(PsiModifierListOwner owner, @NotNull List<Runnable> tasks) {
@NotNull
public List<AnnotationDescriptor> resolveAnnotations(@NotNull PsiModifierListOwner owner, @NotNull PostponedTasks tasks) {
PsiAnnotation[] psiAnnotations = getAllAnnotations(owner);
List<AnnotationDescriptor> r = Lists.newArrayListWithCapacity(psiAnnotations.length);
for (PsiAnnotation psiAnnotation : psiAnnotations) {
@@ -65,17 +66,16 @@ public final class JavaAnnotationResolver {
return r;
}
public List<AnnotationDescriptor> resolveAnnotations(PsiModifierListOwner owner) {
List<Runnable> tasks = Lists.newArrayList();
List<AnnotationDescriptor> annotations = resolveAnnotations(owner, tasks);
for (Runnable task : tasks) {
task.run();
}
@NotNull
public List<AnnotationDescriptor> resolveAnnotations(@NotNull PsiModifierListOwner owner) {
PostponedTasks postponedTasks = new PostponedTasks();
List<AnnotationDescriptor> annotations = resolveAnnotations(owner, postponedTasks);
postponedTasks.performTasks();
return annotations;
}
@Nullable
public AnnotationDescriptor resolveAnnotation(PsiAnnotation psiAnnotation, @NotNull List<Runnable> taskList) {
public AnnotationDescriptor resolveAnnotation(PsiAnnotation psiAnnotation, @NotNull PostponedTasks postponedTasks) {
final AnnotationDescriptor annotation = new AnnotationDescriptor();
String qname = psiAnnotation.getQualifiedName();
if (qname == null) {
@@ -88,16 +88,16 @@ public final class JavaAnnotationResolver {
}
FqName annotationFqName = new FqName(qname);
final ClassDescriptor clazz =
classResolver.resolveClass(annotationFqName, DescriptorSearchRule.INCLUDE_KOTLIN, taskList);
if (clazz == null) {
final ClassDescriptor annotationClass =
classResolver.resolveClass(annotationFqName, DescriptorSearchRule.INCLUDE_KOTLIN, postponedTasks);
if (annotationClass == null) {
return null;
}
taskList.add(new Runnable() {
postponedTasks.addTask(new Runnable() {
@Override
public void run() {
annotation.setAnnotationType(clazz.getDefaultType());
annotation.setAnnotationType(annotationClass.getDefaultType());
}
});
@@ -111,10 +111,10 @@ public final class JavaAnnotationResolver {
assert value != null;
CompileTimeConstant compileTimeConst =
compileTimeConstResolver.getCompileTimeConstFromExpression(annotationFqName, identifier, value, taskList);
compileTimeConstResolver.getCompileTimeConstFromExpression(annotationFqName, identifier, value, postponedTasks);
if (compileTimeConst != null) {
ValueParameterDescriptor valueParameterDescriptor =
DescriptorResolverUtils.getValueParameterDescriptorForAnnotationParameter(identifier, clazz);
DescriptorResolverUtils.getValueParameterDescriptorForAnnotationParameter(identifier, annotationClass);
if (valueParameterDescriptor != null) {
annotation.setValueArgument(valueParameterDescriptor, compileTimeConst);
}
@@ -123,11 +123,9 @@ public final class JavaClassResolver {
@Nullable
public ClassDescriptor resolveClass(@NotNull FqName qualifiedName, @NotNull DescriptorSearchRule searchRule) {
List<Runnable> tasks = Lists.newArrayList();
ClassDescriptor clazz = resolveClass(qualifiedName, searchRule, tasks);
for (Runnable task : tasks) {
task.run();
}
PostponedTasks postponedTasks = new PostponedTasks();
ClassDescriptor clazz = resolveClass(qualifiedName, searchRule, postponedTasks);
postponedTasks.performTasks();
return clazz;
}
@@ -135,7 +133,7 @@ public final class JavaClassResolver {
public ClassDescriptor resolveClass(
@NotNull FqName qualifiedName,
@NotNull DescriptorSearchRule searchRule,
@NotNull List<Runnable> tasks
@NotNull PostponedTasks tasks
) {
if (isTraitImplementation(qualifiedName)) {
return null;
@@ -162,7 +160,7 @@ public final class JavaClassResolver {
}
@Nullable
private ClassDescriptor doResolveClass(@NotNull FqName qualifiedName, List<Runnable> tasks) {
private ClassDescriptor doResolveClass(@NotNull FqName qualifiedName, @NotNull PostponedTasks tasks) {
PsiClass psiClass = psiClassFinder.findPsiClass(qualifiedName, PsiClassFinder.RuntimeClassesHandleMode.THROW);
if (psiClass == null) {
cacheValue(qualifiedName);
@@ -187,7 +185,7 @@ public final class JavaClassResolver {
@NotNull
private ResolverClassData createJavaClassDescriptor(
@NotNull FqName fqName, @NotNull final PsiClass psiClass,
List<Runnable> taskList
@NotNull PostponedTasks taskList
) {
checkFqNamesAreConsistent(psiClass, fqName);
@@ -207,7 +205,7 @@ public final class JavaClassResolver {
private ResolverClassData doCreateClassDescriptor(
@NotNull FqName fqName,
@NotNull PsiClass psiClass,
@NotNull List<Runnable> taskList,
@NotNull PostponedTasks taskList,
@NotNull ClassOrNamespaceDescriptor containingDeclaration
) {
JetClassAnnotation jetClassAnnotation = JetClassAnnotation.get(psiClass);
@@ -59,29 +59,29 @@ public final class JavaCompileTimeConstResolver {
@Nullable
public CompileTimeConstant<?> getCompileTimeConstFromExpression(
FqName annotationFqName, Name parameterName,
PsiAnnotationMemberValue value, List<Runnable> taskList
PsiAnnotationMemberValue value, PostponedTasks postponedTasks
) {
if (value instanceof PsiLiteralExpression) {
return getCompileTimeConstFromLiteralExpression((PsiLiteralExpression) value);
}
// Enum
else if (value instanceof PsiReferenceExpression) {
return getCompileTimeConstFromReferenceExpression((PsiReferenceExpression) value, taskList);
return getCompileTimeConstFromReferenceExpression((PsiReferenceExpression) value, postponedTasks);
}
// Array
else if (value instanceof PsiArrayInitializerMemberValue) {
return getCompileTimeConstFromArrayExpression(annotationFqName, parameterName, (PsiArrayInitializerMemberValue) value,
taskList);
postponedTasks);
}
// Annotation
else if (value instanceof PsiAnnotation) {
return getCompileTimeConstFromAnnotation((PsiAnnotation) value, taskList);
return getCompileTimeConstFromAnnotation((PsiAnnotation) value, postponedTasks);
}
return null;
}
@Nullable
private CompileTimeConstant<?> getCompileTimeConstFromAnnotation(PsiAnnotation value, List<Runnable> taskList) {
private CompileTimeConstant<?> getCompileTimeConstFromAnnotation(PsiAnnotation value, PostponedTasks taskList) {
AnnotationDescriptor annotationDescriptor = annotationResolver.resolveAnnotation(value, taskList);
if (annotationDescriptor != null) {
return new AnnotationValue(annotationDescriptor);
@@ -93,7 +93,7 @@ public final class JavaCompileTimeConstResolver {
private CompileTimeConstant<?> getCompileTimeConstFromArrayExpression(
FqName annotationFqName,
Name valueName, PsiArrayInitializerMemberValue value,
List<Runnable> taskList
PostponedTasks taskList
) {
PsiAnnotationMemberValue[] initializers = value.getInitializers();
List<CompileTimeConstant<?>> values = getCompileTimeConstantForArrayValues(annotationFqName, valueName, taskList, initializers);
@@ -101,6 +101,7 @@ public final class JavaCompileTimeConstResolver {
ClassDescriptor classDescriptor =
classResolver.resolveClass(annotationFqName, DescriptorSearchRule.INCLUDE_KOTLIN, taskList);
//TODO: nullability issues
ValueParameterDescriptor valueParameterDescriptor =
DescriptorResolverUtils.getValueParameterDescriptorForAnnotationParameter(valueName, classDescriptor);
if (valueParameterDescriptor == null) {
@@ -113,7 +114,7 @@ public final class JavaCompileTimeConstResolver {
private List<CompileTimeConstant<?>> getCompileTimeConstantForArrayValues(
FqName annotationQualifiedName,
Name valueName,
List<Runnable> taskList,
PostponedTasks taskList,
PsiAnnotationMemberValue[] initializers
) {
List<CompileTimeConstant<?>> values = new ArrayList<CompileTimeConstant<?>>();
@@ -129,7 +130,7 @@ public final class JavaCompileTimeConstResolver {
}
@Nullable
private CompileTimeConstant<?> getCompileTimeConstFromReferenceExpression(PsiReferenceExpression value, List<Runnable> taskList) {
private CompileTimeConstant<?> getCompileTimeConstFromReferenceExpression(PsiReferenceExpression value, PostponedTasks taskList) {
PsiElement resolveElement = value.resolve();
if (resolveElement instanceof PsiEnumConstant) {
PsiElement psiElement = resolveElement.getParent();
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2012 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.resolve.java.resolver;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public final class PostponedTasks {
@NotNull
private final List<Runnable> tasks = Lists.newArrayList();
public void addTask(@NotNull Runnable runnable) {
tasks.add(runnable);
}
public void performTasks() {
for (Runnable task : tasks) {
task.run();
}
}
}