Refactor AnnotationUtils and NameGenerator

This commit is contained in:
pTalanov
2012-02-29 20:18:10 +04:00
parent 191803f3f6
commit adcffef920
3 changed files with 27 additions and 45 deletions
@@ -21,7 +21,6 @@ import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.JetStandardLibrary;
import org.jetbrains.k2js.translate.context.generator.Generator;
@@ -90,7 +89,7 @@ public final class StaticContext {
private final Map<NamingScope, JsFunction> scopeToFunction = Maps.newHashMap();
//TODO: too many parameters in constructor
//qTODO: too many parameters in constructor
private StaticContext(@NotNull JsProgram program, @NotNull BindingContext bindingContext,
@NotNull Aliaser aliaser,
@NotNull Namer namer, @NotNull Intrinsics intrinsics,
@@ -220,24 +219,12 @@ public final class StaticContext {
Rule<JsName> namesAnnotatedAsLibraryHasUnobfuscatableNames = new Rule<JsName>() {
@Override
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
//TODO: refactor
String name = null;
AnnotationDescriptor annotation = getAnnotationByName(descriptor, LIBRARY_ANNOTATION_FQNAME);
if (annotation != null) {
name = AnnotationsUtils.getAnnotationStringParameter(descriptor, LIBRARY_ANNOTATION_FQNAME);
name = (!name.isEmpty()) ? name : descriptor.getName();
if (!isLibraryObject(descriptor)) {
return null;
}
else {
ClassDescriptor containingClass = getContainingClass(descriptor);
if (containingClass == null) return null;
if (getAnnotationByName(containingClass, LIBRARY_ANNOTATION_FQNAME) != null) {
name = descriptor.getName();
}
}
if (name != null) {
return getEnclosingScope(descriptor).declareUnobfuscatableName(name);
}
return null;
String name = getNameForAnnotatedObject(descriptor, LIBRARY_ANNOTATION_FQNAME);
name = (name != null) ? name : descriptor.getName();
return getEnclosingScope(descriptor).declareUnobfuscatableName(name);
}
};
Rule<JsName> propertiesCorrespondToSpeciallyTreatedBackingFieldNames = new Rule<JsName>() {
@@ -270,24 +257,12 @@ public final class StaticContext {
Rule<JsName> namesForNativeObjectsAreUnobfuscatable = new Rule<JsName>() {
@Override
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
String name = null;
AnnotationDescriptor annotation = getAnnotationByName(descriptor, NATIVE_ANNOTATION_FQNAME);
if (annotation != null) {
name = AnnotationsUtils.getAnnotationStringParameter(descriptor, NATIVE_ANNOTATION_FQNAME);
name = (!name.isEmpty()) ? name : descriptor.getName();
if (!isNativeObject(descriptor)) {
return null;
}
else {
ClassDescriptor containingClass = getContainingClass(descriptor);
if (containingClass == null) return null;
if (getAnnotationByName(containingClass, NATIVE_ANNOTATION_FQNAME) != null) {
name = descriptor.getName();
}
}
if (name != null) {
return getEnclosingScope(descriptor).declareUnobfuscatableName(name);
}
return null;
String name = getNameForAnnotatedObject(descriptor, NATIVE_ANNOTATION_FQNAME);
name = (name != null) ? name : descriptor.getName();
return getEnclosingScope(descriptor).declareUnobfuscatableName(name);
}
};
Rule<JsName> overridingDescriptorsReferToOriginalName = new Rule<JsName>() {
@@ -302,9 +277,7 @@ public final class StaticContext {
else {
//assert overriddenDescriptors.size() == 1;
//TODO: for now translator can't deal with multiple inheritance good enough
for (FunctionDescriptor overriddenDescriptor : overriddenDescriptors) {
return getNameForDescriptor(overriddenDescriptor);
}
return getNameForDescriptor(overriddenDescriptors.iterator().next());
}
}
return null;
@@ -45,7 +45,7 @@ public final class AnnotationsUtils {
return getAnnotationByName(descriptor, annotationFQNAme) != null;
}
@NotNull
@Nullable
public static String getAnnotationStringParameter(@NotNull DeclarationDescriptor declarationDescriptor,
@NotNull String annotationFQName) {
AnnotationDescriptor annotationDescriptor =
@@ -53,18 +53,27 @@ public final class AnnotationsUtils {
assert annotationDescriptor != null;
//TODO: this is a quick fix for unsupported default args problem
if (annotationDescriptor.getValueArguments().isEmpty()) {
return "";
return null;
}
CompileTimeConstant<?> constant = annotationDescriptor.getValueArguments().iterator().next();
//TODO: this is a quick fix for unsupported default args problem
if (constant == null) {
return "";
return null;
}
Object value = constant.getValue();
assert value instanceof String : "Native function annotation should have one String parameter";
return (String) value;
}
@Nullable
public static String getNameForAnnotatedObject(@NotNull DeclarationDescriptor declarationDescriptor,
@NotNull String annotationFQName) {
if (!hasAnnotation(declarationDescriptor, annotationFQName)) {
return null;
}
return getAnnotationStringParameter(declarationDescriptor, annotationFQName);
}
@Nullable
public static AnnotationDescriptor getAnnotationByName(@NotNull DeclarationDescriptor descriptor,
@NotNull String FQName) {
@@ -93,12 +102,12 @@ public final class AnnotationsUtils {
return hasAnnotationOrInsideAnnotatedClass(descriptor, LIBRARY_ANNOTATION_FQNAME);
}
//TODO: the use of this method is splattered across the code which can be hard to track
public static boolean isPredefinedObject(@NotNull DeclarationDescriptor descriptor) {
return isLibraryObject(descriptor) || isNativeObject(descriptor);
}
private static boolean hasAnnotationOrInsideAnnotatedClass(DeclarationDescriptor descriptor, String annotationFQName) {
public static boolean hasAnnotationOrInsideAnnotatedClass(@NotNull DeclarationDescriptor descriptor,
@NotNull String annotationFQName) {
if (getAnnotationByName(descriptor, annotationFQName) != null) {
return true;
}
@@ -110,7 +110,7 @@ public final class BindingUtils {
if (descriptor instanceof NamespaceDescriptor) {
continue;
}
JetDeclaration declaration = BindingUtils.getDeclarationForDescriptor(bindingContext, descriptor);
JetDeclaration declaration = getDeclarationForDescriptor(bindingContext, descriptor);
if (declaration != null) {
declarations.add(declaration);
}