Author: tchemit Date: 2011-02-16 22:18:47 +0100 (Wed, 16 Feb 2011) New Revision: 2219 Url: http://nuiton.org/repositories/revision/jaxx/2219 Log: Anomalie #1129: call constructors other than default ones (part one : register constructor from script) Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/script/ScriptManager.java Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/script/ScriptManager.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/script/ScriptManager.java 2011-02-16 21:15:29 UTC (rev 2218) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/script/ScriptManager.java 2011-02-16 21:18:47 UTC (rev 2219) @@ -27,13 +27,16 @@ import jaxx.compiler.CompilerException; import jaxx.compiler.JAXXCompiler; +import jaxx.compiler.java.JavaArgument; +import jaxx.compiler.java.JavaElementFactory; +import jaxx.compiler.java.JavaMethod; import jaxx.compiler.java.parser.JavaParser; import jaxx.compiler.java.parser.JavaParserTreeConstants; import jaxx.compiler.java.parser.SimpleNode; +import jaxx.compiler.reflect.ClassDescriptor; import jaxx.compiler.reflect.FieldDescriptor; import jaxx.compiler.reflect.MethodDescriptor; import jaxx.compiler.tags.TagManager; -import jaxx.runtime.JAXXUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -226,7 +229,12 @@ compiler.appendBodyCode(modifiers + " " + node.getText().substring(0, node.getText().length() - 1) + code + "}"); } + private void processConstructor(SimpleNode mainNode) { + compiler.registerInitializer(new RegisterConstructor(mainNode)); + } + + /** Logger */ static private final Log log = LogFactory.getLog(ScriptManager.class); @@ -290,7 +298,8 @@ } compiler.appendBodyCode(str); } else if (nodeType == JavaParserTreeConstants.JJTCONSTRUCTORDECLARATION) { - processConstructor(node.getChild(0).getChild(0).getText(), node.getChild(0).getChild(1)); + processConstructor(node); +// processConstructor(node.getChild(0).getChild(0).getText(), node.getChild(0).getChild(1)); } else if (nodeType == JavaParserTreeConstants.JJTLOCALVARIABLEDECLARATION || nodeType == JavaParserTreeConstants.JJTFIELDDECLARATION) { // the "local" variable declarations in this expression aren't actually local -- they are flagged local // just because there isn't an enclosing class scope visible to the parser. "Real" local variable @@ -395,4 +404,86 @@ } } } + + class RegisterConstructor implements Runnable { + + final SimpleNode mainNode; + + public RegisterConstructor(SimpleNode mainNode) { + this.mainNode = mainNode; + } + + @Override + public void run() { + + String className = mainNode.getChild(0).getChild(1).firstToken.image; + String modifiers = mainNode.getChild(0).getChild(0).getText(); + + SimpleNode node = mainNode.getChild(0).getChild(1); + int nbArguments = node.getChild(0).jjtGetNumChildren(); + if (log.isInfoEnabled()) { + log.info("Constructor found with " + nbArguments + " arguments : " + node.getText()); + } + assert node.getId() == JavaParserTreeConstants.JJTCONSTRUCTORDECLARATION : "expected node to be ConstructorDeclaration, found " + JavaParserTreeConstants.jjtNodeName[node.getId()] + " instead"; + assert node.getChild(0).getId() == JavaParserTreeConstants.JJTFORMALPARAMETERS : "expected node 0 to be FormalParameters, found " + JavaParserTreeConstants.jjtNodeName[node.getChild(1).getId()] + " instead"; + SimpleNode params = node.getChild(0); + StringBuilder bodyC = new StringBuilder(); + for (int i = 1; i < node.jjtGetNumChildren(); i++) { + bodyC.append(node.getChild(i).getText()); + } + String bodyContent = bodyC.toString().trim(); + JavaArgument[] arguments = new JavaArgument[nbArguments]; + for (int i = 0; i < nbArguments; i++) { + SimpleNode param = params.getChild(i); + String paramType = param.getChild(0).firstToken.image; + ClassDescriptor type = TagManager.resolveClass(paramType, compiler); + String paramName = param.getChild(2).firstToken.image; + if (log.isDebugEnabled()) { + log.debug("Parameter n°" + i + " --> [" + type + " : " + paramName + "]"); + } + JavaArgument arg = JavaElementFactory.newArgument(type.getName(), paramName); + arguments[i] = arg; + } + String[] modifierSplit = modifiers.trim().split("\\s"); + if (log.isDebugEnabled()) { + log.debug("Modifiers = " + Arrays.toString(modifierSplit)); + } + int finalModifiers = 0; + for (String mod : modifierSplit) { + mod = mod.trim(); + if ("public".equals(mod)) { + finalModifiers = Modifier.PUBLIC; + break; + } + if ("protected".equals(mod)) { + finalModifiers = Modifier.PROTECTED; + break; + } + if ("private".equals(mod)) { + finalModifiers = Modifier.PRIVATE; + break; + } + } + + if (log.isDebugEnabled()) { + log.debug("Final modifier to use : " + Modifier.toString(finalModifiers)); + } + if (!bodyContent.endsWith("$initialize();")) { + bodyContent += JAXXCompiler.getLineSeparator() + " $initialize();"; + } + if (log.isDebugEnabled()) { + log.debug("Constructor body :\n" + bodyContent); + } + JavaMethod constructorMethod = JavaElementFactory.newMethod( + finalModifiers, + null, + className, + bodyContent, + false, + arguments + ); + + compiler.getJavaFile().addMethod(constructorMethod); + } + } }
participants (1)
-
tchemit@users.nuiton.org