- Revision
- 1429
- Author
- rfscholte
- Date
- 2011-10-19 15:47:09 -0500 (Wed, 19 Oct 2011)
Log Message
Support nested classes for binaries
Modified Paths
- trunk/qdox/src/main/java/com/thoughtworks/qdox/library/ClassLoaderLibrary.java
- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/impl/DefaultJavaClass.java
- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/impl/DefaultJavaSource.java
- trunk/qdox/src/main/java/com/thoughtworks/qdox/parser/impl/BinaryClassParser.java
- trunk/qdox/src/test/java/com/thoughtworks/qdox/model/impl/DefaultJavaClassIT.java
Diff
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/library/ClassLoaderLibrary.java (1428 => 1429)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/library/ClassLoaderLibrary.java 2011-10-19 20:00:39 UTC (rev 1428) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/library/ClassLoaderLibrary.java 2011-10-19 20:47:09 UTC (rev 1429) @@ -142,6 +142,10 @@ try { Class<?> clazz = classLoader.loadClass( name ); + if ( clazz.getDeclaringClass() != null ) + { + clazz = clazz.getDeclaringClass(); + } ModelBuilder builder = getModelBuilder(); BinaryClassParser parser = new BinaryClassParser( clazz, builder ); if ( parser.parse() )
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/impl/DefaultJavaClass.java (1428 => 1429)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/impl/DefaultJavaClass.java 2011-10-19 20:00:39 UTC (rev 1428) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/impl/DefaultJavaClass.java 2011-10-19 20:47:09 UTC (rev 1429) @@ -339,7 +339,7 @@ */ public String getValue() { - return getName(); + return getCanonicalName().substring( getSource().getClassNamePrefix().length() ); } /*
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/impl/DefaultJavaSource.java (1428 => 1429)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/impl/DefaultJavaSource.java 2011-10-19 20:00:39 UTC (rev 1428) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/impl/DefaultJavaSource.java 2011-10-19 20:47:09 UTC (rev 1429) @@ -80,16 +80,12 @@ this.url = "" } - /* (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaSource#getURL() - */ + /** {@inheritDoc} */ public URL getURL() { return url; } - /* (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaSource#getPackage() - */ + /** {@inheritDoc} */ public JavaPackage getPackage() { return pkg; } @@ -102,9 +98,7 @@ imports.add(imp); } - /* (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaSource#getImports() - */ + /** {@inheritDoc} */ public List<String> getImports() { return imports; } @@ -131,20 +125,14 @@ return getCodeBlock(); } - /* - * (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaSource#resolveType(java.lang.String) - */ + /** {@inheritDoc} */ public String resolveType( String typeName ) { return resolveFullyQualifiedName( typeName ); } - /* - * (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaClassParent#resolveFullyQualifiedName(java.lang.String) - */ - public String resolveFullyQualifiedName( String name ) + /** {@inheritDoc} */ + public String resolveFullyQualifiedName( String name ) { String result = resolvedTypeCache.get( name ); if ( result == null ) @@ -158,10 +146,7 @@ return result; } - /* - * (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaClassParent#resolveCanonicalName(java.lang.String) - */ + /** {@inheritDoc} */ public String resolveCanonicalName( String name ) { String className = resolveFullyQualifiedName( name ); @@ -189,99 +174,116 @@ * @param typeName the name to resolve * @return the resolved type name, otherwise <code>null</code> */ - private String resolveTypeInternal(String typeName) { + private String resolveTypeInternal( String typeName ) + { String resolvedName = null; - lookup : { + lookup: + { // primitive types - if(PRIMITIVE_TYPES.contains( typeName )) { + if ( PRIMITIVE_TYPES.contains( typeName ) ) + { resolvedName = typeName; break lookup; } String outerName = typeName; - String nestedName = typeName.replace('.', '$'); + String nestedName = typeName.replace( '.', '$' ); int dotpos = typeName.indexOf( '.' ); - if(dotpos >= 0) { + if ( dotpos >= 0 ) + { outerName = typeName.substring( 0, dotpos ); } - + // Check single-type-import with fully qualified name resolvedName = resolveImportedType( typeName, nestedName, true ); - - if(resolvedName != null) { + + if ( resolvedName != null ) + { break lookup; } - + // Check single-type-import with outer name resolvedName = resolveImportedType( outerName, nestedName, false ); - - if(resolvedName != null) { + + if ( resolvedName != null ) + { break lookup; } - + // check for class in the same package - if (getPackage() != null) { + if ( getPackage() != null ) + { resolvedName = resolveFullyQualifiedType( getPackageName() + '.' + typeName ); - - if(resolvedName != null) { + + if ( resolvedName != null ) + { break lookup; } } // check for a class globally resolvedName = resolveFullyQualifiedType( typeName ); - - if(resolvedName != null) { + + if ( resolvedName != null ) + { break lookup; } // check for a class in the same package resolvedName = resolveFromLibrary( getClassNamePrefix() + nestedName ); - if(resolvedName != null) { + if ( resolvedName != null ) + { break lookup; } - + // try java.lang.* resolvedName = resolveFromLibrary( "java.lang." + nestedName ); - if(resolvedName != null) { + if ( resolvedName != null ) + { break lookup; } - + // Check type-import-on-demand resolvedName = resolveImportedType( "*", nestedName, false ); - if(resolvedName != null) { + if ( resolvedName != null ) + { break lookup; } } - + return resolvedName; } - private String resolveImportedType( String importSpec, String typeName, boolean fullMatch ) { + private String resolveImportedType( String importSpec, String typeName, boolean fullMatch ) + { String resolvedName = null; String dotSuffix = "." + importSpec; - - for (String imprt : getImports()) { - //static imports can refer to inner classes - if( imprt.startsWith( "static " ) ) + + for ( String imprt : getImports() ) + { + // static imports can refer to inner classes + if ( imprt.startsWith( "static " ) ) { imprt = imprt.substring( 7 ); } - if (imprt.equals(importSpec) || (!fullMatch && imprt.endsWith(dotSuffix))) { - String candidateName = imprt.substring( 0, imprt.length() - importSpec.length()) + typeName; + if ( imprt.equals( importSpec ) || ( !fullMatch && imprt.endsWith( dotSuffix ) ) ) + { + String candidateName = imprt.substring( 0, imprt.length() - importSpec.length() ) + typeName; resolvedName = resolveFullyQualifiedType( candidateName ); - if(resolvedName == null && !"*".equals(importSpec)) { - resolvedName = candidateName; + if ( resolvedName == null && !"*".equals( importSpec ) ) + { + resolvedName = candidateName; } - if(resolvedName != null) { + if ( resolvedName != null ) + { break; } - } + } } - + return resolvedName; } @@ -309,9 +311,7 @@ return null; } - /* (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaSource#getClassNamePrefix() - */ + /** {@inheritDoc} */ public String getClassNamePrefix() { return ( pkg == null ? "" : pkg.getName() + '.' ); } @@ -320,9 +320,7 @@ return this; } - /* (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaSource#getNestedClassByName(java.lang.String) - */ + /** {@inheritDoc} */ public JavaClass getNestedClassByName(String name) { JavaClass result = null; @@ -335,6 +333,7 @@ return result; } + /** {@inheritDoc} */ public JavaClass getClassByName(String name) { JavaClass result = null; @@ -344,27 +343,22 @@ result = JavaModelUtils.getClassByName( candidateCls, name ); if ( result != null ) { - result = candidateCls; break; } } return result; } - /* (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaSource#getJavaClassLibrary() - */ + /** {@inheritDoc} */ public ClassLibrary getJavaClassLibrary() { return classLibrary; } - /* (non-Javadoc) - * @see com.thoughtworks.qdox.model.JavaSource#getPackageName() - */ + /** {@inheritDoc} */ public String getPackageName() { - return (pkg == null ? "" : pkg.getName()); + return ( pkg == null ? "" : pkg.getName() ); } /** @@ -379,14 +373,15 @@ private ModelWriter getModelWriter() { - ModelWriter result; - if (modelWriterFactory != null) { + ModelWriter result; + if ( modelWriterFactory != null ) + { result = modelWriterFactory.newInstance(); } - else { + else + { result = new DefaultModelWriter(); } return result; } - -} +} \ No newline at end of file
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/parser/impl/BinaryClassParser.java (1428 => 1429)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/parser/impl/BinaryClassParser.java 2011-10-19 20:00:39 UTC (rev 1428) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/parser/impl/BinaryClassParser.java 2011-10-19 20:47:09 UTC (rev 1429) @@ -68,7 +68,7 @@ private void addClass( Class<?> clazz ) { - ClassDef classDef = new ClassDef( getClassName( clazz.getName() ) ); + ClassDef classDef = new ClassDef( clazz.getSimpleName() ); // Set the extended class and interfaces. Class<?>[] interfaces = clazz.getInterfaces(); @@ -128,6 +128,13 @@ { addField( fields[i] ); } + + Class<?>[] classes = clazz.getDeclaredClasses(); + for ( int i = 0; i < classes.length; i++ ) + { + addClass( classes[i] ); + } + binaryBuilder.endClass(); } @@ -221,10 +228,4 @@ int lastDot = fullClassName.lastIndexOf( '.' ); return lastDot == -1 ? "" : fullClassName.substring( 0, lastDot ); } - - private String getClassName( String fullClassName ) - { - int lastDot = fullClassName.lastIndexOf( '.' ); - return lastDot == -1 ? fullClassName : fullClassName.substring( lastDot + 1 ); - } }
Modified: trunk/qdox/src/test/java/com/thoughtworks/qdox/model/impl/DefaultJavaClassIT.java (1428 => 1429)
--- trunk/qdox/src/test/java/com/thoughtworks/qdox/model/impl/DefaultJavaClassIT.java 2011-10-19 20:00:39 UTC (rev 1428) +++ trunk/qdox/src/test/java/com/thoughtworks/qdox/model/impl/DefaultJavaClassIT.java 2011-10-19 20:47:09 UTC (rev 1429) @@ -27,8 +27,6 @@ assertTrue( clazz instanceof DefaultJavaClass<?> ); JavaClass superClass = clazz.getSuperJavaClass(); assertEquals( "java.util.AbstractSet", superClass.getFullyQualifiedName() ); - assertEquals( "java.util.AbstractSet", superClass.getCanonicalName() ); - assertEquals( "java.util.AbstractSet", superClass.getValue() ); } @Test @@ -41,5 +39,18 @@ assertTrue( hashSetClass.isA( setClass ) ); assertTrue( hashSetClass.isA( "java.util.Set" ) ); } + + @Test + public void testNames() + { + //subclass + JavaClass entryClass = library.getJavaClass( "java.util.Map$Entry" ); + assertTrue( entryClass instanceof DefaultJavaClass<?> ); + + assertEquals( "java.util.Map$Entry", entryClass.getFullyQualifiedName() ); + assertEquals( "java.util.Map.Entry", entryClass.getCanonicalName() ); + assertEquals( "Map.Entry", entryClass.getValue() ); + + } }
To unsubscribe from this list please visit:
