Repository: flex-falcon Updated Branches: refs/heads/develop 3870092bb -> 210535099
http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/21053509/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java index ccceab0..4fa7d89 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ReferenceModel.java @@ -19,439 +19,242 @@ package org.apache.flex.compiler.internal.codegen.externals.reference; -import java.io.File; -import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; -import java.util.Map.Entry; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.FilenameUtils; -import org.apache.flex.compiler.internal.codegen.externals.pass.AddMemberPass; -import org.apache.flex.compiler.internal.codegen.externals.pass.CollectTypesPass; -import org.apache.flex.utils.FilenameNormalization; +import org.apache.flex.compiler.internal.codegen.externals.ExternalsClientConfig; +import org.apache.flex.compiler.internal.codegen.externals.ExternalsClientConfig.ExcludedMemeber; -import com.google.common.collect.ImmutableList; -import com.google.javascript.jscomp.CustomPassExecutionTime; -import com.google.javascript.jscomp.JXCompilerOptions; +import com.google.javascript.jscomp.Compiler; import com.google.javascript.jscomp.NodeUtil; -import com.google.javascript.jscomp.Result; -import com.google.javascript.jscomp.SourceFile; import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.Node; +import com.google.javascript.rhino.jstype.JSType; public class ReferenceModel { - private static final List<SourceFile> EMPTY_EXTERNS = ImmutableList.of(SourceFile.fromCode( - "externs", "")); - - private File asRoot; - private File asFunctionRoot; - private File asConstantRoot; - - private List<ExcludedMemeber> excludesClass = new ArrayList<ExcludedMemeber>(); - private List<ExcludedMemeber> excludesField = new ArrayList<ExcludedMemeber>(); - private List<ExcludedMemeber> excludes = new ArrayList<ExcludedMemeber>(); - private List<ExternalFile> externals = new ArrayList<ExternalFile>(); + private ExternalsClientConfig configuration; + private Compiler compiler; + private List<String> namespaces = new ArrayList<String>(); + private HashMap<String, ClassReference> typedefs = new HashMap<String, ClassReference>(); private HashMap<String, ClassReference> classes = new HashMap<String, ClassReference>(); private HashMap<String, FunctionReference> functions = new HashMap<String, FunctionReference>(); private HashMap<String, ConstantReference> constants = new HashMap<String, ConstantReference>(); - private com.google.javascript.jscomp.Compiler compiler; - - public void setASRoot(File file) + public Compiler getCompiler() { - this.asRoot = file; - - asFunctionRoot = new File(asRoot.getParent(), "as_functions"); - asConstantRoot = new File(asRoot.getParent(), "as_constants"); + return compiler; } - public com.google.javascript.jscomp.Compiler getCompiler() + public void setCompiler(Compiler compiler) { - return compiler; + this.compiler = compiler; } - public ReferenceModel() + public ExternalsClientConfig getConfiguration() { - //walker = new JSBlockWalker(this); + return configuration; } - public void addExclude(String className, String name) + public Collection<String> getNamespaces() { - excludes.add(new ExcludedMemeber(className, name)); + return namespaces; } - public void addExclude(String className, String name, String description) + public Collection<ClassReference> getTypedefs() { - excludes.add(new ExcludedMemeber(className, name, description)); + return typedefs.values(); } - public void addFieldExclude(String className, String fieldName) + public Collection<ClassReference> getClasses() { - excludesField.add(new ExcludedMemeber(className, fieldName, "")); + return classes.values(); } - public void addClassExclude(String className) + public Collection<FunctionReference> getFunctions() { - excludesClass.add(new ExcludedMemeber(className, null, "")); + return functions.values(); } - public void addExternal(File file) throws IOException + public Collection<ConstantReference> getConstants() { - if (!file.exists()) - throw new IOException(file.getAbsolutePath() + " does not exist."); - externals.add(new ExternalFile(file)); + return constants.values(); } - public void addExternal(String externalFile) throws IOException + public ReferenceModel(ExternalsClientConfig config) { - addExternal(new File(FilenameNormalization.normalize(externalFile))); + this.configuration = config; } - // public void addExternal(String name) - // { - // File file = new File(jsRoot, name + ".js"); - // externals.add(new ExternalFile(file)); - // } - - public ClassReference getClassReference(String qualifiedName) + public ClassReference getClassReference(String qName) { - return classes.get(qualifiedName); + return classes.get(qName); } - public void addClass(Node node, String qualfiedName) + public void addNamespace(Node node, String qName) { - if (classes.containsKey(qualfiedName)) + if (namespaces.contains(qName)) { // XXX Record warning; return; } - System.out.println("Model.addClass(" + qualfiedName + ")"); + System.out.println("Model.addNamespace(" + qName + ")"); - ClassReference reference = new ClassReference(this, node, qualfiedName, - node.getJSDocInfo()); - classes.put(qualfiedName, reference); + namespaces.add(qName); } - public void addInterface(Node node, String qualfiedName) + public void addClass(Node node, String qName) { - if (classes.containsKey(qualfiedName)) + if (classes.containsKey(qName)) { // XXX Record warning; return; } - System.out.println("Model.addInterface(" + qualfiedName + ")"); + System.out.println("Model.addClass(" + qName + ")"); - ClassReference reference = new ClassReference(this, node, qualfiedName, + ClassReference reference = new ClassReference(this, node, qName, node.getJSDocInfo()); - classes.put(qualfiedName, reference); + classes.put(qName, reference); } - public void addFinalClass(Node node, String qualfiedName) + public void addTypeDef(Node node, String qName) { - if (classes.containsKey(qualfiedName)) + if (typedefs.containsKey(qName)) { // XXX Record warning; return; } - System.out.println("Model.addFinalClass(" + qualfiedName + ")"); + System.out.println("Model.addTypeDef(" + qName + ")"); - ClassReference reference = new ClassReference(this, node, qualfiedName, + ClassReference reference = new ClassReference(this, node, qName, node.getJSDocInfo()); - reference.setFinal(true); - classes.put(qualfiedName, reference); + typedefs.put(qName, reference); } - public void addFunction(Node node, String qualfiedName) + public void addInterface(Node node, String qName) { - if (functions.containsKey(qualfiedName)) + if (classes.containsKey(qName)) { // XXX Record warning; return; } - System.out.println("Model.addFunction(" + qualfiedName + ")"); - //System.err.println(node.toStringTree()); - FunctionReference reference = new FunctionReference(this, node, - qualfiedName, node.getJSDocInfo()); - functions.put(qualfiedName, reference); + System.out.println("Model.addInterface(" + qName + ")"); + + ClassReference reference = new ClassReference(this, node, qName, + node.getJSDocInfo()); + classes.put(qName, reference); } - public void addConstant(Node node, String qualfiedName) + public void addFinalClass(Node node, String qName) { - if (constants.containsKey(qualfiedName)) + if (classes.containsKey(qName)) { // XXX Record warning; return; } - System.out.println("Model.addConstant(" + qualfiedName + ")"); + System.out.println("Model.addFinalClass(" + qName + ")"); - ConstantReference reference = new ConstantReference(this, node, - qualfiedName, node.getJSDocInfo()); - constants.put(qualfiedName, reference); - } - - public void addField(Node node, String className, String qualfiedName) - { - ClassReference classReference = getClassReference(className); - classReference.addField(node, qualfiedName, node.getJSDocInfo(), false); + ClassReference reference = new ClassReference(this, node, qName, + node.getJSDocInfo()); + reference.setFinal(true); + classes.put(qName, reference); } - public void addStaticField(Node node, String className, String qualfiedName) + public void addFunction(Node node, String qName) { - ClassReference classReference = getClassReference(className); - // XXX this is here because for now, the doc might be on the parent ASSIGN node - // if it's a static property with a value - JSDocInfo comment = NodeUtil.getBestJSDocInfo(node); - if (classReference != null) + if (functions.containsKey(qName)) { - classReference.addField(node, qualfiedName, comment, true); - } - else - { - System.err.println(">>>> {ReferenceModel} Class [" + className - + "] not found in " + node.getSourceFileName()); + // XXX Record warning; + return; } - } - //---------------------------------------------------- + System.out.println("Model.addFunction(" + qName + ")"); + //System.err.println(node.toStringTree()); + FunctionReference reference = new FunctionReference(this, node, qName, + node.getJSDocInfo()); + functions.put(qName, reference); + } - public void cleanOutput() throws IOException + public boolean hasConstant(String qName) { - FileUtils.deleteDirectory(asRoot); + return constants.containsKey(qName); } - public void emit() throws IOException + public void addConstant(Node node, String qName) { - asRoot.mkdirs(); - - for (Entry<String, ClassReference> set : classes.entrySet()) + if (constants.containsKey(qName)) { - StringBuilder sb = new StringBuilder(); - - ClassReference reference = set.getValue(); - if (isExcludedClass(reference) != null) - continue; - - if (!reference.isInterface()) - continue; - - emit(reference, sb); - - File sourceFile = reference.getFile(asRoot); - FileUtils.write(sourceFile, sb.toString()); + // XXX Record warning; + return; } - for (Entry<String, ClassReference> set : classes.entrySet()) - { - ClassReference reference = set.getValue(); - if (isExcludedClass(reference) != null) - continue; - - if (reference.isInterface()) - continue; + System.out.println("Model.addConstant(" + qName + ")"); - StringBuilder sb = new StringBuilder(); - - emit(reference, sb); - - File sourceFile = reference.getFile(asRoot); - FileUtils.write(sourceFile, sb.toString()); - } + ConstantReference reference = new ConstantReference(this, node, qName, + node.getJSDocInfo()); + constants.put(qName, reference); + } - for (Entry<String, FunctionReference> set : functions.entrySet()) + public void addConstantType(Node node, String qName, JSType type) + { + if (constants.containsKey(qName)) { - StringBuilder sb = new StringBuilder(); - - FunctionReference reference = set.getValue(); - emit(reference, sb); - - File sourceFile = reference.getFile(asFunctionRoot); - FileUtils.write(sourceFile, sb.toString()); + // XXX Record warning; + return; } - for (Entry<String, ConstantReference> set : constants.entrySet()) - { - StringBuilder sb = new StringBuilder(); - - ConstantReference reference = set.getValue(); - emit(reference, sb); + System.out.println("Model.addConstantType(" + qName + ")"); - File sourceFile = reference.getFile(asConstantRoot); - FileUtils.write(sourceFile, sb.toString()); - } - - // StringBuilder sb = new StringBuilder(); - // sb.append("package {\n"); - // for (Entry<String, ConstantReference2> set : constants.entrySet()) - // { - // ConstantReference2 reference = set.getValue(); - // emit(reference, sb); - // } - // sb.append("\n}"); - // File sourceFile = new File(asRoot, "constants.as"); - // FileUtils.write(sourceFile, sb.toString()); + ConstantReference reference = new ConstantReference(this, node, qName, + node.getJSDocInfo(), type); + constants.put(qName, reference); } - public void emit(BaseReference reference, StringBuilder sb) - { - reference.emit(sb); - } - - public String emit(BaseReference reference) + public void addField(Node node, String className, String qualfiedName) { - StringBuilder sb = new StringBuilder(); - reference.emit(sb); - return sb.toString(); + ClassReference classReference = getClassReference(className); + if (classReference != null) + classReference.addField(node, qualfiedName, node.getJSDocInfo(), + false); } - public void compile() throws IOException + public void addStaticField(Node node, String className, String qualfiedName) { - JXCompilerOptions options = new JXCompilerOptions(); - //options.setLanguageIn(LanguageMode.ECMASCRIPT6_TYPED); - //options.setLanguageOut(LanguageMode.ECMASCRIPT6_TYPED); - options.setPreserveTypeAnnotations(true); - options.setPrettyPrint(true); - options.setLineLengthThreshold(80); - options.setPreferSingleQuotes(true); - options.setIdeMode(true); - options.setParseJsDocDocumentation(true); - options.setExternExports(false); - - compiler = new com.google.javascript.jscomp.Compiler(); - - options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, - new CollectTypesPass(this, compiler)); - options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, - new AddMemberPass(this, compiler)); - - //compiler.setErrorManager(testErrorManager); - compiler.initOptions(options); - - //Node script = compiler.parse(SourceFile.fromCode("[test]", source)); - - List<SourceFile> sources = new ArrayList<SourceFile>(); - for (ExternalFile externalFile : externals) + ClassReference classReference = getClassReference(className); + // XXX this is here because for now, the doc might be on the parent ASSIGN node + // if it's a static property with a value + JSDocInfo comment = NodeUtil.getBestJSDocInfo(node); + if (classReference != null) { - String name = externalFile.getName(); - String source = FileUtils.readFileToString(externalFile.getFile()); - sources.add(SourceFile.fromCode("[" + name + "]", source)); + classReference.addField(node, qualfiedName, comment, true); } - - Result compile = compiler.compile(EMPTY_EXTERNS, sources, options); - if (!compile.success) + else { - + System.err.println(">>>> {ReferenceModel} Class [" + className + + "] not found in " + node.getSourceFileName()); } } + //---------------------------------------------------- + public ExcludedMemeber isExcludedClass(ClassReference classReference) { - for (ExcludedMemeber memeber : excludesClass) - { - if (memeber.isExcluded(classReference, null)) - return memeber; - } - return null; + return getConfiguration().isExcludedClass(classReference); } public ExcludedMemeber isExcludedMember(ClassReference classReference, MemberReference memberReference) { - if (memberReference instanceof FieldReference) - { - for (ExcludedMemeber memeber : excludesField) - { - if (memeber.isExcluded(classReference, memberReference)) - return memeber; - } - } - for (ExcludedMemeber memeber : excludes) - { - if (memeber.isExcluded(classReference, memberReference)) - return memeber; - } - return null; - } - - public static class ExcludedMemeber - { - private String className; - private String name; - private String description; - - public String getClassName() - { - return className; - } - - public String getName() - { - return name; - } - - public String getDescription() - { - return description; - } - - public ExcludedMemeber(String className, String name) - { - this.className = className; - this.name = name; - } - - public ExcludedMemeber(String className, String name, String description) - { - this.className = className; - this.name = name; - this.description = description; - } - - public boolean isExcluded(ClassReference classReference, - MemberReference memberReference) - { - if (memberReference == null) - { - return classReference.getQualifiedName().equals(className); - } - return classReference.getQualifiedName().equals(className) - && memberReference.getQualifiedName().equals(name); - } - - public void print(StringBuilder sb) - { - if (description != null) - sb.append("// " + description + "\n"); - sb.append("//"); - } - } - - public static class ExternalFile - { - private File file; - - public File getFile() - { - return file; - } - - public ExternalFile(File file) - { - this.file = file; - } - - public String getName() - { - return FilenameUtils.getBaseName(getFile().getAbsolutePath()); - } + return getConfiguration().isExcludedMember(classReference, + memberReference); } } http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/21053509/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java index a6aebdb..815d9ee 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/JSTypeUtils.java @@ -36,6 +36,7 @@ public class JSTypeUtils JSTypeExpression paramType = reference.getComment().getParameterType( paramName); + if (paramType != null) { JSType jsType = JSTypeUtils.toParamJsType(
