Return-Path: X-Original-To: apmail-flex-commits-archive@www.apache.org Delivered-To: apmail-flex-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8EA2E18D60 for ; Thu, 9 Jul 2015 14:55:11 +0000 (UTC) Received: (qmail 96628 invoked by uid 500); 9 Jul 2015 14:55:11 -0000 Delivered-To: apmail-flex-commits-archive@flex.apache.org Received: (qmail 96556 invoked by uid 500); 9 Jul 2015 14:55:11 -0000 Mailing-List: contact commits-help@flex.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flex.apache.org Delivered-To: mailing list commits@flex.apache.org Received: (qmail 96504 invoked by uid 99); 9 Jul 2015 14:55:10 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Jul 2015 14:55:10 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 97069E682F; Thu, 9 Jul 2015 14:55:10 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: fthomas@apache.org To: commits@flex.apache.org Date: Thu, 09 Jul 2015 14:55:14 -0000 Message-Id: <18d7bb65d55747188e78f836aa30a001@git.apache.org> In-Reply-To: <4c67afb222c74c39abdc4b90e34300a4@git.apache.org> References: <4c67afb222c74c39abdc4b90e34300a4@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [5/9] git commit: [flex-falcon] [refs/heads/develop] - Adding the collect of imports pass Adding the collect of imports pass Replacing the last fix Fix bugs Cleanup and format Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/c55587ce Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/c55587ce Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/c55587ce Branch: refs/heads/develop Commit: c55587ceb2cb8439b884c1c3c09367056ca52975 Parents: 69f0a8b Author: Frédéric THOMAS Authored: Tue Jul 7 19:12:53 2015 +0100 Committer: Frédéric THOMAS Committed: Tue Jul 7 19:15:53 2015 +0100 ---------------------------------------------------------------------- .../externals/pass/CollectImportsPass.java | 164 ++++++++++++++++ .../externals/pass/ReferenceCompiler.java | 23 +-- .../externals/reference/BaseReference.java | 36 ++-- .../externals/reference/ClassReference.java | 196 ++++++++----------- .../externals/reference/FieldReference.java | 24 +-- .../externals/reference/MethodReference.java | 71 +++++-- .../externals/reference/ReferenceModel.java | 38 ++-- .../codegen/externals/utils/FunctionUtils.java | 71 ++++--- .../codegen/externals/utils/JSTypeUtils.java | 20 +- 9 files changed, 414 insertions(+), 229 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c55587ce/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/CollectImportsPass.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/CollectImportsPass.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/CollectImportsPass.java new file mode 100644 index 0000000..28815f9 --- /dev/null +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/CollectImportsPass.java @@ -0,0 +1,164 @@ +package org.apache.flex.compiler.internal.codegen.externals.pass; + +import java.util.List; +import java.util.Map; + +import org.apache.flex.compiler.internal.codegen.externals.reference.*; + +import com.google.javascript.jscomp.AbstractCompiler; +import com.google.javascript.jscomp.NodeTraversal; +import com.google.javascript.rhino.Node; + +/** + * @author: Frederic Thomas Date: 05/07/2015 Time: 18:16 + */ +public class CollectImportsPass extends AbstractCompilerPass +{ + public CollectImportsPass(final ReferenceModel model, AbstractCompiler compiler) + { + super(model, compiler); + } + + @Override + public boolean shouldTraverse(final NodeTraversal nodeTraversal, final Node n, final Node parent) + { + for (ClassReference reference : model.getClasses()) + { + collectClassImports(reference); + } + + for (FunctionReference reference : model.getFunctions()) + { + collectFunctionImports(reference); + } + + return false; + } + + private void collectClassImports(ClassReference reference) + { + final MethodReference constructor = reference.getConstructor(); + final List superClasses = reference.getSuperClasses(); + final List interfaces = reference.getInterfaces(); + final List extendedInterfaces = reference.getExtendedInterfaces(); + final Map fields = reference.getFields(); + final Map methods = reference.getMethods(); + + for (ClassReference superClass : superClasses) + { + if (model.isExcludedClass(superClass) == null) + { + addClassImport(reference, superClass); + } + } + + for (ClassReference _interface : interfaces) + { + if (model.isExcludedClass(_interface) == null) + { + addClassImport(reference, _interface); + } + } + + for (ClassReference _interface : extendedInterfaces) + { + if (model.isExcludedClass(_interface) == null) + { + addClassImport(reference, _interface); + } + } + + for (FieldReference field : fields.values()) + { + if (field.isExcluded() == null) + { + addClassImport(reference, getType(field)); + } + } + + for (ParameterReference parameterReference : constructor.getParameters()) + { + addClassImport(reference, getType(parameterReference)); + } + + for (MethodReference method : methods.values()) + { + if (method.isExcluded() == null) + { + addClassImport(reference, getReturnType(method)); + + for (ParameterReference parameterReference : method.getParameters()) + { + addClassImport(reference, getType(parameterReference)); + } + } + } + } + + private void addClassImport(final ClassReference thisReference, final ClassReference referenceToImport) + { + if (canImport(referenceToImport)) + { + final String thisPackageName = thisReference.getPackageName(); + final String importPackageName = referenceToImport.getPackageName(); + + if (!importPackageName.equals(thisPackageName)) + { + thisReference.addImport(referenceToImport); + } + } + } + + private void collectFunctionImports(final FunctionReference function) + { + if (function.isExcluded() == null) + { + ClassReference returnType = getReturnType(function); + + if (canImport(returnType)) + { + function.addImport(returnType); + } + + for (ParameterReference parameterReference : function.getParameters()) + { + ClassReference type = getType(parameterReference); + + if (canImport(type)) + { + function.addImport(type); + } + } + } + } + + private ClassReference getType(final FieldReference field) + { + return model.getClassReference(field.toTypeString()); + } + + private ClassReference getReturnType(final MethodReference method) + { + return model.getClassReference(method.transformReturnString()); + } + + private ClassReference getReturnType(final FunctionReference function) + { + return model.getClassReference(function.transformReturnString()); + } + + private ClassReference getType(final ParameterReference parameter) + { + return model.getClassReference(parameter.getQualifiedName()); + } + + private boolean canImport(ClassReference reference) + { + return reference != null && reference.isQualifiedName() && model.isExcludedClass(reference) == null; + } + + @Override + public void visit(final NodeTraversal t, final Node n, final Node parent) + { + } +} http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c55587ce/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java index ab47767..e5ac6c8 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/pass/ReferenceCompiler.java @@ -29,16 +29,12 @@ import org.apache.commons.io.FilenameUtils; import org.apache.flex.compiler.internal.codegen.externals.reference.ReferenceModel; import com.google.common.collect.ImmutableList; +import com.google.javascript.jscomp.*; import com.google.javascript.jscomp.Compiler; -import com.google.javascript.jscomp.CustomPassExecutionTime; -import com.google.javascript.jscomp.JXCompilerOptions; -import com.google.javascript.jscomp.Result; -import com.google.javascript.jscomp.SourceFile; public class ReferenceCompiler { - private static final List EMPTY_EXTERNS = ImmutableList.of(SourceFile.fromCode( - "externs", "")); + private static final List EMPTY_EXTERNS = ImmutableList.of(SourceFile.fromCode("externs", "")); private ReferenceModel model; @@ -72,15 +68,14 @@ public class ReferenceCompiler options.setParseJsDocDocumentation(true); options.setExternExports(false); - options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, - new NamespaceResolutionPass(model, jscompiler)); - options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, - new ResolvePackagesPass(model, jscompiler)); + options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, new NamespaceResolutionPass(model, + jscompiler)); + options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, new ResolvePackagesPass(model, jscompiler)); - options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, - new CollectTypesPass(model, jscompiler)); - options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, - new AddMemberPass(model, jscompiler)); + options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, new CollectTypesPass(model, jscompiler)); + options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, new AddMemberPass(model, jscompiler)); + + options.addCustomPass(CustomPassExecutionTime.BEFORE_OPTIMIZATIONS, new CollectImportsPass(model, jscompiler)); //compiler.setErrorManager(testErrorManager); jscompiler.initOptions(options); http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c55587ce/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/BaseReference.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/BaseReference.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/BaseReference.java index ea29a2f..cb3ef49 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/BaseReference.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/BaseReference.java @@ -33,7 +33,7 @@ import com.google.javascript.rhino.Node; public abstract class BaseReference { - private String qualfiedName; + private String qualifiedName; protected JSDocInfo comment; @@ -57,25 +57,25 @@ public abstract class BaseReference public String getBaseName() { - return qualfiedName.substring(qualfiedName.lastIndexOf('.') + 1); + return qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1); } public String getPackageName() { - int end = qualfiedName.lastIndexOf('.'); + int end = qualifiedName.lastIndexOf('.'); if (end == -1) return ""; - return qualfiedName.substring(0, end); + return qualifiedName.substring(0, end); } public String getQualifiedName() { - return qualfiedName; + return qualifiedName; } public final boolean isQualifiedName() { - return qualfiedName.indexOf('.') != -1; + return qualifiedName.indexOf('.') != -1; } public Node getNode() @@ -103,12 +103,11 @@ public abstract class BaseReference return model; } - public BaseReference(ReferenceModel model, Node node, String qualfiedName, - JSDocInfo comment) + public BaseReference(ReferenceModel model, Node node, String qualifiedName, JSDocInfo comment) { this.model = model; this.node = node; - this.qualfiedName = qualfiedName; + this.qualifiedName = qualifiedName; this.comment = comment; } @@ -143,7 +142,7 @@ public abstract class BaseReference sb.append(indent); sb.append(" * "); sb.append(blockDescription.replaceAll("\\n", "\n" + indent + " * ")); - sb.append("\n " + indent + "*\n"); + sb.append("\n ").append(indent).append("*\n"); } } @@ -161,11 +160,8 @@ public abstract class BaseReference if (!name.getItem().equals("see")) continue; - if (name != null) - { - desc.append(name.getItem()); - desc.append(" "); - } + desc.append(name.getItem()); + desc.append(" "); if (typePosition != null) { @@ -180,14 +176,14 @@ public abstract class BaseReference } sb.append(indent); - sb.append(" * @" + desc.toString() + "\n"); + sb.append(" * @").append(desc.toString()).append("\n"); } } protected void emitSeeSourceFileName(StringBuilder sb) { sb.append(indent); - sb.append(" * @see " + getNode().getSourceFileName() + "\n"); + sb.append(" * @see ").append(getNode().getSourceFileName()).append("\n"); } protected void emitFunctionCommentBody(StringBuilder sb) @@ -204,10 +200,8 @@ public abstract class BaseReference Set parameterNames = getComment().getParameterNames(); for (String paramName : parameterNames) { - JSTypeExpression parameterType = getComment().getParameterType( - paramName); - String description = getComment().getDescriptionForParameter( - paramName); + JSTypeExpression parameterType = getComment().getParameterType(paramName); + String description = getComment().getDescriptionForParameter(paramName); sb.append(indent); sb.append(" * @param "); http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c55587ce/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java index d2b1cc8..ae533d2 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/ClassReference.java @@ -19,18 +19,18 @@ package org.apache.flex.compiler.internal.codegen.externals.reference; +import java.io.File; +import java.util.*; +import java.util.Map.Entry; + +import org.apache.flex.compiler.internal.codegen.externals.utils.DebugLogUtils; +import org.apache.flex.compiler.internal.codegen.externals.utils.JSTypeUtils; + import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSDocInfoBuilder; import com.google.javascript.rhino.JSTypeExpression; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.jstype.JSType; -import org.apache.flex.compiler.internal.codegen.externals.utils.DebugLogUtils; -import org.apache.flex.compiler.internal.codegen.externals.utils.FunctionUtils; -import org.apache.flex.compiler.internal.codegen.externals.utils.JSTypeUtils; - -import java.io.File; -import java.util.*; -import java.util.Map.Entry; public class ClassReference extends BaseReference { @@ -114,13 +114,12 @@ public class ClassReference extends BaseReference /** * * @param model - * @param node (FUNCTION [NAME, PARAM_LIST, BLOCK]), or (ASSIGN [FUNCTION - * [NAME, PARAM_LIST, BLOCK]]) - * @param qualfiedName + * @param node (FUNCTION [NAME, PARAM_LIST, BLOCK]), or (ASSIGN [FUNCTION [NAME, PARAM_LIST, BLOCK]]) + * @param qualifiedName */ - public ClassReference(ReferenceModel model, Node node, String qualfiedName) + public ClassReference(ReferenceModel model, Node node, String qualifiedName) { - super(model, node, qualfiedName, node.getJSDocInfo()); + super(model, node, qualifiedName, node.getJSDocInfo()); indent = ""; @@ -165,20 +164,22 @@ public class ClassReference extends BaseReference objLit = node.getLastChild(); } - for (Node stringKey : objLit.children()) + if (objLit != null) { - if (stringKey.isStringKey()) + for (Node stringKey : objLit.children()) { - Node valueNode = stringKey.getFirstChild(); - - JSDocInfoBuilder b = new JSDocInfoBuilder(true); - JSDocInfo fieldComment = b.build(); - String fieldName = stringKey.getString(); - FieldReference field = addField(stringKey, fieldName, - fieldComment, true); - field.setConst(true); - field.setOverrideStringType(overrideStringType); - field.setConstantValueNode(valueNode); + if (stringKey.isStringKey()) + { + Node valueNode = stringKey.getFirstChild(); + + JSDocInfoBuilder b = new JSDocInfoBuilder(true); + JSDocInfo fieldComment = b.build(); + String fieldName = stringKey.getString(); + FieldReference field = addField(stringKey, fieldName, fieldComment, true); + field.setConst(true); + field.setOverrideStringType(overrideStringType); + field.setConstantValueNode(valueNode); + } } } } @@ -197,8 +198,7 @@ public class ClassReference extends BaseReference NAME Math OBJECTLIT */ - constructor = new NullConstructorReference(model, this, node, - getBaseName(), comment); + constructor = new NullConstructorReference(model, this, node, getBaseName(), comment); } else if (node.isFunction()) { @@ -255,8 +255,7 @@ public class ClassReference extends BaseReference if (functionNode != null) { - constructor = new MethodReference(model, this, functionNode, - getBaseName(), comment, false); + constructor = new MethodReference(model, this, functionNode, getBaseName(), comment, false); } } @@ -270,7 +269,7 @@ public class ClassReference extends BaseReference sb.append("package "); if (!packageName.equals("")) - sb.append(packageName + " "); + sb.append(packageName).append(" "); sb.append("{\n"); sb.append("\n"); @@ -364,8 +363,7 @@ public class ClassReference extends BaseReference for (JSTypeExpression jsTypeExpression : getComment().getImplementedInterfaces()) { String interfaceName = getModel().evaluate(jsTypeExpression).getDisplayName(); - ClassReference classReference = getModel().getClassReference( - interfaceName); + ClassReference classReference = getModel().getClassReference(interfaceName); if (classReference != null) result.add(classReference); } @@ -379,8 +377,20 @@ public class ClassReference extends BaseReference for (JSTypeExpression jsTypeExpression : getComment().getImplementedInterfaces()) { String interfaceName = getModel().evaluate(jsTypeExpression).toAnnotationString(); - ClassReference reference = getModel().getClassReference( - interfaceName); + ClassReference reference = getModel().getClassReference(interfaceName); + if (reference != null) + result.add(reference); + } + return result; + } + + public List getExtendedInterfaces() + { + ArrayList result = new ArrayList(); + for (JSTypeExpression jsTypeExpression : getComment().getExtendedInterfaces()) + { + String interfaceName = getModel().evaluate(jsTypeExpression).toAnnotationString(); + ClassReference reference = getModel().getClassReference(interfaceName); if (reference != null) result.add(reference); } @@ -422,18 +432,12 @@ public class ClassReference extends BaseReference public boolean hasInstanceField(String fieldName) { - if (!fields.containsKey(fieldName)) - return false; - - return !fields.get(fieldName).isStatic(); + return fields.containsKey(fieldName) && !fields.get(fieldName).isStatic(); } public boolean hasStaticField(String fieldName) { - if (!fields.containsKey(fieldName)) - return false; - - return fields.get(fieldName).isStatic(); + return fields.containsKey(fieldName) && fields.get(fieldName).isStatic(); } public boolean hasMethod(String methodName) @@ -443,22 +447,15 @@ public class ClassReference extends BaseReference public boolean hasInstanceMethod(String fieldName) { - if (!methods.containsKey(fieldName)) - return false; - - return !methods.get(fieldName).isStatic(); + return methods.containsKey(fieldName) && !methods.get(fieldName).isStatic(); } public boolean hasStaticMethod(String fieldName) { - if (!methods.containsKey(fieldName)) - return false; - - return methods.get(fieldName).isStatic(); + return methods.containsKey(fieldName) && methods.get(fieldName).isStatic(); } - public FieldReference addField(Node node, String fieldName, - JSDocInfo comment, boolean isStatic) + public FieldReference addField(Node node, String fieldName, JSDocInfo comment, boolean isStatic) { if (hasField(fieldName)) { @@ -471,88 +468,62 @@ public class ClassReference extends BaseReference if (comment == null) { - DebugLogUtils.err("Field comment null for; " - + node.getQualifiedName()); + DebugLogUtils.err("Field comment null for; " + node.getQualifiedName()); //DebugLogUtils.err(node); JSDocInfoBuilder b = new JSDocInfoBuilder(true); b.recordBlockDescription("Generated doc for missing field JSDoc."); comment = b.build(); } - FieldReference field = new FieldReference(getModel(), this, node, - fieldName, comment, isStatic); + FieldReference field = new FieldReference(getModel(), this, node, fieldName, comment, isStatic); fields.put(fieldName, field); return field; } - public MethodReference addMethod(Node node, String functionName, - JSDocInfo comment, boolean isStatic) + public MethodReference addMethod(Node node, String functionName, JSDocInfo comment, boolean isStatic) { if (isNamespace) isStatic = false; if (comment == null) { - DebugLogUtils.err("Method comment null for; " - + node.getQualifiedName()); + DebugLogUtils.err("Method comment null for; " + node.getQualifiedName()); //DebugLogUtils.err(node); JSDocInfoBuilder b = new JSDocInfoBuilder(true); b.recordBlockDescription("Generated doc for missing method JSDoc."); comment = b.build(); } - MethodReference method = new MethodReference(getModel(), this, node, - functionName, comment, isStatic); - - final String returnType = getReturnTypeToImport(method); - if (returnType != null) - { - addImport(returnType); - } + MethodReference method = new MethodReference(getModel(), this, node, functionName, comment, isStatic); methods.put(functionName, method); return method; } - private String getReturnTypeToImport(final MethodReference method) { - String returnType = null; - - final JSDocInfo comment = method.getComment(); - if (method.isExcluded() == null && comment != null && comment.hasReturnType()) - { - try { - final Node firstChild = comment.getReturnType().getRoot().getFirstChild(); - returnType = firstChild.getString(); - } catch (Exception e) { - returnType = null; - } - } - - final boolean canBeImported = FunctionUtils.canBeImported(getModel(), getNode(), returnType, getPackageName()); - - return canBeImported ? returnType : null; - } - public boolean isMethodOverrideFromInterface(MethodReference reference) { - if (!hasImplementations()) - return false; + boolean isMethodOverrideFromInterface = false; - List implementedInterfaces = getComment().getImplementedInterfaces(); - for (JSTypeExpression jsTypeExpression : implementedInterfaces) + if (!hasImplementations()) { - String interfaceName = getModel().evaluate(jsTypeExpression).getDisplayName(); - ClassReference classReference = getModel().getClassReference( - interfaceName); - return classReference.hasSuperMethod(reference.getQualifiedName()); + List implementedInterfaces = getComment().getImplementedInterfaces(); + for (JSTypeExpression jsTypeExpression : implementedInterfaces) + { + String interfaceName = getModel().evaluate(jsTypeExpression).getDisplayName(); + ClassReference classReference = getModel().getClassReference(interfaceName); + if (classReference.hasSuperMethod(reference.getQualifiedName())) + { + isMethodOverrideFromInterface = true; + break; + } + } } - return false; + return isMethodOverrideFromInterface; } - public MethodReference getMethodOverrideFromInterface( - MethodReference reference) + public MethodReference getMethodOverrideFromInterface(MethodReference reference) { // get all super classes, reverse and search top down List superClasses = getSuperClasses(); @@ -630,9 +601,12 @@ public class ClassReference extends BaseReference return fields.containsKey(fieldName); } - public void addImport(String qualifiedName) + public void addImport(ClassReference reference) { - imports.add(qualifiedName); + if (reference != null) + { + imports.add(reference.getQualifiedName()); + } } public boolean hasImport(String qualifiedName) @@ -647,12 +621,14 @@ public class ClassReference extends BaseReference private void emitImports(StringBuilder sb) { - sb.append("\n"); - for (String imp : imports) + if (imports.size() > 0) { - sb.append("import " + imp + ";\n"); + for (String anImport : imports) + { + sb.append("import ").append(anImport).append(";\n"); + } + sb.append("\n"); } - sb.append("\n"); } private void emitClass(StringBuilder sb) @@ -671,7 +647,7 @@ public class ClassReference extends BaseReference } sb.append("class "); - sb.append(getBaseName() + " "); + sb.append(getBaseName()).append(" "); if (getComment().hasBaseType()) { @@ -694,11 +670,10 @@ public class ClassReference extends BaseReference { sb.append("public interface "); - sb.append(getBaseName() + " "); + sb.append(getBaseName()).append(" "); List extendedInterfaces = getComment().getExtendedInterfaces(); int len = extendedInterfaces.size(); - int i = 0; if (len > 0) { sb.append("extends "); @@ -706,7 +681,7 @@ public class ClassReference extends BaseReference { String value = getModel().evaluate(jsTypeExpression).toAnnotationString(); sb.append(value); - if (i < len - 1) + if (--len > 0) sb.append(", "); } sb.append(" "); @@ -771,8 +746,7 @@ public class ClassReference extends BaseReference public File getFile(File asSourceRoot) { String packagePath = toPackagePath(); - return new File(asSourceRoot, packagePath + File.separator - + getBaseName() + ".as"); + return new File(asSourceRoot, packagePath + File.separator + getBaseName() + ".as"); } private String toPackagePath() @@ -783,9 +757,9 @@ public class ClassReference extends BaseReference String sdirPath = ""; if (cname.length > 0) { - for (int i = 0; i < cname.length; i++) + for (final String aCname : cname) { - sdirPath += cname[i] + File.separator; + sdirPath += aCname + File.separator; } return sdirPath; http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c55587ce/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java index 9cb6f09..ae39de9 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/FieldReference.java @@ -67,12 +67,16 @@ public class FieldReference extends MemberReference public String toTypeAnnotationString() { - JSType jsType = getModel().evaluate(getComment().getType()); - return jsType.toAnnotationString(); + JSType jsType = null; + if (getComment() != null && getComment().getReturnType() != null) + { + jsType = getModel().evaluate(getComment().getType()); + } + return jsType != null ? jsType.toAnnotationString() : "Object"; } - public FieldReference(ReferenceModel model, ClassReference classReference, - Node node, String name, JSDocInfo comment, boolean isStatic) + public FieldReference(ReferenceModel model, ClassReference classReference, Node node, String name, + JSDocInfo comment, boolean isStatic) { super(model, classReference, node, name, comment); this.isStatic = isStatic; @@ -101,10 +105,8 @@ public class FieldReference extends MemberReference return; // XXX (mschmalle) accessors are not treated right, need to exclude get/set } - if (!getClassReference().isInterface() - && !getComment().isOverride() - && !getClassReference().isPropertyInterfaceImplementation( - getBaseName())) + if (!getClassReference().isInterface() && !getComment().isOverride() + && !getClassReference().isPropertyInterfaceImplementation(getBaseName())) { emitVar(sb); } @@ -124,7 +126,7 @@ public class FieldReference extends MemberReference String setBody = isInterface ? "" : "{}"; String type = toTypeString(); - if (type.indexOf("|") != -1 || type.indexOf("?") != -1) + if (type.contains("|") || type.contains("?")) type = "*"; // getter @@ -157,7 +159,7 @@ public class FieldReference extends MemberReference String constVarValue = (isConst) ? "const " : "var "; String type = toTypeString(); - if (type.indexOf("|") != -1 || type.indexOf("?") != -1) + if (type.contains("|") || type.contains("?")) type = "*"; sb.append(indent); @@ -189,7 +191,7 @@ public class FieldReference extends MemberReference return "undefined /* TODO type not set */"; } - private String toTypeString() + public String toTypeString() { if (overrideStringType != null) return overrideStringType; http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c55587ce/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java index d031945..b37677b 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/reference/MethodReference.java @@ -19,11 +19,14 @@ package org.apache.flex.compiler.internal.codegen.externals.reference; +import java.util.ArrayList; +import java.util.List; import java.util.Set; import org.apache.flex.compiler.clients.ExternCConfiguration.ExcludedMember; import org.apache.flex.compiler.internal.codegen.externals.utils.FunctionUtils; +import com.google.common.collect.Lists; import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.Node; import com.google.javascript.rhino.jstype.JSType; @@ -35,6 +38,8 @@ public class MethodReference extends MemberReference private MethodReference override; private Node paramNode; + private List parameters; + private MethodReference getContext() { return override == null ? this : override; @@ -50,6 +55,11 @@ public class MethodReference extends MemberReference this.isStatic = isStatic; } + public List getParameters() + { + return parameters; + } + public Set getParameterNames() { return getComment().getParameterNames(); @@ -61,8 +71,8 @@ public class MethodReference extends MemberReference return jsType.toAnnotationString(); } - public MethodReference(ReferenceModel model, ClassReference classReference, - Node node, String name, JSDocInfo comment, boolean isStatic) + public MethodReference(ReferenceModel model, ClassReference classReference, Node node, String name, + JSDocInfo comment, boolean isStatic) { super(model, classReference, node, name, comment); this.isStatic = isStatic; @@ -75,14 +85,50 @@ public class MethodReference extends MemberReference { this.paramNode = node.getLastChild().getChildAtIndex(1); } + + addParameterReferences(); + } + + private void addParameterReferences() + { + + parameters = new ArrayList(); + + if (paramNode != null) + { + + final boolean isDocumented = comment.getParameterCount() > 0; + List parameterNames = null; + + if (isDocumented) + { + parameterNames = Lists.newArrayList(comment.getParameterNames()); + } + + for (Node param : paramNode.children()) + { + ParameterReference parameterReference; + + if (isDocumented && parameterNames.contains(param.getString())) + { + final String qualifiedName = FunctionUtils.toParameterType(this, param.getString()); + parameterReference = new ParameterReference(getModel(), param, qualifiedName); + } + else + { + parameterReference = new ParameterReference(getModel(), param); + } + + parameters.add(parameterReference); + } + } } @Override public void emit(StringBuilder sb) { // XXX HACK TEMP! - if (getComment().isConstructor() - && !getBaseName().equals(getClassReference().getBaseName())) + if (getComment().isConstructor() && !getBaseName().equals(getClassReference().getBaseName())) return; if (isConstructor()) @@ -117,8 +163,7 @@ public class MethodReference extends MemberReference if (!getClassReference().isInterface()) { - MethodReference overrideFromInterface = getClassReference().getMethodOverrideFromInterface( - this); + MethodReference overrideFromInterface = getClassReference().getMethodOverrideFromInterface(this); if (/*isOverride() && */overrideFromInterface != null) { override = overrideFromInterface; @@ -146,7 +191,7 @@ public class MethodReference extends MemberReference sb.append(staticValue); sb.append("function "); sb.append(getQualifiedName()); - sb.append(toPrameterString()); + sb.append(toParameterString()); sb.append(":"); sb.append(transformReturnString()); sb.append(braces); @@ -162,7 +207,7 @@ public class MethodReference extends MemberReference sb.append(getBaseName()); if (!getBaseName().equals("Object")) { - sb.append(toPrameterString()); + sb.append(toParameterString()); sb.append(" {\n"); sb.append(indent); emitSuperCall(sb); @@ -205,16 +250,14 @@ public class MethodReference extends MemberReference return getComment().isConstructor(); } - private String transformReturnString() + public String transformReturnString() { - return FunctionUtils.toReturnString(getContext(), - getContext().getComment()); + return FunctionUtils.toReturnString(getContext()); } - private String toPrameterString() + private String toParameterString() { - return FunctionUtils.toPrameterString(getContext(), - getContext().getComment(), paramNode); + return FunctionUtils.toParameterString(getContext(), getContext().getComment(), paramNode); } public boolean isOverride() http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c55587ce/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 adfa8c9..872bcd1 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 @@ -208,8 +208,7 @@ public class ReferenceModel log("Model.addFunction(" + qualifiedName + ")"); - FunctionReference reference = new FunctionReference(this, node, - qualifiedName, node.getJSDocInfo()); + FunctionReference reference = new FunctionReference(this, node, qualifiedName, node.getJSDocInfo()); functions.put(qualifiedName, reference); } @@ -233,8 +232,7 @@ public class ReferenceModel log("Model.addConstant(" + qualifiedName + ")"); - ConstantReference reference = new ConstantReference(this, node, - qualifiedName, node.getJSDocInfo()); + ConstantReference reference = new ConstantReference(this, node, qualifiedName, node.getJSDocInfo()); constants.put(qualifiedName, reference); } @@ -248,8 +246,7 @@ public class ReferenceModel log("Model.addConstantType(" + qualifiedName + ")"); - ConstantReference reference = new ConstantReference(this, node, - qualifiedName, node.getJSDocInfo(), type); + ConstantReference reference = new ConstantReference(this, node, qualifiedName, node.getJSDocInfo(), type); constants.put(qualifiedName, reference); } @@ -257,8 +254,7 @@ public class ReferenceModel { ClassReference classReference = getClassReference(className); if (classReference != null) - classReference.addField(node, memberName, node.getJSDocInfo(), - false); + classReference.addField(node, memberName, node.getJSDocInfo(), false); } public void addStaticField(Node node, String className, String memberName) @@ -273,8 +269,7 @@ public class ReferenceModel } else { - err(">>>> {ReferenceModel} Class [" + className + "] not found in " - + node.getSourceFileName()); + err(">>>> {ReferenceModel} Class [" + className + "] not found in " + node.getSourceFileName()); } } @@ -298,14 +293,25 @@ public class ReferenceModel } else { - err(">>>> {ReferenceModel} Class [" + className + "] not found in " - + node.getSourceFileName()); + err(">>>> {ReferenceModel} Class [" + className + "] not found in " + node.getSourceFileName()); } } public final JSType evaluate(JSTypeExpression expression) { - JSType jsType = expression.evaluate(null, jscompiler.getTypeRegistry()); + JSType jsType = null; + + if (expression != null) + { + try + { + jsType = expression.evaluate(null, jscompiler.getTypeRegistry()); + } + catch (Exception e) + { + e.printStackTrace(); + } + } return jsType; } @@ -316,11 +322,9 @@ public class ReferenceModel return getConfiguration().isExcludedClass(classReference); } - public ExcludedMember isExcludedMember(ClassReference classReference, - MemberReference memberReference) + public ExcludedMember isExcludedMember(ClassReference classReference, MemberReference memberReference) { - return getConfiguration().isExcludedMember(classReference, - memberReference); + return getConfiguration().isExcludedMember(classReference, memberReference); } //-------------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c55587ce/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/FunctionUtils.java ---------------------------------------------------------------------- diff --git a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/FunctionUtils.java b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/FunctionUtils.java index 561fddb..6bf3ac5 100644 --- a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/FunctionUtils.java +++ b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/externals/utils/FunctionUtils.java @@ -19,23 +19,45 @@ package org.apache.flex.compiler.internal.codegen.externals.utils; -import com.google.common.base.Strings; import org.apache.flex.compiler.internal.codegen.externals.reference.BaseReference; +import org.apache.flex.compiler.internal.codegen.externals.reference.ClassReference; +import org.apache.flex.compiler.internal.codegen.externals.reference.ReferenceModel; +import com.google.common.base.Strings; import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSTypeExpression; import com.google.javascript.rhino.Node; -import org.apache.flex.compiler.internal.codegen.externals.reference.ClassReference; -import org.apache.flex.compiler.internal.codegen.externals.reference.ReferenceModel; public class FunctionUtils { - public static String toReturnString(BaseReference reference, - JSDocInfo comment) + /** + * Compute the type of a function or method parameter. + * + * @param reference The FunctionReference or MethodReference the parameter belongs to + * @param name The name of the parameter + * @return the type of a function or method parameter + */ + public static String toParameterType(final BaseReference reference, final String name) + { + + String parameterType; + if (FunctionUtils.hasTemplate(reference)) + { + parameterType = "Object"; + } + else + { + parameterType = JSTypeUtils.toParamTypeString(reference, name); + } + + return parameterType; + } + + public static String toReturnString(BaseReference reference) { final StringBuilder sb = new StringBuilder(); - String returnType = null; + String returnType; if (hasTemplate(reference)) { @@ -51,8 +73,7 @@ public class FunctionUtils return sb.toString(); } - public static String toPrameterString(BaseReference reference, - JSDocInfo comment, Node paramNode) + public static String toParameterString(BaseReference reference, JSDocInfo comment, Node paramNode) { final StringBuilder sb = new StringBuilder(); @@ -71,7 +92,7 @@ public class FunctionUtils { for (Node param : paramNode.children()) { - sb.append(param.getString() + ":Object"); + sb.append(param.getString()).append(":Object"); if (index < len - 1) sb.append(", "); index++; @@ -82,8 +103,7 @@ public class FunctionUtils { for (String paramName : comment.getParameterNames()) { - sb.append(toParameter(reference, comment, paramName, - comment.getParameterType(paramName))); + sb.append(toParameter(reference, comment, paramName, comment.getParameterType(paramName))); if (index < len - 1) sb.append(", "); @@ -104,10 +124,11 @@ public class FunctionUtils * @param model The containing reference model * @param node The containing node * @param typeName The type we want check - * @param currentPackage The current package + * @param packageName The current package * @return true if we can import the given type into the given package */ - public static boolean canBeImported(final ReferenceModel model, final Node node, final String typeName, final String currentPackage) + public static boolean canBeImported(final ReferenceModel model, final Node node, final String typeName, + final String packageName) { boolean canImport = false; @@ -116,33 +137,27 @@ public class FunctionUtils final ClassReference reference = new ClassReference(null, node, typeName); final int lastDotPosition = typeName.lastIndexOf("."); - // Can import when the type to import does not belong the current package. - canImport = lastDotPosition > -1 && !typeName.substring(0, lastDotPosition).equals(currentPackage); + + // Can import when the type to import does not belong to the current package. + canImport = lastDotPosition > -1 && !typeName.substring(0, lastDotPosition).equals(packageName); + // And is not excluded. canImport &= model.isExcludedClass(reference) == null; - - // TODO: - /* - Manage the case where a custom class belongs - to the top level package and needs to be imported - in a sub-package as I don't know how to deal with - builtin classes. - */ } return canImport; } - private static String toParameter(BaseReference reference, - JSDocInfo comment, String paramName, JSTypeExpression parameterType) + private static String toParameter(BaseReference reference, JSDocInfo comment, String paramName, + JSTypeExpression parameterType) { final StringBuilder sb = new StringBuilder(); - String paramType = null; + String paramType; if (parameterType.isVarArgs()) { - sb.append("..." + paramName); + sb.append("...").append(paramName); } else { @@ -182,7 +197,7 @@ public class FunctionUtils return "null"; } - private static boolean hasTemplate(BaseReference reference) + public static boolean hasTemplate(BaseReference reference) { return reference.getComment().getTemplateTypeNames().size() > 0; } http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/c55587ce/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 3b4ae40..c14ddb8 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 @@ -34,16 +34,13 @@ public class JSTypeUtils { public static String toClassTypeString(ClassReference reference) { - String type = getJsType(reference.getModel(), - reference.getComment().getBaseType()).toString(); + String type = getJsType(reference.getModel(), reference.getComment().getBaseType()).toString(); return type; } - public static String toParamTypeString(BaseReference reference, - String paramName) + public static String toParamTypeString(BaseReference reference, String paramName) { - JSTypeExpression expression = reference.getComment().getParameterType( - paramName); + JSTypeExpression expression = reference.getComment().getParameterType(paramName); if (expression == null) return "Object"; @@ -80,8 +77,7 @@ public class JSTypeUtils public static String toEnumTypeString(BaseReference reference) { JSTypeExpression enumParameterType = reference.getComment().getEnumParameterType(); - String overrideStringType = transformType(reference.getModel().evaluate( - enumParameterType).toAnnotationString()); + String overrideStringType = transformType(reference.getModel().evaluate(enumParameterType).toAnnotationString()); return overrideStringType; } @@ -100,7 +96,7 @@ public class JSTypeUtils //-------------------------------------------------------------------------- - private static String transformType(String type) + public static String transformType(String type) { // XXX This is an error but, needs to be reduced in @param union if (type.indexOf("|") != -1) @@ -121,8 +117,7 @@ public class JSTypeUtils return type; } - private static String toTypeExpressionString(BaseReference reference, - JSTypeExpression expression) + private static String toTypeExpressionString(BaseReference reference, JSTypeExpression expression) { JSType jsType = getJsType(reference.getModel(), expression); String type = toTypeString(jsType); @@ -156,8 +151,7 @@ public class JSTypeUtils return type; } - private static JSType getJsType(ReferenceModel model, - JSTypeExpression typeExpression) + private static JSType getJsType(ReferenceModel model, JSTypeExpression typeExpression) { JSType jsType = model.evaluate(typeExpression);