Refactor: predefined annotation become represented as enum, all the logic reside in AnnotationUtils.
This commit is contained in:
@@ -27,6 +27,7 @@ import org.jetbrains.k2js.translate.context.generator.Generator;
|
|||||||
import org.jetbrains.k2js.translate.context.generator.Rule;
|
import org.jetbrains.k2js.translate.context.generator.Rule;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
|
import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
|
||||||
import org.jetbrains.k2js.translate.utils.AnnotationsUtils;
|
import org.jetbrains.k2js.translate.utils.AnnotationsUtils;
|
||||||
|
import org.jetbrains.k2js.translate.utils.PredefinedAnnotation;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -216,15 +217,18 @@ public final class StaticContext {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Rule<JsName> namesAnnotatedAsLibraryHasUnobfuscatableNames = new Rule<JsName>() {
|
Rule<JsName> predefinedObjectsHasUnobfuscatableNames = new Rule<JsName>() {
|
||||||
@Override
|
@Override
|
||||||
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
||||||
if (!isLibraryObject(descriptor)) {
|
for (PredefinedAnnotation annotation : PredefinedAnnotation.values()) {
|
||||||
return null;
|
if (!hasAnnotationOrInsideAnnotatedClass(descriptor, annotation)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String name = getNameForAnnotatedObject(descriptor, annotation);
|
||||||
|
name = (name != null) ? name : descriptor.getName();
|
||||||
|
return getEnclosingScope(descriptor).declareUnobfuscatableName(name);
|
||||||
}
|
}
|
||||||
String name = getNameForAnnotatedObject(descriptor, LIBRARY_ANNOTATION_FQNAME);
|
return null;
|
||||||
name = (name != null) ? name : descriptor.getName();
|
|
||||||
return getEnclosingScope(descriptor).declareUnobfuscatableName(name);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Rule<JsName> propertiesCorrespondToSpeciallyTreatedBackingFieldNames = new Rule<JsName>() {
|
Rule<JsName> propertiesCorrespondToSpeciallyTreatedBackingFieldNames = new Rule<JsName>() {
|
||||||
@@ -254,17 +258,7 @@ public final class StaticContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
Rule<JsName> namesForNativeObjectsAreUnobfuscatable = new Rule<JsName>() {
|
|
||||||
@Override
|
|
||||||
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
|
||||||
if (!isNativeObject(descriptor)) {
|
|
||||||
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>() {
|
Rule<JsName> overridingDescriptorsReferToOriginalName = new Rule<JsName>() {
|
||||||
@Override
|
@Override
|
||||||
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
||||||
@@ -285,8 +279,7 @@ public final class StaticContext {
|
|||||||
};
|
};
|
||||||
addRule(namesForStandardClasses);
|
addRule(namesForStandardClasses);
|
||||||
addRule(constructorHasTheSameNameAsTheClass);
|
addRule(constructorHasTheSameNameAsTheClass);
|
||||||
addRule(namesAnnotatedAsLibraryHasUnobfuscatableNames);
|
addRule(predefinedObjectsHasUnobfuscatableNames);
|
||||||
addRule(namesForNativeObjectsAreUnobfuscatable);
|
|
||||||
addRule(toStringHack);
|
addRule(toStringHack);
|
||||||
addRule(propertiesCorrespondToSpeciallyTreatedBackingFieldNames);
|
addRule(propertiesCorrespondToSpeciallyTreatedBackingFieldNames);
|
||||||
addRule(namespacesShouldBeDefinedInRootScope);
|
addRule(namespacesShouldBeDefinedInRootScope);
|
||||||
@@ -413,27 +406,13 @@ public final class StaticContext {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsNameRef apply(@NotNull DeclarationDescriptor descriptor) {
|
public JsNameRef apply(@NotNull DeclarationDescriptor descriptor) {
|
||||||
if (getAnnotationByName(descriptor, AnnotationsUtils.LIBRARY_ANNOTATION_FQNAME) != null) {
|
if (isLibraryObject(descriptor)) {
|
||||||
return namer.kotlinObject();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Rule<JsNameRef> membersOfAnnotatedClassesHaveKotlinQualifier = new Rule<JsNameRef>() {
|
|
||||||
@Override
|
|
||||||
public JsNameRef apply(@NotNull DeclarationDescriptor descriptor) {
|
|
||||||
ClassDescriptor containingClass = getContainingClass(descriptor);
|
|
||||||
if (containingClass == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (getAnnotationByName(descriptor, LIBRARY_ANNOTATION_FQNAME) != null) {
|
|
||||||
return namer.kotlinObject();
|
return namer.kotlinObject();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
addRule(libraryObjectsHaveKotlinQualifier);
|
addRule(libraryObjectsHaveKotlinQualifier);
|
||||||
addRule(membersOfAnnotatedClassesHaveKotlinQualifier);
|
|
||||||
addRule(constructorHaveTheSameQualifierAsTheClass);
|
addRule(constructorHaveTheSameQualifierAsTheClass);
|
||||||
addRule(namespacesHaveNoQualifiers);
|
addRule(namespacesHaveNoQualifiers);
|
||||||
addRule(namespaceLevelDeclarationsHaveEnclosingNamespacesNamesAsQualifier);
|
addRule(namespaceLevelDeclarationsHaveEnclosingNamespacesNamesAsQualifier);
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ public final class Translation {
|
|||||||
StaticContext staticContext = StaticContext.generateStaticContext(standardLibrary, bindingContext);
|
StaticContext staticContext = StaticContext.generateStaticContext(standardLibrary, bindingContext);
|
||||||
JsBlock block = staticContext.getProgram().getFragmentBlock(0);
|
JsBlock block = staticContext.getProgram().getFragmentBlock(0);
|
||||||
TranslationContext context = TranslationContext.rootContext(staticContext);
|
TranslationContext context = TranslationContext.rootContext(staticContext);
|
||||||
block.getStatements().addAll(Translation.translateFiles(files, context));
|
block.getStatements().addAll(translateFiles(files, context));
|
||||||
JsNamer namer = new JsPrettyNamer();
|
JsNamer namer = new JsPrettyNamer();
|
||||||
namer.exec(context.program());
|
namer.exec(context.program());
|
||||||
return context.program();
|
return context.program();
|
||||||
|
|||||||
@@ -31,25 +31,18 @@ import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getContainingCl
|
|||||||
*/
|
*/
|
||||||
public final class AnnotationsUtils {
|
public final class AnnotationsUtils {
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static final String NATIVE_ANNOTATION_FQNAME = "js.native";
|
|
||||||
@NotNull
|
|
||||||
public static final String LIBRARY_ANNOTATION_FQNAME = "js.library";
|
|
||||||
|
|
||||||
private AnnotationsUtils() {
|
private AnnotationsUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: make public, use when necessary
|
|
||||||
private static boolean hasAnnotation(@NotNull DeclarationDescriptor descriptor,
|
private static boolean hasAnnotation(@NotNull DeclarationDescriptor descriptor,
|
||||||
@NotNull String annotationFQNAme) {
|
@NotNull PredefinedAnnotation annotation) {
|
||||||
return getAnnotationByName(descriptor, annotationFQNAme) != null;
|
return getAnnotationByName(descriptor, annotation) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static String getAnnotationStringParameter(@NotNull DeclarationDescriptor declarationDescriptor,
|
public static String getAnnotationStringParameter(@NotNull DeclarationDescriptor declarationDescriptor,
|
||||||
@NotNull String annotationFQName) {
|
@NotNull PredefinedAnnotation annotation) {
|
||||||
AnnotationDescriptor annotationDescriptor =
|
AnnotationDescriptor annotationDescriptor = getAnnotationByName(declarationDescriptor, annotation);
|
||||||
getAnnotationByName(declarationDescriptor, annotationFQName);
|
|
||||||
assert annotationDescriptor != null;
|
assert annotationDescriptor != null;
|
||||||
//TODO: this is a quick fix for unsupported default args problem
|
//TODO: this is a quick fix for unsupported default args problem
|
||||||
if (annotationDescriptor.getValueArguments().isEmpty()) {
|
if (annotationDescriptor.getValueArguments().isEmpty()) {
|
||||||
@@ -67,19 +60,19 @@ public final class AnnotationsUtils {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static String getNameForAnnotatedObject(@NotNull DeclarationDescriptor declarationDescriptor,
|
public static String getNameForAnnotatedObject(@NotNull DeclarationDescriptor declarationDescriptor,
|
||||||
@NotNull String annotationFQName) {
|
@NotNull PredefinedAnnotation annotation) {
|
||||||
if (!hasAnnotation(declarationDescriptor, annotationFQName)) {
|
if (!hasAnnotation(declarationDescriptor, annotation)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return getAnnotationStringParameter(declarationDescriptor, annotationFQName);
|
return getAnnotationStringParameter(declarationDescriptor, annotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static AnnotationDescriptor getAnnotationByName(@NotNull DeclarationDescriptor descriptor,
|
public static AnnotationDescriptor getAnnotationByName(@NotNull DeclarationDescriptor descriptor,
|
||||||
@NotNull String FQName) {
|
@NotNull PredefinedAnnotation annotation) {
|
||||||
for (AnnotationDescriptor annotationDescriptor : descriptor.getAnnotations()) {
|
for (AnnotationDescriptor annotationDescriptor : descriptor.getAnnotations()) {
|
||||||
String annotationClassFQName = getAnnotationClassFQName(annotationDescriptor);
|
String annotationClassFQName = getAnnotationClassFQName(annotationDescriptor);
|
||||||
if (annotationClassFQName.equals(FQName)) {
|
if (annotationClassFQName.equals(annotation.getFQName())) {
|
||||||
return annotationDescriptor;
|
return annotationDescriptor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,26 +88,31 @@ public final class AnnotationsUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isNativeObject(@NotNull DeclarationDescriptor descriptor) {
|
public static boolean isNativeObject(@NotNull DeclarationDescriptor descriptor) {
|
||||||
return hasAnnotationOrInsideAnnotatedClass(descriptor, NATIVE_ANNOTATION_FQNAME);
|
return hasAnnotationOrInsideAnnotatedClass(descriptor, PredefinedAnnotation.NATIVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isLibraryObject(@NotNull DeclarationDescriptor descriptor) {
|
public static boolean isLibraryObject(@NotNull DeclarationDescriptor descriptor) {
|
||||||
return hasAnnotationOrInsideAnnotatedClass(descriptor, LIBRARY_ANNOTATION_FQNAME);
|
return hasAnnotationOrInsideAnnotatedClass(descriptor, PredefinedAnnotation.LIBRARY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isPredefinedObject(@NotNull DeclarationDescriptor descriptor) {
|
public static boolean isPredefinedObject(@NotNull DeclarationDescriptor descriptor) {
|
||||||
return isLibraryObject(descriptor) || isNativeObject(descriptor);
|
for (PredefinedAnnotation annotation : PredefinedAnnotation.values()) {
|
||||||
|
if (hasAnnotationOrInsideAnnotatedClass(descriptor, annotation)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean hasAnnotationOrInsideAnnotatedClass(@NotNull DeclarationDescriptor descriptor,
|
public static boolean hasAnnotationOrInsideAnnotatedClass(@NotNull DeclarationDescriptor descriptor,
|
||||||
@NotNull String annotationFQName) {
|
@NotNull PredefinedAnnotation annotation) {
|
||||||
if (getAnnotationByName(descriptor, annotationFQName) != null) {
|
if (getAnnotationByName(descriptor, annotation) != null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
ClassDescriptor containingClass = getContainingClass(descriptor);
|
ClassDescriptor containingClass = getContainingClass(descriptor);
|
||||||
if (containingClass == null) {
|
if (containingClass == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return (getAnnotationByName(containingClass, annotationFQName) != null);
|
return (getAnnotationByName(containingClass, annotation) != null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2000-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.k2js.translate.utils;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Pavel Talanov
|
||||||
|
*/
|
||||||
|
public enum PredefinedAnnotation {
|
||||||
|
|
||||||
|
|
||||||
|
LIBRARY("js.library"),
|
||||||
|
NATIVE("js.native");
|
||||||
|
|
||||||
|
PredefinedAnnotation(@NotNull String fqName) {
|
||||||
|
this.fqName = fqName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String fqName;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public String getFQName() {
|
||||||
|
return fqName;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user