- Revision
- 751
- Author
- rfscholte
- Date
- 2010-09-27 14:51:14 -0500 (Mon, 27 Sep 2010)
Log Message
First step into refactoring the ClassLibrary Mechanism
Added Paths
- trunk/qdox/src/java/com/thoughtworks/qdox/library/
- trunk/qdox/src/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java
- trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLibrary.java
- trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLoaderLibrary.java
- trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassNameLibrary.java
- trunk/qdox/src/java/com/thoughtworks/qdox/library/OrderedClassLibraryBuilder.java
- trunk/qdox/src/java/com/thoughtworks/qdox/library/SortedClassLibraryBuilder.java
- trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceFolderLibrary.java
- trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceLibrary.java
Diff
Added: trunk/qdox/src/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java (0 => 751)
--- trunk/qdox/src/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java (rev 0) +++ trunk/qdox/src/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java 2010-09-27 19:51:14 UTC (rev 751) @@ -0,0 +1,55 @@ +package com.thoughtworks.qdox.library; + +import com.thoughtworks.qdox.JavaClassContext; +import com.thoughtworks.qdox.model.JavaClass; + +public abstract class AbstractClassLibrary + implements ClassLibrary +{ + private ClassLibrary parent; + + private JavaClassContext context; + + public AbstractClassLibrary() + { + } + + public AbstractClassLibrary( ClassLibrary parent ) + { + this.parent = parent; + } + + /** + * First checks if there's a JavaClass available in the private context by this name. Otherwise try to resolve it by + * the concrete class. If there's still no JavaClass, ask the parent (if available) to resolve it. + * + * @param name + * @return + */ + public final JavaClass getJavaClass( String name ) + { + JavaClass result = context.getClassByName( name ); + if ( result == null ) + { + result = resolveJavaClass( name ); + } + if ( result != null ) + { + context.add( result ); + } + else if ( parent != null ) + { + result = parent.getJavaClass( name ); + } + return result; + } + + /** + * The implementation should check it's sources to see if it can build a JavaClass Model If not, just return null; + * Once found it will be mapped, so there's no need to keep a reference to this object. + * + * @param name + * @return + */ + protected abstract JavaClass resolveJavaClass( String name ); +} Property changes on: trunk/qdox/src/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native
Added: trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLibrary.java (0 => 751)
--- trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLibrary.java (rev 0) +++ trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLibrary.java 2010-09-27 19:51:14 UTC (rev 751) @@ -0,0 +1,11 @@ +package com.thoughtworks.qdox.library; + +import java.io.Serializable; + +import com.thoughtworks.qdox.model.JavaClass; + +public interface ClassLibrary + extends Serializable +{ + public JavaClass getJavaClass( String name ); +} Property changes on: trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLibrary.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native
Added: trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLoaderLibrary.java (0 => 751)
--- trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLoaderLibrary.java (rev 0) +++ trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLoaderLibrary.java 2010-09-27 19:51:14 UTC (rev 751) @@ -0,0 +1,111 @@ +package com.thoughtworks.qdox.library; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import com.thoughtworks.qdox.model.JavaClass; +import com.thoughtworks.qdox.model.ModelBuilder; +import com.thoughtworks.qdox.parser.impl.BinaryClassParser; + +/** + * <strong>Important!! Be sure to add a classloader with the bootstrap classes.</strong> + * <p> + * Normally you can generate your classLibrary like this:<br/> + * <code> + * ClassLibrary classLibrary = new ClassLibrary(); + * classLibrary.addDefaultLoader(); + * </code> + * </p> + * <p> + * If you want full control over the classLoaders you might want to create your library like:<br/> + * <code> + * ClassLibrary classLibrary = new ClassLibrary( ClassLoader.getSystemClassLoader() ) + * </code> + * </p> + * + * @author <a href="" Walnes</a> + * @author Aslak Hellesøy + * @author Robert Scholte + */ +public class ClassLoaderLibrary + extends AbstractClassLibrary +{ + + private transient List classLoaders = new ArrayList(); // <java.lang.ClassLoader> + + private boolean defaultClassLoadersAdded = false; + + public ClassLoaderLibrary( ClassLibrary parent ) + { + super( parent ); + } + + public ClassLoaderLibrary( ClassLoader classLoader ) + { + this.classLoaders.add( classLoader ); + } + + public ClassLoaderLibrary( ClassLoader classLoader, ClassLibrary parent ) + { + super( parent ); + this.classLoaders.add( classLoader ); + } + + public void addClassLoader( ClassLoader classLoader ) + { + classLoaders.add( classLoader ); + } + + public void addDefaultLoader() + { + if ( !defaultClassLoadersAdded ) + { + classLoaders.add( getClass().getClassLoader() ); + classLoaders.add( Thread.currentThread().getContextClassLoader() ); + } + defaultClassLoadersAdded = true; + } + + /** + * {...@inheritdoc} + */ + protected JavaClass resolveJavaClass( String name ) + { + JavaClass result = null; + Iterator iter = classLoaders.iterator(); + while ( iter.hasNext() ) + { + ClassLoader classLoader = (ClassLoader) iter.next(); + try + { + Class clazz = classLoader.loadClass( name ); + ModelBuilder builder = new ModelBuilder(); + BinaryClassParser parser = new BinaryClassParser( clazz, builder ); + if ( parser.parse() ) + { + result = builder.getSource().getClasses()[0]; + } + break; + } + catch ( ClassNotFoundException e ) + { + } + } + return result; + } + + private void readObject( ObjectInputStream in ) + throws IOException, ClassNotFoundException + { + in.defaultReadObject(); + classLoaders = new ArrayList(); + if ( defaultClassLoadersAdded ) + { + defaultClassLoadersAdded = false; + addDefaultLoader(); + } + } +} Property changes on: trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassLoaderLibrary.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native
Added: trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassNameLibrary.java (0 => 751)
--- trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassNameLibrary.java (rev 0) +++ trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassNameLibrary.java 2010-09-27 19:51:14 UTC (rev 751) @@ -0,0 +1,34 @@ +package com.thoughtworks.qdox.library; + +import com.thoughtworks.qdox.model.JavaClass; +import com.thoughtworks.qdox.model.JavaSource; +import com.thoughtworks.qdox.model.ModelBuilder; +import com.thoughtworks.qdox.parser.structs.ClassDef; + +/** + * This library always resolve a className by generating an empty JavaClass Model + */ +public class ClassNameLibrary + extends AbstractClassLibrary +{ + + public ClassNameLibrary() + { + } + + /** + * {...@inheritdoc} + */ + protected JavaClass resolveJavaClass( String name ) + { + ModelBuilder unknownBuilder = new ModelBuilder(); + ClassDef classDef = new ClassDef(); + classDef.name = name; + unknownBuilder.beginClass( classDef ); + unknownBuilder.endClass(); + JavaSource unknownSource = unknownBuilder.getSource(); + JavaClass result = unknownSource.getClasses()[0]; + return result; + } + +} Property changes on: trunk/qdox/src/java/com/thoughtworks/qdox/library/ClassNameLibrary.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native
Added: trunk/qdox/src/java/com/thoughtworks/qdox/library/OrderedClassLibraryBuilder.java (0 => 751)
--- trunk/qdox/src/java/com/thoughtworks/qdox/library/OrderedClassLibraryBuilder.java (rev 0) +++ trunk/qdox/src/java/com/thoughtworks/qdox/library/OrderedClassLibraryBuilder.java 2010-09-27 19:51:14 UTC (rev 751) @@ -0,0 +1,86 @@ +package com.thoughtworks.qdox.library; + +import java.io.File; +import java.io.InputStream; +import java.io.Reader; + +/** + * This library resolves JavaClasses in the order in which class sources are added. + * + * @author Robert Scholte + */ +public class OrderedClassLibraryBuilder +{ + + private ClassLibrary classLibrary; + + private boolean debugLexer; + + private boolean debugParser; + + public OrderedClassLibraryBuilder() + { + classLibrary = new ClassNameLibrary(); + } + + public OrderedClassLibraryBuilder addClassLoader( ClassLoader classLoader ) + { + if ( !( classLibrary instanceof ClassLoaderLibrary ) ) + { + classLibrary = new ClassLoaderLibrary( classLibrary ); + } + ( (ClassLoaderLibrary) classLibrary ).addClassLoader( classLoader ); + return this; + } + + public OrderedClassLibraryBuilder addSourceFolder( File sourceFolder ) + { + if ( !( classLibrary instanceof SourceFolderLibrary ) ) + { + classLibrary = new SourceFolderLibrary( classLibrary ); + } + ( (SourceFolderLibrary) classLibrary ).addSourceFolder( sourceFolder ); + return this; + } + + public OrderedClassLibraryBuilder addSource( InputStream stream ) + { + if ( !( classLibrary instanceof SourceLibrary ) ) + { + classLibrary = new SourceLibrary( classLibrary ); + } + SourceLibrary sourceLibrary = (SourceLibrary) classLibrary; + sourceLibrary.addSource( stream ); + sourceLibrary.setDebugLexer( debugLexer ); + sourceLibrary.setDebugParser( debugParser ); + return this; + } + + public OrderedClassLibraryBuilder addSource( Reader reader ) + { + if ( !( classLibrary instanceof SourceLibrary ) ) + { + classLibrary = new SourceLibrary( classLibrary ); + } + ( (SourceLibrary) classLibrary ).addSource( reader ); + return this; + } + + public OrderedClassLibraryBuilder setDebugLexer( boolean debugLexer ) + { + this.debugLexer = debugLexer; + return this; + } + + public OrderedClassLibraryBuilder setDebugParser( boolean debugParser ) + { + this.debugParser = debugParser; + return this; + } + + public ClassLibrary getClassLibrary() + { + return classLibrary; + } + +} Property changes on: trunk/qdox/src/java/com/thoughtworks/qdox/library/OrderedClassLibraryBuilder.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native
Added: trunk/qdox/src/java/com/thoughtworks/qdox/library/SortedClassLibraryBuilder.java (0 => 751)
--- trunk/qdox/src/java/com/thoughtworks/qdox/library/SortedClassLibraryBuilder.java (rev 0) +++ trunk/qdox/src/java/com/thoughtworks/qdox/library/SortedClassLibraryBuilder.java 2010-09-27 19:51:14 UTC (rev 751) @@ -0,0 +1,72 @@ +package com.thoughtworks.qdox.library; + +import java.io.File; +import java.io.InputStream; +import java.io.Reader; + +/** + * + * + * @author Robert Scholte + * + */ +public class SortedClassLibraryBuilder +{ + private ClassNameLibrary classNameLibrary; + + private ClassLoaderLibrary classLoaderLibrary; + + private SourceFolderLibrary sourceFolderLibrary; + + private SourceLibrary sourceLibrary; + + public SortedClassLibraryBuilder() + { + classNameLibrary = new ClassNameLibrary(); + classLoaderLibrary = new ClassLoaderLibrary( classNameLibrary ); + sourceFolderLibrary = new SourceFolderLibrary( classLoaderLibrary ); + sourceLibrary = new SourceLibrary( sourceFolderLibrary ); + } + + public SortedClassLibraryBuilder addClassLoader( ClassLoader classLoader ) + { + classLoaderLibrary.addClassLoader( classLoader ); + return this; + } + + public SortedClassLibraryBuilder addSourceFolder( File sourceFolder ) + { + sourceFolderLibrary.addSourceFolder( sourceFolder ); + return this; + } + + public SortedClassLibraryBuilder addSource( InputStream stream ) + { + sourceLibrary.addSource( stream ); + return this; + } + + public SortedClassLibraryBuilder addSource( Reader reader ) + { + sourceLibrary.addSource( reader ); + return this; + } + + public SortedClassLibraryBuilder setDebugLexer( boolean debugLexer ) + { + sourceFolderLibrary.setDebugLexer( debugLexer ); + sourceLibrary.setDebugLexer( debugLexer ); + return this; + } + + public SortedClassLibraryBuilder setDebugParser( boolean debugParser ) + { + sourceFolderLibrary.setDebugParser( debugParser ); + sourceLibrary.setDebugParser( debugParser ); + return this; + } + + public ClassLibrary getClassLibrary() { + return sourceLibrary; + } +} Property changes on: trunk/qdox/src/java/com/thoughtworks/qdox/library/SortedClassLibraryBuilder.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native
Added: trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceFolderLibrary.java (0 => 751)
--- trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceFolderLibrary.java (rev 0) +++ trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceFolderLibrary.java 2010-09-27 19:51:14 UTC (rev 751) @@ -0,0 +1,63 @@ +package com.thoughtworks.qdox.library; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import com.thoughtworks.qdox.model.JavaClass; + +public class SourceFolderLibrary + extends SourceLibrary +{ + private List sourceFolders = new ArrayList(); // <java.io.File> + + public SourceFolderLibrary( ClassLibrary parent ) + { + super( parent ); + } + + public SourceFolderLibrary( File sourceFolder ) + { + super(); + this.sourceFolders.add( sourceFolder ); + } + + public SourceFolderLibrary( File sourceFolder, ClassLibrary parent ) + { + super( parent ); + this.sourceFolders.add( sourceFolder ); + } + + public void addSourceFolder( File sourceFolder ) + { + this.sourceFolders.add( sourceFolder ); + } + + /** + * {...@inheritdoc} + */ + protected JavaClass resolveJavaClass( String className ) + { + JavaClass result = null; + for ( Iterator iterator = sourceFolders.iterator(); iterator.hasNext(); ) + { + File sourceFolder = (File) iterator.next(); + String mainClassName = className.split( "\\$" )[0]; + File classFile = new File( sourceFolder, mainClassName.replace( '.', File.separatorChar ) + ".java" ); + if ( classFile.exists() && classFile.isFile() ) + { + try + { + result = parse( new FileReader( classFile ) ); + } + catch ( FileNotFoundException e ) + { + } + } + } + return result; + } +} Property changes on: trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceFolderLibrary.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native
Added: trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceLibrary.java (0 => 751)
--- trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceLibrary.java (rev 0) +++ trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceLibrary.java 2010-09-27 19:51:14 UTC (rev 751) @@ -0,0 +1,95 @@ +package com.thoughtworks.qdox.library; + +import java.io.InputStream; +import java.io.Reader; +import java.util.Hashtable; +import java.util.Map; + +import com.thoughtworks.qdox.model.JavaClass; +import com.thoughtworks.qdox.model.ModelBuilder; +import com.thoughtworks.qdox.parser.Lexer; +import com.thoughtworks.qdox.parser.ParseException; +import com.thoughtworks.qdox.parser.impl.JFlexLexer; +import com.thoughtworks.qdox.parser.impl.Parser; + +public class SourceLibrary + extends AbstractClassLibrary +{ + // parser and unused javaclasses + private Map javaClassesMap = new Hashtable(); // <java.lang.String, com.thoughtworks.qdox.model.JavaClass> + + private boolean debugLexer; + + private boolean debugParser; + + public SourceLibrary() + { + super(); + } + + public SourceLibrary( ClassLibrary parent ) + { + super( parent ); + } + + public void addSource( Reader reader ) + throws ParseException + { + JavaClass clazz = parse( reader ); + javaClassesMap.put( clazz.getFullyQualifiedName(), clazz ); + } + + public void addSource( InputStream stream ) + throws ParseException + { + JavaClass clazz = parse( stream ); + javaClassesMap.put( clazz.getFullyQualifiedName(), clazz ); + } + + protected JavaClass parse( Reader reader ) + throws ParseException + { + return parse( new JFlexLexer( reader ) ); + } + + protected JavaClass parse( InputStream stream ) + throws ParseException + { + return parse( new JFlexLexer( stream ) ); + } + + private JavaClass parse( Lexer lexer ) + throws ParseException + { + JavaClass result = null; + ModelBuilder builder = new ModelBuilder(); + Parser parser = new Parser( lexer, builder ); + parser.setDebugLexer( debugLexer ); + parser.setDebugParser( debugParser ); + if ( parser.parse() ) + { + result = builder.getSource().getClasses()[0]; + } + return result; + } + + /** + * {...@inheritdoc} + */ + protected JavaClass resolveJavaClass( String name ) + { + // abstractLibrary only calls this when it can't find the source itself. + // it will take over the reference + return (JavaClass) javaClassesMap.remove( name ); + } + + public void setDebugLexer( boolean debugLexer ) + { + this.debugLexer = debugLexer; + } + + public void setDebugParser( boolean debugParser ) + { + this.debugParser = debugParser; + } +} Property changes on: trunk/qdox/src/java/com/thoughtworks/qdox/library/SourceLibrary.java ___________________________________________________________________ Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native
To unsubscribe from this list please visit:
