Author: tchemit Date: 2011-02-18 11:12:27 +0100 (Fri, 18 Feb 2011) New Revision: 2224 Url: http://nuiton.org/repositories/revision/jaxx/2224 Log: Evolution #1347: Improve classDescriptor loading (try also to obtain constructors if possible) Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFile.java Modified: trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFile.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFile.java 2011-02-18 10:10:44 UTC (rev 2223) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFile.java 2011-02-18 10:12:27 UTC (rev 2224) @@ -65,6 +65,10 @@ private static final Log log = LogFactory.getLog(ClassDescriptorResolverFromJavaFile.class); + public ClassDescriptorResolverFromJavaFile() { + super(ClassDescriptorHelper.ResolverType.JAVA_FILE); + } + @Override public ClassDescriptor resolvDescriptor(String className, URL source) throws ClassNotFoundException { @@ -105,11 +109,12 @@ } } - private static class JavaFileClassDescriptor extends ClassDescriptor { + private class JavaFileClassDescriptor extends ClassDescriptor { public JavaFileClassDescriptor(JavaFileParser parser, ClassLoader classLoader) { super( + ClassDescriptorResolverFromJavaFile.this.getResolverType(), parser.className, parser.packageName, parser.superclass, @@ -119,6 +124,7 @@ null, parser.jaxxObjectDescriptorValue == null ? null : JAXXUtil.decodeCompressedJAXXObjectDescriptor(parser.jaxxObjectDescriptorValue), classLoader, + parser.constructors.toArray(new MethodDescriptor[parser.constructors.size()]), parser.methods.toArray(new MethodDescriptor[parser.methods.size()]), parser.fields.toArray(new FieldDescriptor[parser.fields.size()]), parser.declaredFields.toArray(new FieldDescriptor[parser.declaredFields.size()]) @@ -186,6 +192,8 @@ /** public methods of the class */ private List<MethodDescriptor> methods; + private List<MethodDescriptor> constructors; + /** none public methods of the class */ // private List<MethodDescriptor> declaredMethods; @@ -209,6 +217,7 @@ // (for class resolving)... compiler = JAXXFactory.newDummyCompiler(classLoader); methods = new ArrayList<MethodDescriptor>(); + constructors = new ArrayList<MethodDescriptor>(); // declaredMethods = new ArrayList<MethodDescriptor>(); interfaces = new HashSet<String>(); fields = new ArrayList<FieldDescriptor>(); @@ -290,6 +299,7 @@ case JavaParserTreeConstants.JJTCLASSORINTERFACEDECLARATION: isInterface = child.firstToken.image.equals("interface"); scanClass(child); + break; case JavaParserTreeConstants.JJTENUMDECLARATION: isEnum = child.firstToken.image.equals("enum"); scanClass(child); @@ -365,7 +375,7 @@ case JavaParserTreeConstants.JJTCONSTRUCTORDECLARATION: - // TODO: handle constructors + scanConstructorDeclaration(node); break; case JavaParserTreeConstants.JJTMETHODDECLARATION: @@ -444,7 +454,6 @@ log.debug("detected a $jaxxObjectDescriptor = " + jaxxObjectDescriptorValue); } - } if (Modifier.isPublic(modifiers)) { @@ -456,6 +465,55 @@ } } + protected void scanConstructorDeclaration(SimpleNode node) { + String name = null; + List<String> parameterTypes = new ArrayList<String>(); + //List<String> parameterNames = new ArrayList<String>(); + for (int i = 0; i < node.jjtGetNumChildren(); i++) { + SimpleNode child = node.getChild(i); + int type = child.getId(); + + if (type == JavaParserTreeConstants.JJTMETHODDECLARATOR) { + name = child.firstToken.image.trim(); + SimpleNode formalParameters = child.getChild(0); + assert formalParameters.getId() == JavaParserTreeConstants.JJTFORMALPARAMETERS; + for (int j = 0; j < formalParameters.jjtGetNumChildren(); j++) { + SimpleNode parameter = formalParameters.getChild(j); + String rawParameterType = parameter.getChild(1).getText().trim().replaceAll("\\.\\.\\.", "[]"); + String parameterType = TagManager.resolveClassName(rawParameterType, compiler); + if (parameterType == null && JAXXCompiler.STRICT_CHECKS) { + throw new CompilerException("could not find class '" + rawParameterType + "'"); + } + parameterTypes.add(parameterType); + //parameterNames.add(parameter.getChild(2).getText().trim()); + } + } + } + + // determine the actual modifiers + + int modifiers = getModifiers(node); + + if (isInterface) { + + modifiers = Modifier.PUBLIC; + } + if (log.isDebugEnabled()) { + log.debug("method [" + name + "] modifiers == " + Modifier.toString(modifiers)); + } + + MethodDescriptor methodDescriptor = new MethodDescriptor( + name, + modifiers, + null, + parameterTypes.toArray(new String[parameterTypes.size()]), + compiler.getClassLoader() + ); + + constructors.add(methodDescriptor); + + } + protected void scanMethodDeclaration(SimpleNode node) { String returnType = null; String name = null; @@ -488,7 +546,7 @@ int modifiers = getModifiers(node); if (isInterface) { - + modifiers = Modifier.PUBLIC; } if (log.isDebugEnabled()) {