r2107 - in trunk/jaxx-compiler/src: main/java/jaxx/compiler/reflect test/java/jaxx/compiler/reflect test/java/jaxx/compiler/reflect/resolvers
Author: tchemit Date: 2010-10-05 10:55:56 +0200 (Tue, 05 Oct 2010) New Revision: 2107 Url: http://nuiton.org/repositories/revision/jaxx/2107 Log: Evolution #915: Remove deprecated api from 2.0.2 (JavaFileParser and ClassDescriptorLoader) Removed: trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/ClassDescriptorLoader.java trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/JavaFileParser.java trunk/jaxx-compiler/src/test/java/jaxx/compiler/reflect/JavaFileParserTest.java Modified: trunk/jaxx-compiler/src/test/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFileTest.java Deleted: trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/ClassDescriptorLoader.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/ClassDescriptorLoader.java 2010-10-05 08:37:01 UTC (rev 2106) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/ClassDescriptorLoader.java 2010-10-05 08:55:56 UTC (rev 2107) @@ -1,648 +0,0 @@ -/* - * #%L - * JAXX :: Compiler - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2008 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package jaxx.compiler.reflect; - -import jaxx.compiler.CompilerException; -import jaxx.compiler.JAXXCompiler; -import jaxx.compiler.JAXXEngine; -import jaxx.compiler.JAXXFactory; -import jaxx.compiler.SymbolTable; -import jaxx.runtime.JAXXObject; -import jaxx.runtime.JAXXObjectDescriptor; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.net.URL; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Mirrors the class <code>java.lang.ClassLoader</code>. JAXX uses <code>ClassDescriptor</code> instead of <code>Class</code> - * almost everywhere so that it can handle circular dependencies (there can't be a <code>Class</code> object for an uncompiled - * JAXX or Java source file, and a compiler must be allow references to symbols in uncompiled source files in order to handle - * circular dependencies). - * - * @deprecated since 2.0.2, prefer use the more efficient {@link ClassDescriptorHelper}. - */ -@Deprecated -public class ClassDescriptorLoader { - - /** Logger */ - private static final Log log = LogFactory.getLog(ClassDescriptorLoader.class); - - private static Map<String, ClassDescriptor> descriptors = - new HashMap<String, ClassDescriptor>(); - - private ClassDescriptorLoader() { - // on instance - } - - public static ClassDescriptor getClassDescriptor( - String className) throws ClassNotFoundException { - - ClassDescriptor descriptor = getClassDescriptor( - className, - Thread.currentThread().getContextClassLoader() - ); - return descriptor; - } - - public static ClassDescriptor getClassDescriptor( - String className, - ClassLoader classLoader) throws ClassNotFoundException { - - ClassDescriptor result = descriptors.get(className); - if (result != null) { - return result; - } - - JAXXEngine engine = JAXXFactory.isEngineRegistred() ? - JAXXFactory.getEngine() : null; - - if (engine != null) { - - SymbolTable symbolTable = engine.getSymbolTable(className); - - if (symbolTable != null) { - - log.info("<<< from symbol table [" + className + "]"); - - JAXXCompiler compiler = engine.getJAXXCompiler(className); - - result = createClassDescriptorFromSymbolTable( - compiler, - className, - classLoader - ); - descriptors.put(className, result); - return result; - } - } - - // the class is not in this compile set, try to have it from java - // sources or classes. - - if (classLoader == null) { - classLoader = ClassDescriptorLoader.class.getClassLoader(); - } - - String relativePath = className.replaceAll("\\.", "/"); - String relativePathPattern = ".*";// + className + ".*"; // used to ensure that the located resource has the right character cases - - // find the most recently updated source for the class -- Java source, JAXX source, or compiled class file - long javaLastModified = -1; - URL javaFile = classLoader.getResource(relativePath + ".java"); - if (javaFile != null && - javaFile.toString().startsWith("file:") && - javaFile.toString().matches(relativePathPattern)) { - javaLastModified = JAXXCompiler.URLtoFile(javaFile).lastModified(); - log.info("[" + className + "] javaFile lastModified " + javaLastModified); - } - - long classLastModified = -1; - URL classFile = classLoader.getResource(relativePath + ".class"); - if (classFile != null && - classFile.toString().startsWith("file:") && - classFile.toString().matches(relativePathPattern)) { - classLastModified = JAXXCompiler.URLtoFile(classFile).lastModified(); - log.info("[" + className + "] class lastModified " + classLastModified); - } - - long jaxxLastModified = -1; - URL jaxxFile = classLoader.getResource(relativePath + ".jaxx"); - if (jaxxFile != null && - jaxxFile.toString().startsWith("file:") && - jaxxFile.toString().matches(relativePathPattern)) { - File jaxxFilePath = JAXXCompiler.URLtoFile(jaxxFile); - jaxxLastModified = jaxxFilePath.lastModified(); - String simplePath = jaxxFilePath.getPath(); - simplePath = simplePath.substring(0, simplePath.length() - ".jaxx".length()); - File cssFilePath = new File(simplePath + ".css"); - if (cssFilePath.exists()) { - jaxxLastModified = Math.max(jaxxLastModified, cssFilePath.lastModified()); - } - File scriptFilePath = new File(simplePath + ".script"); - if (scriptFilePath.exists()) { - jaxxLastModified = Math.max(jaxxLastModified, scriptFilePath.lastModified()); - } - log.info("[" + className + "] jaxxFile lastModified " + jaxxLastModified); - } - - if (classLastModified != -1) { - - // there is a class - - if (classLastModified >= javaLastModified && classLastModified >= jaxxLastModified) { - - // ok class is up to date, choose it - Class<?> javaClass = getClass(className, classLoader); - - log.debug("<<< from class [" + className + "] " + javaClass); - - result = createClassDescriptorFromClass(javaClass); - descriptors.put(className, result); - return result; - } - - // the class is not the last modified resource, do not use it - } - -// if (jaxxLastModified != -1) { -// -// // there is a jaxx file, use it -// -// File jaxxFilePath = JAXXCompiler.URLtoFile(jaxxFile); -// -// JAXXCompilerFile file = new JAXXCompilerFile(jaxxFilePath, className); -// -// engine.addFileToCompile(jaxxFilePath, className); -// -// JAXXCompiler compiler = engine.getJAXXCompiler(className); -// -// result = createClassDescriptorFromSymbolTable( -// compiler, -// className, -// classLoader -// ); -// // result = createClassDescriptorFromSymbolTable(className, classLoader); -// descriptors.put(className, result); -// return result; -// } - - if (javaLastModified != -1) { - - // there is a java source - - if (javaLastModified > jaxxLastModified) { - - log.info("<<< from java file [" + className + "] " + javaFile); - - // ok java source is up to date, choose it - result = createClassDescriptorFromJavaSource(javaFile, classLoader); - - descriptors.put(className, result); -// } - return result; - } - - // the java source is not the last modified resource, do not use it - - } - - - // last fall back for class - Class<?> javaClass = getClass(className, classLoader); - - log.debug("<<< from class [" + className + "] " + javaClass); - - result = createClassDescriptorFromClass(javaClass); - - if (result != null) { - descriptors.put(className, result); - return result; - } - - // can NOT come here, means could not find result... - - throw new IllegalStateException("Can not find descriptor for " + className); -// -// if (jaxxLastModified != -1 && JAXXFactory.isEngineRegistred() && symbolTable == null) { -// jaxxLastModified = -1; // file has been modified, but wasn't included in this -// } -// // compilation set so we don't have a symbol table -// -// if (javaLastModified != -1 || classLastModified != -1 || jaxxLastModified != -1) { -// -// if (javaLastModified > classLastModified && javaLastModified > jaxxLastModified) { -// result = createClassDescriptorFromJavaSource(javaFile, classLoader); -// } -//// if (jaxxLastModified > classLastModified && jaxxLastModified > javaLastModified) { -//// result = createClassDescriptorFromSymbolTable(className, classLoader); -//// } else if (javaLastModified > classLastModified && javaLastModified > jaxxLastModified) { -//// result = createClassDescriptorFromJavaSource(javaFile, classLoader); -//// } -// } -// // else work off of the class file. This also handles the case where -// // the class is available, but wasn't in a location where -// // we could check its last modified date (in a JAR, over the network, etc.) -// if (result == null) { -// Class<?> javaClass = getClass(className, classLoader); -// result = createClassDescriptorFromClass(javaClass); -// } -// descriptors.put(className, result); -//// } -// return result; - } - - @Deprecated - public static ClassDescriptor getClassDescriptor(Class<?> javaClass) { - return ClassDescriptorHelper.getClassDescriptor(javaClass); -// try { -// return getClassDescriptor( -// javaClass.getName(), -// javaClass.getClassLoader() -// ); -// } catch (ClassNotFoundException e) { -// throw new RuntimeException(e); -// } - } - - @Deprecated - public static Class<?> getPrimitiveBoxedClass(String className) { - return ClassDescriptorHelper.getPrimitiveBoxedClass(className); -// if (className.equals("boolean")) { -// return Boolean.class; -// } -// if (className.equals("byte")) { -// return Byte.class; -// } -// if (className.equals("short")) { -// return Short.class; -// } -// if (className.equals("int")) { -// return Integer.class; -// } -// if (className.equals("long")) { -// return Long.class; -// } -// if (className.equals("float")) { -// return Float.class; -// } -// if (className.equals("double")) { -// return Double.class; -// } -// if (className.equals("char")) { -// return Character.class; -// } -// if (className.equals("void")) { -// return Void.class; -// } -// return null; - } - - @Deprecated - public static Class<?> getPrimitiveClass( - String className) throws ClassNotFoundException { - return ClassDescriptorHelper.getPrimitiveClass(className); -// if (className.equals("boolean")) { -// return boolean.class; -// } -// if (className.equals("byte")) { -// return byte.class; -// } -// if (className.equals("short")) { -// return short.class; -// } -// if (className.equals("int")) { -// return int.class; -// } -// if (className.equals("long")) { -// return long.class; -// } -// if (className.equals("float")) { -// return float.class; -// } -// if (className.equals("double")) { -// return double.class; -// } -// if (className.equals("char")) { -// return char.class; -// } -// if (className.equals("void")) { -// return void.class; -// } -// // detect arrays -// int arrayCount = 0; -// while (className.endsWith("[]")) { -// arrayCount++; -// className = className.substring(0, className.length() - 2); -// } -// Class<?> klass; -// if (arrayCount > 0) { -// klass = getPrimitiveClass(className); -// if (klass == null) { -// // none primitive array -// return null; -// } -// // must take the boxed class, other it does not works -// // to make a Class.forName("[Lchar;"); but works -// // with Class.forName("[LCharacter;"); ... -// klass = getPrimitiveBoxedClass(className); -// className = klass.getName(); -// -// className = "L" + className + ";"; -// while (arrayCount > 0) { -// className = "[" + className; -// arrayCount--; -// } -// //System.out.println("primitive array class "+className); -// return Class.forName(className); -// } -// return null; - } - - @Deprecated - public static Class<?> getClass(String className, - ClassLoader classLoader) throws ClassNotFoundException { - return ClassDescriptorHelper.getClass(className, classLoader); -// Class<?> klass = getPrimitiveClass(className); -// if (klass != null) { -// return klass; -// } -// // try an array of none primitive classes -// int arrayCount = 0; -// while (className.endsWith("[]")) { -// arrayCount++; -// className = className.substring(0, className.length() - 2); -// } -// if (arrayCount > 0) { -// className = "L" + className + ";"; -// while (arrayCount > 0) { -// className = "[" + className; -// arrayCount--; -// } -// } -// try { -// return classLoader != null ? -// Class.forName(className, true, classLoader) : -// Class.forName(className); -// -// } catch (ClassNotFoundException e) { -// // perharps we are in a inner class ? -// int dotIndex = className.lastIndexOf("."); -// if (dotIndex > -1) { -// String parentFQN = className.substring(0, dotIndex); -// String simpleName = className.substring(dotIndex + 1); -// try { -// Class<?> parentClass = classLoader != null ? Class.forName(parentFQN, true, classLoader) : Class.forName(parentFQN); -// for (Class<?> innerClass : parentClass.getClasses()) { -// if (simpleName.equals(innerClass.getSimpleName())) { -// return innerClass; -// } -// } -// } catch (ClassNotFoundException e1) { -// // no super class,so let the first exception throw... -// } -// } -// throw e; -// } catch (NoClassDefFoundError e) { -// -// throw new ClassNotFoundException(e.toString()); -// } - } - - private static MethodDescriptor createMethodDescriptor(Method javaMethod, - ClassLoader classLoader) { - String methodName = javaMethod.getName(); - int modifiers = javaMethod.getModifiers(); - String returnType = javaMethod.getReturnType().getName(); - Class<?>[] javaParameters = javaMethod.getParameterTypes(); - String[] parameters = new String[javaParameters.length]; - for (int i = 0; i < parameters.length; i++) { - parameters[i] = javaParameters[i].getName(); - } - return new MethodDescriptor(methodName, modifiers, returnType, parameters, classLoader); - } - - private static FieldDescriptor createFieldDescriptor(Field javaField, - ClassLoader classLoader) { - String fieldName = javaField.getName(); - int modifiers = javaField.getModifiers(); - String type = javaField.getType().getName(); - return new FieldDescriptor(fieldName, modifiers, type, classLoader); - } - - private static JAXXObjectDescriptor getJAXXObjectDescriptor(Class<?> jaxxClass) { - if (!JAXXObject.class.isAssignableFrom(jaxxClass) || - JAXXObject.class.equals(jaxxClass)) { - return null; - } - try { - Method getJAXXObjectDescriptor = jaxxClass.getMethod("$getJAXXObjectDescriptor"); - return (JAXXObjectDescriptor) getJAXXObjectDescriptor.invoke(null); - } catch (NoSuchMethodException e) { - throw new CompilerException("Expected JAXXObject " + jaxxClass.getName() + " to have a static method named $getJAXXObjectDescriptor"); - } catch (IllegalAccessException e) { - throw new CompilerException("Expected JAXXObject " + jaxxClass.getName() + "'s $getJAXXObjectDescriptor method to be public"); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } - } - - private static ClassDescriptor createClassDescriptorFromJavaSource(URL javaSource, ClassLoader classLoader) throws ClassNotFoundException { - try { - InputStream in = javaSource.openStream(); - ClassDescriptor result = null; - Reader reader = new InputStreamReader(in, "utf-8"); - try { - if (log.isDebugEnabled()) { - log.debug("for source " + javaSource); - } - result = JavaFileParser.parseJavaFile(javaSource.toString(), reader, classLoader); - } finally { - - reader.close(); - } - return result; - } catch (IOException e) { - throw new ClassNotFoundException(e.toString()); - } - } - - private static ClassDescriptor createClassDescriptorFromSymbolTable( - final JAXXCompiler compiler, - String className, - ClassLoader classLoader) throws ClassNotFoundException { - - final SymbolTable symbolTable = compiler.getSymbolTable(); -// final JAXXCompiler compiler = JAXXFactory.getEngine().getJAXXCompiler(className); -// final SymbolTable symbolTable = JAXXFactory.getEngine().getSymbolTable(className); - if (symbolTable == null) { - throw new CompilerException("Internal error: no symbol table was generated for class '" + className + "'"); - } - if (log.isDebugEnabled()) { - log.debug("for compiler " + compiler.getOutputClassName()); - } - ClassDescriptor superclass = getClassDescriptor(symbolTable.getSuperclassName(), classLoader); - List<MethodDescriptor> publicMethods = symbolTable.getScriptMethods(); - List<FieldDescriptor> publicFields = symbolTable.getScriptFields(); - //List<MethodDescriptor> declaredMethods = new ArrayList<MethodDescriptor>(publicMethods); - //List<FieldDescriptor> declaredFields = new ArrayList<FieldDescriptor>(publicFields); - Iterator<MethodDescriptor> methods = publicMethods.iterator(); - while (methods.hasNext()) { - MethodDescriptor method = methods.next(); - if (!Modifier.isPublic(method.getModifiers())) { - methods.remove(); - } - } - Iterator<FieldDescriptor> fields = publicFields.iterator(); - while (fields.hasNext()) { - FieldDescriptor field = fields.next(); - if (!Modifier.isPublic(field.getModifiers())) { - fields.remove(); - } - } - Set<String> interfaces = new HashSet<String>(); - if (symbolTable.getInterfaces() != null) { - // having interfaces - for (String anInterface : symbolTable.getInterfaces()) { - int genericIndex = anInterface.indexOf("<"); - if (genericIndex > -1) { - // remove generic type - anInterface = anInterface.substring(0, genericIndex); - } - if (log.isDebugEnabled()) { - log.debug("getting interface " + anInterface + " descriptor for class " + className); - } - interfaces.add(anInterface); - ClassDescriptor interfaceclass = getClassDescriptor(anInterface, classLoader); - publicMethods.addAll(Arrays.asList(interfaceclass.getMethodDescriptors())); - publicFields.addAll(Arrays.asList(interfaceclass.getFieldDescriptors())); - } - } - publicMethods.addAll(Arrays.asList(superclass.getMethodDescriptors())); - publicFields.addAll(Arrays.asList(superclass.getFieldDescriptors())); - int dotPos = className.lastIndexOf("."); - String packageName = dotPos != -1 ? className.substring(0, dotPos) : null; - - ClassDescriptor[] superclassInterfaces = superclass.getInterfaces(); - for (ClassDescriptor superclassInterface : superclassInterfaces) { - interfaces.add(superclassInterface.getName()); - } - interfaces.add(JAXXObject.class.getName()); - return new ClassDescriptor(className, packageName, symbolTable.getSuperclassName(), - interfaces.toArray(new String[interfaces.size()]), false, false, null, null, classLoader, - publicMethods.toArray(new MethodDescriptor[publicMethods.size()]), - publicFields.toArray(new FieldDescriptor[publicFields.size()])) { - - @Override - public FieldDescriptor getDeclaredFieldDescriptor(String name) throws NoSuchFieldException { - String type = symbolTable.getClassTagIds().get(name); - if (type != null) { - return new FieldDescriptor(name, Modifier.PROTECTED, type, compiler.getClassLoader()); - } - throw new NoSuchFieldException(name); - } - - @Override - public MethodDescriptor getDeclaredMethodDescriptor(String name, ClassDescriptor... parameterTypes) throws NoSuchMethodException { - throw new NoSuchMethodException(name); - } - - @Override - public JAXXObjectDescriptor getJAXXObjectDescriptor() { - return compiler.getJAXXObjectDescriptor(); - } - }; - } - - private static ClassDescriptor createClassDescriptorFromClass(final Class<?> javaClass) { - String name = javaClass.getName(); - Package p = javaClass.getPackage(); - String packageName = p != null ? p.getName() : null; - Class<?> superclass = javaClass.getSuperclass(); - String superclassName = superclass != null ? superclass.getName() : null; - Class<?>[] interfaces = javaClass.getInterfaces(); - String[] interfaceNames = new String[interfaces.length]; - for (int i = 0; i < interfaces.length; i++) { - interfaceNames[i] = interfaces[i].getName(); - } - boolean isInterface = javaClass.isInterface(); - boolean isArray = javaClass.isArray(); - String componentTypeName = isArray ? javaClass.getComponentType().getName() : null; - JAXXObjectDescriptor jaxxObjectDescriptor = getJAXXObjectDescriptor(javaClass); - ClassLoader classLoader = javaClass.getClassLoader(); - Method[] javaMethods = javaClass.getMethods(); - MethodDescriptor[] methods = new MethodDescriptor[javaMethods.length]; - for (int i = 0; i < methods.length; i++) { - methods[i] = createMethodDescriptor(javaMethods[i], javaClass.getClassLoader()); - } - Field[] javaFields = javaClass.getFields(); - FieldDescriptor[] fields = new FieldDescriptor[javaFields.length]; - for (int i = 0; i < fields.length; i++) { - fields[i] = createFieldDescriptor(javaFields[i], javaClass.getClassLoader()); - } - return new ClassDescriptor(name, packageName, - superclassName, - interfaceNames, - isInterface, - isArray, - componentTypeName, - jaxxObjectDescriptor, - classLoader, - methods, - fields) { - - @Override - public FieldDescriptor getDeclaredFieldDescriptor(String name) throws NoSuchFieldException { - return createFieldDescriptor(javaClass.getDeclaredField(name), - javaClass.getClassLoader() - ); - } - - @Override - public MethodDescriptor getDeclaredMethodDescriptor(String name, - ClassDescriptor... parameterTypes) throws NoSuchMethodException { - try { - Class<?>[] parameterTypeClasses = new Class[parameterTypes.length]; - for (int i = 0; i < parameterTypes.length; i++) { - parameterTypeClasses[i] = Class.forName(parameterTypes[i].getName()); - } - return createMethodDescriptor(javaClass.getDeclaredMethod(name, parameterTypeClasses), javaClass.getClassLoader()); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } - } - }; - } - - public static void checkSupportClass(Class<?> handlerClass, ClassDescriptor beanClass, Class<?>... tagClasses) { - for (Class<?> tagClass : tagClasses) { - if (getClassDescriptor(tagClass).isAssignableFrom(beanClass)) { - return; - } - } - throw new IllegalArgumentException(handlerClass.getName() + " does not support the class " + beanClass.getName()); - } - - public static void reset() { - descriptors.clear(); - } -} Deleted: trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/JavaFileParser.java =================================================================== --- trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/JavaFileParser.java 2010-10-05 08:37:01 UTC (rev 2106) +++ trunk/jaxx-compiler/src/main/java/jaxx/compiler/reflect/JavaFileParser.java 2010-10-05 08:55:56 UTC (rev 2107) @@ -1,573 +0,0 @@ -/* - * #%L - * JAXX :: Compiler - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2008 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package jaxx.compiler.reflect; - -import jaxx.compiler.CompilerException; -import jaxx.compiler.JAXXCompiler; -import jaxx.compiler.JAXXFactory; -import jaxx.compiler.java.parser.JavaParser; -import jaxx.compiler.java.parser.JavaParserTreeConstants; -import jaxx.compiler.java.parser.ParseException; -import jaxx.compiler.java.parser.SimpleNode; -import jaxx.compiler.tags.TagManager; -import jaxx.runtime.JAXXObjectDescriptor; -import jaxx.runtime.JAXXUtil; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.io.Reader; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -/** - * @deprecated since 2.0.2, now use the {@link ClassDescriptorHelper} to obtain - * descriptors from java source files. - */ -@Deprecated -public class JavaFileParser { - - /** Logger */ - static private final Log log = LogFactory.getLog(JavaFileParser.class); - - /** - * the compiler used (this is a dummy compiler with no link with any file). - * <p/> - * FIXME-TC20100504 We should remove this link : should not need of a - * compiler to parse a java files. - */ - private JAXXCompiler compiler; - - /** fully qualified name of the class */ - private String className; - - /** package of the class */ - private String packageName; - - /** fully qualified name of the super class or {@code null} if no super class. */ - private String superclass; - - /** - * flag to known if we deal with an enum (the state is setted in the - * {@link #doParse(String, Reader)} method). - */ - private boolean isEnum; - - /** - * flag to known if we deal with an interface(the state is setted in the - * {@link #doParse(String, Reader)} method). - */ - private boolean isInterface; - - /** set of fully qualified names of interfaces of the class. */ - private Set<String> interfaces; - - /** public methods of the class */ - private List<MethodDescriptor> methods; - - /** public fields of the class */ - private List<FieldDescriptor> fields; - - /** none public fields of the class */ - private List<FieldDescriptor> declaredFields; - - /** - * If sets, compressed value of the $jaxxObjectDescriptor field, this means - * the class if a JAXXObject implementation. - */ - private String jaxxObjectDescriptorValue; - - public static final String[] EMPTY_STRING_ARRAY = new String[0]; - - private JavaFileParser(ClassLoader classLoader) { - //FIXME-TC-20100504 : shoudl remove this to make the parser free of jaxx :) - // We could imagine just to offers to the parser a list of namespaces - // (for class resolving)... - compiler = JAXXFactory.newDummyCompiler(classLoader); - methods = new ArrayList<MethodDescriptor>(); - interfaces = new HashSet<String>(); - fields = new ArrayList<FieldDescriptor>(); - declaredFields = new ArrayList<FieldDescriptor>(); - superclass = Object.class.getName(); - } - - public static ClassDescriptor parseJavaFile( - String displayName, - Reader src, - ClassLoader classLoader) throws ClassNotFoundException { - //FIXME-TC20091205 : no now JAXX can look to interfaces, must add them - // has some limitations -- it reports all members as public, leaves getDeclaredMethod and getDeclaredField - // undefined, and doesn't report interfaces. It's safe to leave those the way they are for now, because - // JAXX doesn't look at any of those for non-JAXX classes. - JavaFileParser parser = new JavaFileParser(classLoader); - if (log.isDebugEnabled()) { - log.debug("starting parsing : " + displayName); - } - try { - parser.doParse(displayName, src); - } catch (Exception e) { - log.error(e.getMessage()); - throw new RuntimeException(e); - } - - JAXXObjectDescriptor jaxxObjectDescriptor = null; - - if (parser.jaxxObjectDescriptorValue != null) { - - // compute the jaxx object descriptor - - jaxxObjectDescriptor = JAXXUtil.decodeCompressedJAXXObjectDescriptor( - parser.jaxxObjectDescriptorValue - ); - } - - List<MethodDescriptor> publicMethods = parser.methods; - List<FieldDescriptor> publicFields = parser.fields; - List<FieldDescriptor> declaredFields = parser.declaredFields; - //List/*<MethodDescriptor>*/ declaredMethods = new ArrayList/*<MethodDescriptor>*/(publicMethods); - //List/*<FieldDescriptor>*/ declaredFields = new ArrayList/*<FieldDescriptor>*/(publicFields); - Iterator<MethodDescriptor> methods = publicMethods.iterator(); - while (methods.hasNext()) { - MethodDescriptor method = methods.next(); - if (!Modifier.isPublic(method.getModifiers())) { - methods.remove(); - } - } - Iterator<FieldDescriptor> fields = publicFields.iterator(); - while (fields.hasNext()) { - FieldDescriptor field = fields.next(); - if (!Modifier.isPublic(field.getModifiers())) { - fields.remove(); - } - } - - if (parser.superclass != null) { - //FIXME-TC20100504 This is not good, should add nothing here - // and modify the algorithm of ClassDescriptor to go and seek - // in super classes on interfaces if required. - ClassDescriptor superclassDescriptor = ClassDescriptorHelper.getClassDescriptor(parser.superclass, classLoader); - publicMethods.addAll(Arrays.asList(superclassDescriptor.getMethodDescriptors())); - publicFields.addAll(Arrays.asList(superclassDescriptor.getFieldDescriptors())); - } - //FIXME-TC20091205 : now JAXX can look to interfaces, must add them - //Set<String> interfaces = new HashSet<String>(); - //ClassDescriptor[] superclassInterfaces = superclassDescriptor.getInterfaces(); - //for (ClassDescriptor superclassInterface : superclassInterfaces) { - //interfaces.add(superclassInterface.getName()); - //} - String[] interfaces = parser.interfaces.toArray(new String[parser.interfaces.size()]); - return new ClassDescriptor(parser.className, - parser.packageName, - parser.superclass, - interfaces, - parser.isInterface, - false, - null, - jaxxObjectDescriptor, - classLoader, - publicMethods.toArray(new MethodDescriptor[publicMethods.size()]), - publicFields.toArray(new FieldDescriptor[publicFields.size()]), - declaredFields.toArray(new FieldDescriptor[declaredFields.size()]) - ) { -// publicFields.toArray(new FieldDescriptor[publicFields.size()])) { - - @Override - public FieldDescriptor getDeclaredFieldDescriptor(String name) throws NoSuchFieldException { - for (FieldDescriptor descriptor : declaredFieldDescriptors) { - if (name.equals(descriptor.getName())) { - log.info("Using a declared field descriptor [" + name + "] for " + getName()); - return descriptor; - } - } - throw new NoSuchFieldException(name); - } - - @Override - public MethodDescriptor getDeclaredMethodDescriptor(String name, ClassDescriptor... parameterTypes) throws NoSuchMethodException { - throw new NoSuchMethodException(name); - } - }; - } - - private void doParse(String displayName, Reader src) { - try { - JavaParser p = new JavaParser(src); - p.CompilationUnit(); - SimpleNode node = p.popNode(); - if (node != null) { - scanCompilationUnit(node); - return; - } - throw new CompilerException("Internal error: null node parsing Java file from " + src); - } catch (ParseException e) { - throw new CompilerException("Error parsing Java source code " + displayName + ": " + e.getMessage()); - } finally { - if (isInterface) { - - // remove super class - superclass = null; - } - - if (isEnum) { - - // super class is always Enum - - superclass = Enum.class.getName(); - } - } - } - - private void scanCompilationUnit(SimpleNode node) { - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - SimpleNode child = node.getChild(i); - scanCompilationUnitChild(child); - } - } - - private void scanCompilationUnitChild(SimpleNode child) { - int nodeType = child.getId(); - switch (nodeType) { - case JavaParserTreeConstants.JJTPACKAGEDECLARATION: - packageName = child.getChild(1).getText().trim(); - compiler.addImport(packageName + ".*"); - - // add implicit java.lnag namespace available - compiler.addImport("java.lang.*"); - break; - case JavaParserTreeConstants.JJTIMPORTDECLARATION: - String text = child.getText().trim(); - if (text.startsWith("import")) { - text = text.substring("import".length()).trim(); - } - if (text.endsWith(";")) { - text = text.substring(0, text.length() - 1); - } - if (log.isDebugEnabled()) { - log.debug("import " + text); - } - compiler.addImport(text); - break; - case JavaParserTreeConstants.JJTTYPEDECLARATION: - scanCompilationUnit(child); - break; - - case JavaParserTreeConstants.JJTCLASSORINTERFACEDECLARATION: - isInterface = child.firstToken.image.equals("interface"); - scanClass(child); - case JavaParserTreeConstants.JJTENUMDECLARATION: - isEnum = child.firstToken.image.equals("enum"); - scanClass(child); - break; - } - } - - // scans the main ClassOrInterfaceDeclaration - - private void scanClass(SimpleNode node) { -// boolean isInterface = node.firstToken.image.equals("interface"); - className = node.firstToken.next.image; - if (packageName != null) { - className = packageName + "." + className; - } - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - SimpleNode child = node.getChild(i); - int nodeType = child.getId(); - - if (nodeType == JavaParserTreeConstants.JJTIMPLEMENTSLIST) { - - if (log.isDebugEnabled()) { - log.debug("[" + className + "] Found a implements list " + child + " :: " + child.jjtGetNumChildren()); - } - // obtain interfaces - for (int j = 0; j < child.jjtGetNumChildren(); j++) { - String rawName = child.getChild(j).getText().trim(); - - addInterface(rawName); - } - continue; - } - if (nodeType == JavaParserTreeConstants.JJTEXTENDSLIST) { - - if (isInterface) { - - // obtain interfaces - for (int j = 0; j < child.jjtGetNumChildren(); j++) { - String rawName = child.getChild(j).getText().trim(); - - addInterface(rawName); - } - continue; - } - // this is an extends - assert child.jjtGetNumChildren() == 1 : "expected ExtendsList to have exactly one child for a non-interface class"; - String rawName = child.getChild(0).getText().trim(); - superclass = TagManager.resolveClassName(rawName, compiler); - if (superclass == null) { - throw new CompilerException("Could not find class: " + rawName); - } - if (log.isDebugEnabled()) { - log.debug("Set superClass = " + superclass); - } - } else if (nodeType == JavaParserTreeConstants.JJTCLASSORINTERFACEBODY) { - scanClassNode(child); - } - } - } - - protected void addInterface(String rawName) { - if (rawName.contains("<")) { - - // generic type - rawName = rawName.substring(0, rawName.indexOf("<")); - } - if (log.isDebugEnabled()) { - log.debug("[" + className + "] try to obtain type of interface " + rawName); - } - - String myInterface = resolveFullyQualifiedName(rawName); - if (myInterface == null) { - throw new CompilerException("Could not find interface: " + myInterface); - } - if (!interfaces.contains(myInterface)) { - if (log.isDebugEnabled()) { - log.debug("[" + className + "] add interface " + myInterface); - } - interfaces.add(myInterface); - } - } - - protected String resolveFullyQualifiedName(String rawName) { - String result; - - String realRawName = null; - String realParentRawName = null; - if (rawName.contains(".")) { - // this is a inner class - int index = rawName.lastIndexOf("."); - realParentRawName = rawName.substring(0, index); - realRawName = rawName.substring(index + 1); - - log.info("inner class detected ? " + realParentRawName + "//" + realRawName); - } - - log.info("try fqn = " + rawName); - result = TagManager.resolveClassName(rawName, compiler); - - if (result != null) { - // interface is detected fine (fqn was used or in good package ?) - return result; - } - - String suffix = "." + rawName; - - if (realParentRawName != null) { - suffix = "." + realParentRawName; - } - - for (String aClass : compiler.getImportedClasses()) { - - if (aClass.endsWith(suffix)) { - - // found the class as an already knwon class - - if (realRawName != null) { - aClass += "." + realRawName; - } - - return aClass; - } - } - - // try on packages - - Set<String> importedPackages = compiler.getImportedPackages(); - - for (String aClass : importedPackages) { - String fqn = aClass + rawName; - - log.info("try fqn = " + fqn); - result = TagManager.resolveClassName(fqn, compiler); - if (result != null) { - return result; - } - } - - // nothing was found - return null; - } - - - // scans class body nodes - - private void scanClassNode(SimpleNode node) { - int nodeType = node.getId(); - if (nodeType == JavaParserTreeConstants.JJTMETHODDECLARATION) { - String returnType = null; - 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.JJTRESULTTYPE) { - returnType = TagManager.resolveClassName(child.getText().trim(), compiler); - } else 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()); - } - } - } - methods.add(new MethodDescriptor(name, Modifier.PUBLIC, returnType, parameterTypes.toArray(new String[parameterTypes.size()]), compiler.getClassLoader())); // TODO: determine the actual modifiers - } else if (nodeType == JavaParserTreeConstants.JJTCLASSORINTERFACEDECLARATION) { - // TODO: handle inner classes - } else if (nodeType == JavaParserTreeConstants.JJTCONSTRUCTORDECLARATION) { - // TODO: handle constructors - } else if (nodeType == JavaParserTreeConstants.JJTFIELDDECLARATION) { - - String text = node.getText(); - String declaration = text; - String value = null; - int equals = text.indexOf("="); - if (equals != -1) { - value = declaration.substring(equals + 1).trim(); - declaration = declaration.substring(0, equals); - } - declaration = declaration.trim(); - - // get modifiers of the field - - int modifiers; - if (isInterface) { - modifiers = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL; - } else { - modifiers = getModifiers(node); - } - - log.info("field [" + declaration + "] modifiers == " + Modifier.toString(modifiers)); - - String[] declarationTokens = declaration.split("\\s"); - String name = declarationTokens[declarationTokens.length - 1]; - if (name.endsWith(";")) { - name = name.substring(0, name.length() - 1).trim(); - } - String cName = declarationTokens[declarationTokens.length - 2]; - String type = TagManager.resolveClassName(cName, compiler); - - FieldDescriptor descriptor = new FieldDescriptor( - name, - modifiers, - type, - compiler.getClassLoader() - ); - - if ("$jaxxObjectDescriptor".equals(name) && value != null) { - - // we are in a jaxx object, save the value of the field - - // value have this form : = "XXX";, we just want the XXX part - int firstIndex = value.indexOf("\""); - int lastIndex = value.lastIndexOf("\""); - - jaxxObjectDescriptorValue = value.substring(firstIndex + 1, lastIndex); - - log.info("detected a $jaxxObjectDescriptor = " + - jaxxObjectDescriptorValue); - - } - - if (Modifier.isPublic(modifiers)) { - fields.add(descriptor); - } else { - declaredFields.add(descriptor); - } - - } else { - for (int i = 0; i < node.jjtGetNumChildren(); i++) { - SimpleNode child = node.getChild(i); - scanClassNode(child); - } - } - } - - protected int getModifiers(SimpleNode node) { - SimpleNode parentNode = node.getParent(); - for (int i = 0; i < parentNode.jjtGetNumChildren(); i++) { - SimpleNode child = parentNode.getChild(i); - if (child.getId() == JavaParserTreeConstants.JJTMODIFIERS) { - String modifiersStr = child.getText().trim(); - int modifiers = scanModifiers(modifiersStr); - return modifiers; - } - } - return 0; - } - - protected int scanModifiers(String modifiersStr) { - int modifiers = 0; - if (modifiersStr.contains("public")) { - modifiers |= Modifier.PUBLIC; - } - if (modifiersStr.contains("protected")) { - modifiers |= Modifier.PROTECTED; - } - if (modifiersStr.contains("private")) { - modifiers |= Modifier.PRIVATE; - } - if (modifiersStr.contains("static")) { - modifiers |= Modifier.STATIC; - } - if (modifiersStr.contains("final")) { - modifiers |= Modifier.FINAL; - } - if (modifiersStr.contains("volatile")) { - modifiers |= Modifier.VOLATILE; - } - if (modifiersStr.contains("transient")) { - modifiers |= Modifier.TRANSIENT; - } - if (modifiersStr.contains("synchronized")) { - modifiers |= Modifier.SYNCHRONIZED; - } - if (modifiersStr.contains("abstract")) { - modifiers |= Modifier.ABSTRACT; - } - return modifiers; - } -} Deleted: trunk/jaxx-compiler/src/test/java/jaxx/compiler/reflect/JavaFileParserTest.java =================================================================== --- trunk/jaxx-compiler/src/test/java/jaxx/compiler/reflect/JavaFileParserTest.java 2010-10-05 08:37:01 UTC (rev 2106) +++ trunk/jaxx-compiler/src/test/java/jaxx/compiler/reflect/JavaFileParserTest.java 2010-10-05 08:55:56 UTC (rev 2107) @@ -1,286 +0,0 @@ -/* - * #%L - * JAXX :: Compiler - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2008 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package jaxx.compiler.reflect; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.nuiton.util.ApplicationConfig; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.Serializable; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; - -public class JavaFileParserTest { - - /** log */ - protected static final Log log = - LogFactory.getLog(JavaFileParserTest.class); - - /** test source root directory */ - public static File testSourceRoot; - - @BeforeClass - public static void initBaseDir() { - // get maven env basedir - String basedir = System.getenv("basedir"); - if (basedir == null) { - - // says basedir is where we start tests. - basedir = new File("").getAbsolutePath(); - } - testSourceRoot = new File(new File(basedir), - "src" + File.separator + - "test" + File.separator + "java"); - } - - @Test - public void parseJavaFileParserTest() throws Exception { - - ClassDescriptor descriptor = getDescriptor(JavaFileParserTest.class); - - assertInterfaces(descriptor); - assertSuperClass(descriptor, Object.class); - } - - @Test - public void parseMyEnum() throws Exception { - - ClassDescriptor descriptor = getDescriptor(MyEnum.class); - - assertInterfaces(descriptor, Serializable.class, MyInterface.class); - assertInterfaces(descriptor, MyEnum.class.getInterfaces()); - assertSuperClass(descriptor, Enum.class); - - assertIsAssignableFrom( - descriptor, - Serializable.class, - MyInterface.class, - Enum.class - ); - } - - @Test - public void parseMyInterface() throws Exception { - - ClassDescriptor descriptor = getDescriptor(MyInterface.class); - - assertInterfaces(descriptor); - assertSuperClass(descriptor, null); - } - - @Test - public void parseMyInterface2() throws Exception { - - ClassDescriptor descriptor = getDescriptor(MyInterface2.class); - - assertInterfaces(descriptor, MyInterface2.class.getInterfaces()); - assertInterfaces(descriptor, ApplicationConfig.OptionDef.class); - assertSuperClass(descriptor, null); - } - - @Test - public void parseMyInterface3() throws Exception { - - ClassDescriptor descriptor = getDescriptor(MyInterface3.class); - - assertInterfaces(descriptor, MyInterface3.class.getInterfaces()); - assertInterfaces(descriptor, ApplicationConfig.OptionDef.class, Iterable.class); - assertSuperClass(descriptor, null); - } - - @Test - public void parseMyAbstractClass() throws Exception { - - ClassDescriptor descriptor = getDescriptor(MyAbstractClass.class); - - assertInterfaces(descriptor, MyAbstractClass.class.getInterfaces()); - assertInterfaces(descriptor, MyInterface.class); - assertSuperClass(descriptor, Object.class); - assertIsAssignableFrom(descriptor, MyInterface.class, Object.class); - } - - @Test - public void parseMyClass() throws Exception { - - ClassDescriptor descriptor = getDescriptor(MyClass.class); - - assertInterfaces(descriptor, MyClass.class.getInterfaces()); - assertInterfaces(descriptor, Serializable.class); - assertSuperClass(descriptor, MyAbstractClass.class); - assertIsAssignableFrom(descriptor, - Serializable.class, - MyInterface.class, - MyAbstractClass.class); - - assertDeclaredField( - descriptor, - "serialVersionUID", - long.class, - Modifier.PRIVATE | Modifier.FINAL | Modifier.STATIC - ); - - assertDeclaredField( - descriptor, - "myPrivateStringField", - String.class, - Modifier.PRIVATE | Modifier.FINAL - ); - - assertDeclaredField( - descriptor, - "myProtectedStringField", - String.class, - Modifier.PROTECTED - ); - - assertField( - descriptor, - "myPublicStringField", - String.class, - Modifier.PUBLIC - ); - - } - - public static void assertField(ClassDescriptor descriptor, - String fieldName, - Class<?> fieldType, - int fieldModifiers) throws NoSuchFieldException { - FieldDescriptor fieldDescriptor = - descriptor.getFieldDescriptor(fieldName); - Assert.assertNotNull(fieldDescriptor); - ClassDescriptor type = fieldDescriptor.getType(); - Assert.assertNotNull(type); - Assert.assertEquals(fieldType.getName(), type.getName()); - int modifiers = fieldDescriptor.getModifiers(); - Assert.assertEquals(fieldModifiers, modifiers); - } - - public static void assertDeclaredField(ClassDescriptor descriptor, - String fieldName, - Class<?> fieldType, - int fieldModifiers) throws NoSuchFieldException { - FieldDescriptor fieldDescriptor = - descriptor.getDeclaredFieldDescriptor(fieldName); - Assert.assertNotNull(fieldDescriptor); - ClassDescriptor type = fieldDescriptor.getType(); - Assert.assertNotNull(type); - Assert.assertEquals(fieldType.getName(), type.getName()); - int modifiers = fieldDescriptor.getModifiers(); - Assert.assertEquals(fieldModifiers, modifiers); - } - - protected ClassDescriptor getDescriptor(Class<?> klass) throws Exception { - - String javaFilePath = - klass.getName().replaceAll("\\.", File.separator) + ".java"; - File src = new File(testSourceRoot, javaFilePath); - Assert.assertTrue(src.exists()); - - if (log.isInfoEnabled()) { - log.info("file to parse " + src); - } - - Reader reader = new InputStreamReader(new FileInputStream(src)); - try { - - ClassDescriptor descriptor = JavaFileParser.parseJavaFile( - klass.getSimpleName(), - reader, null); - - Assert.assertNotNull(descriptor); - if (log.isInfoEnabled()) { - log.info("loaded " + descriptor); - } - Assert.assertEquals(klass.getName(), descriptor.getName()); - return descriptor; - } finally { - - reader.close(); - } - } - - public static void assertIsAssignableFrom(ClassDescriptor descriptor, - Class<?>... interfaces) { - - for (Class<?> anInterface : interfaces) { - ClassDescriptor descriptor2 = - ClassDescriptorHelper.getClassDescriptor(anInterface); - Assert.assertNotNull(descriptor2); - boolean value = descriptor2.isAssignableFrom(descriptor); - Assert.assertTrue( - anInterface + " should be assignable from " + descriptor, - value - ); - } - } - - public static void assertInterfaces(ClassDescriptor descriptor, - Class<?>... interfaces) { - ClassDescriptor[] descriptors = descriptor.getInterfaces(); - Assert.assertEquals(interfaces.length, descriptors.length); - - List<String> doFind = new ArrayList<String>(); - for (Class<?> anInterface : interfaces) { - doFind.add(anInterface.getName()); - } - - for (ClassDescriptor descriptor1 : descriptors) { - String name = descriptor1.getName(); - Assert.assertTrue(doFind.contains(name)); - doFind.remove(name); - } - Assert.assertTrue( - "The follwing interfaces were not find found : " + doFind, - doFind.isEmpty() - ); - - } - - public static void assertSuperClass(ClassDescriptor descriptor, - Class<?> superClass) { - ClassDescriptor superDescriptor = descriptor.getSuperclass(); - if (superClass == null) { - Assert.assertNull( - "Should be null but was " + superDescriptor, - superDescriptor - ); - } else { - Assert.assertNotNull(superDescriptor); - Assert.assertEquals(superClass.getName(), superDescriptor.getName()); - } - - } - -} Modified: trunk/jaxx-compiler/src/test/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFileTest.java =================================================================== --- trunk/jaxx-compiler/src/test/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFileTest.java 2010-10-05 08:37:01 UTC (rev 2106) +++ trunk/jaxx-compiler/src/test/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFileTest.java 2010-10-05 08:55:56 UTC (rev 2107) @@ -25,7 +25,8 @@ package jaxx.compiler.reflect.resolvers; import jaxx.compiler.reflect.ClassDescriptor; -import jaxx.compiler.reflect.JavaFileParserTest; +import jaxx.compiler.reflect.ClassDescriptorHelper; +import jaxx.compiler.reflect.FieldDescriptor; import jaxx.compiler.reflect.MyAbstractClass; import jaxx.compiler.reflect.MyClass; import jaxx.compiler.reflect.MyEnum; @@ -42,13 +43,9 @@ import java.io.File; import java.io.Serializable; import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; -import static jaxx.compiler.reflect.JavaFileParserTest.assertDeclaredField; -import static jaxx.compiler.reflect.JavaFileParserTest.assertField; -import static jaxx.compiler.reflect.JavaFileParserTest.assertInterfaces; -import static jaxx.compiler.reflect.JavaFileParserTest.assertIsAssignableFrom; -import static jaxx.compiler.reflect.JavaFileParserTest.assertSuperClass; - /** * Tests the {@link ClassDescriptorResolverFromJavaFile} resolver. * @@ -61,16 +58,29 @@ protected static final Log log = LogFactory.getLog(ClassDescriptorResolverFromJavaFileTest.class); + + /** test source root directory */ + public static File testSourceRoot; + @BeforeClass public static void initBaseDir() { - JavaFileParserTest.initBaseDir(); + // get maven env basedir + String basedir = System.getenv("basedir"); + if (basedir == null) { + + // says basedir is where we start tests. + basedir = new File("").getAbsolutePath(); + } + testSourceRoot = new File(new File(basedir), + "src" + File.separator + + "test" + File.separator + "java"); } protected ClassDescriptor getDescriptor(Class<?> klass) throws Exception { String javaFilePath = klass.getName().replaceAll("\\.", File.separator) + ".java"; - File src = new File(JavaFileParserTest.testSourceRoot, javaFilePath); + File src = new File(testSourceRoot, javaFilePath); Assert.assertTrue(src.exists()); if (log.isInfoEnabled()) { @@ -205,4 +215,85 @@ } + public static void assertField(ClassDescriptor descriptor, + String fieldName, + Class<?> fieldType, + int fieldModifiers) throws NoSuchFieldException { + FieldDescriptor fieldDescriptor = + descriptor.getFieldDescriptor(fieldName); + Assert.assertNotNull(fieldDescriptor); + ClassDescriptor type = fieldDescriptor.getType(); + Assert.assertNotNull(type); + Assert.assertEquals(fieldType.getName(), type.getName()); + int modifiers = fieldDescriptor.getModifiers(); + Assert.assertEquals(fieldModifiers, modifiers); + } + + public static void assertDeclaredField(ClassDescriptor descriptor, + String fieldName, + Class<?> fieldType, + int fieldModifiers) throws NoSuchFieldException { + FieldDescriptor fieldDescriptor = + descriptor.getDeclaredFieldDescriptor(fieldName); + Assert.assertNotNull(fieldDescriptor); + ClassDescriptor type = fieldDescriptor.getType(); + Assert.assertNotNull(type); + Assert.assertEquals(fieldType.getName(), type.getName()); + int modifiers = fieldDescriptor.getModifiers(); + Assert.assertEquals(fieldModifiers, modifiers); + } + + + public static void assertIsAssignableFrom(ClassDescriptor descriptor, + Class<?>... interfaces) { + + for (Class<?> anInterface : interfaces) { + ClassDescriptor descriptor2 = + ClassDescriptorHelper.getClassDescriptor(anInterface); + Assert.assertNotNull(descriptor2); + boolean value = descriptor2.isAssignableFrom(descriptor); + Assert.assertTrue( + anInterface + " should be assignable from " + descriptor, + value + ); + } + } + + public static void assertInterfaces(ClassDescriptor descriptor, + Class<?>... interfaces) { + ClassDescriptor[] descriptors = descriptor.getInterfaces(); + Assert.assertEquals(interfaces.length, descriptors.length); + + List<String> doFind = new ArrayList<String>(); + for (Class<?> anInterface : interfaces) { + doFind.add(anInterface.getName()); + } + + for (ClassDescriptor descriptor1 : descriptors) { + String name = descriptor1.getName(); + Assert.assertTrue(doFind.contains(name)); + doFind.remove(name); + } + Assert.assertTrue( + "The follwing interfaces were not find found : " + doFind, + doFind.isEmpty() + ); + + } + + public static void assertSuperClass(ClassDescriptor descriptor, + Class<?> superClass) { + ClassDescriptor superDescriptor = descriptor.getSuperclass(); + if (superClass == null) { + Assert.assertNull( + "Should be null but was " + superDescriptor, + superDescriptor + ); + } else { + Assert.assertNotNull(superDescriptor); + Assert.assertEquals(superClass.getName(), superDescriptor.getName()); + } + + } + }
participants (1)
-
tchemit@users.nuiton.org