- Revision
- 1270
- Author
- rfscholte
- Date
- 2011-07-21 15:53:40 -0500 (Thu, 21 Jul 2011)
Log Message
Fix QDOX-233: support JavaClass.getConanicalName()
Modified Paths
- trunk/qdox/src/main/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java
- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/DefaultJavaClass.java
- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/DefaultJavaSource.java
- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaClass.java
- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaClassParent.java
- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaMethod.java
- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaParameter.java
- trunk/qdox/src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java
Diff
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java (1269 => 1270)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java 2011-07-16 11:48:28 UTC (rev 1269) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/library/AbstractClassLibrary.java 2011-07-21 20:53:40 UTC (rev 1270) @@ -74,7 +74,7 @@ * the concrete class. If there's still no JavaClass, ask the parent (if available) to resolve it. * * @param name - * @return + * @return */ public final JavaClass getJavaClass( String name ) { return getJavaClass( name, false ); @@ -265,7 +265,7 @@ * This method is used to detect if there's a match with this classname. * The name could be constructed based on imports and inner class paths. * - * @param name the fully qualifed name of the class + * @param name the fully qualified name of the class * @return true if this ClassLibrary has a reference to this class. */ protected abstract boolean containsClassReference( String name ); @@ -279,9 +279,14 @@ this.modelBuilderFactory = factory; } - public final void setModelWriterFactory( ModelWriterFactory modelWriterFactory ) + /** + * Set the ModelWriterFactory for this class. + * + * @param factory the ModelWriterFactory + */ + public final void setModelWriterFactory( ModelWriterFactory factory ) { - this.modelWriterFactory = modelWriterFactory; + this.modelWriterFactory = factory; }
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/DefaultJavaClass.java (1269 => 1270)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/DefaultJavaClass.java 2011-07-16 11:48:28 UTC (rev 1269) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/DefaultJavaClass.java 2011-07-21 20:53:40 UTC (rev 1270) @@ -277,6 +277,15 @@ : getPackage() != null ? ( getPackage().getName() + "." ) : "" ) + getName(); } + + /* + * (non-Javadoc) + * @see com.thoughtworks.qdox.model.JavaClass#getCanonicalName() + */ + public String getCanonicalName() + { + return getFullyQualifiedName().replace( '$', '.' ); + } /* * (non-Javadoc) @@ -320,6 +329,32 @@ } return getParent().resolveType( typeName ); } + + public String resolveCanonicalName( String name ) + { + // Maybe it's an inner class? + for ( JavaClass innerClass : getNestedClasses() ) + { + if ( innerClass.getName().equals( name ) ) + { + return innerClass.getName(); + } + } + return getParent().resolveCanonicalName( name ); + } + + public String resolveFullyQualifiedName( String name ) + { + // Maybe it's an inner class? + for ( JavaClass innerClass : getNestedClasses() ) + { + if ( innerClass.getName().equals( name ) ) + { + return innerClass.getFullyQualifiedName(); + } + } + return getParent().resolveFullyQualifiedName( name ); + } /* (non-Javadoc) * @see com.thoughtworks.qdox.model.JavaClass#getClassNamePrefix()
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/DefaultJavaSource.java (1269 => 1270)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/DefaultJavaSource.java 2011-07-16 11:48:28 UTC (rev 1269) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/DefaultJavaSource.java 2011-07-21 20:53:40 UTC (rev 1270) @@ -133,18 +133,34 @@ */ public String resolveType( String typeName ) { - String result = resolvedTypeCache.get( typeName ); + return resolveFullyQualifiedName( typeName ); + } + + public String resolveFullyQualifiedName( String name ) + { + String result = resolvedTypeCache.get( name ); if ( result == null ) { - result = resolveTypeInternal( typeName ); + result = resolveTypeInternal( name ); if ( result != null ) { - resolvedTypeCache.put( typeName, result ); + resolvedTypeCache.put( name, result ); } } return result; } + public String resolveCanonicalName( String name ) + { + String className = resolveFullyQualifiedName( name ); + String result = null; + if ( className != null ) + { + result = className.replace( '$', '.' ); + } + return result; + } + /** * Resolves a type name * <p>
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaClass.java (1269 => 1270)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaClass.java 2011-07-16 11:48:28 UTC (rev 1269) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaClass.java 2011-07-21 20:53:40 UTC (rev 1270) @@ -19,7 +19,6 @@ * under the License. */ -import java.lang.reflect.Member; import java.lang.reflect.Modifier; import java.util.List; @@ -117,9 +116,25 @@ * <li>implicit imports</li> * </ul> * - * @return the resolved name, otherwise the name itself. + * @return the resolved name, otherwise <code>null</code>. */ String resolveType( String name ); + + /** + * The name can be both absolute (including the package) or relative (matching a subclass or an import). + * + * @param name + * @return + */ + String resolveCanonicalName( String name ); + + /** + * The name can be both absolute (including the package) or relative (matching a subclass or an import). + * + * @param name the name to resolve + * @return the resolved fully qualified name, otherwise <code>null</code> + */ + String resolveFullyQualifiedName( String name ); /** * If this class has a package, it will return the package name, followed by a "."(dot). @@ -293,6 +308,13 @@ String getName(); /** + * Equivalent of (@link {@link java.lang.Class#getCanonicalName()}. + * + * @return the canonical name of this class + */ + String getCanonicalName(); + + /** * If there's a reference to this class, use the value used in the code. Otherwise return the simple name. * When including all imports, you should be safe to use this method. * This won't return generics, so it's java1.4 safe.
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaClassParent.java (1269 => 1270)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaClassParent.java 2011-07-16 11:48:28 UTC (rev 1269) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaClassParent.java 2011-07-21 20:53:40 UTC (rev 1270) @@ -30,6 +30,10 @@ * be resolved */ String resolveType(String typeName); + + String resolveCanonicalName(String typeName); + + String resolveFullyQualifiedName(String typeName); JavaSource getParentSource();
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaMethod.java (1269 => 1270)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaMethod.java 2011-07-16 11:48:28 UTC (rev 1269) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaMethod.java 2011-07-21 20:53:40 UTC (rev 1270) @@ -19,6 +19,7 @@ * under the License. */ +import java.lang.reflect.Method; import java.util.List; public interface JavaMethod extends JavaAnnotatedElement, JavaMember, JavaModel, JavaGenericDeclaration @@ -38,8 +39,10 @@ List<Type> getExceptions(); /** + * Equivalent of {@link Method#isVarArgs()} * - * @return true is this method conains varArgs + * @return <code>true</code> if this method was declared to take a variable number of arguments, + * otherwise <code>false</code> */ boolean isVarArgs(); @@ -73,26 +76,46 @@ boolean signatureMatches( String name, List<Type> parameterTypes, boolean varArg ); /** - * @return true if this method is a Java Bean accessor + * Returns <code>true</code> if this method follows the bean convention of being an accessor. + * + * <pre> + * public String getName(); // true + * public boolean isValid() // true + * public String getName( String def ); // false, it has a parameter + * public String gettingUp(); // false, 'get' is not followed by an uppercase character + * public boolean isolate(); // false, 'is' is not followed by an uppercase character + * public static String getName(); // false, it is static + * </pre> + * + * @return <code>true</code> if this method is a Java Bean accessor, otherwise <code>false</code> * @since 1.3 */ boolean isPropertyAccessor(); /** - * @return true if this method is a Java Bean accessor + * Returns <code>true</code> if this method follows the bean convention of being an mutator. + * + * <pre> + * public void setName(String name); // true + * public void setUp(); // false, it has no parameter + * public void settingUp(String def); // false, 'set' is not followed by an uppercase character + * public static void setName(String name); // false, it is static + * </pre> + * + * @return <code>true</code> if this method is a Java Bean mutator, otherwise <code>false</code> * @since 1.3 */ boolean isPropertyMutator(); /** - * @return the type of the property this method represents, or null if this method + * @return the type of the property this method represents, or <code>null</code> if this method * is not a property mutator or property accessor. * @since 1.3 */ Type getPropertyType(); /** - * @return the name of the property this method represents, or null if this method + * @return the name of the property this method represents, or <code>null</code> if this method * is not a property mutator or property accessor. * @since 1.3 */
Modified: trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaParameter.java (1269 => 1270)
--- trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaParameter.java 2011-07-16 11:48:28 UTC (rev 1269) +++ trunk/qdox/src/main/java/com/thoughtworks/qdox/model/JavaParameter.java 2011-07-21 20:53:40 UTC (rev 1270) @@ -28,16 +28,35 @@ public interface JavaParameter extends JavaAnnotatedElement { + /** + * + * @return the name of the parameter + */ String getName(); + /** + * + * @return the type of this parameter + */ Type getType(); + /** + * Returns the declaring method of this parameter + * + * @return the declaring method + */ JavaMethod getParentMethod(); + /** + * The declaring class of the declaring method of this parameter. + * + * @return the declaring class of the declaring method + */ JavaClass getParentClass(); /** - * Is this a Java 5 var args type specified using three dots. e.g. void doStuff(Object... thing) + * Is this a Java 5 var args type specified using three dots. e.g. <code>void doStuff(Object... thing)</code> + * * @since 1.6 */ boolean isVarArgs();
Modified: trunk/qdox/src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java (1269 => 1270)
--- trunk/qdox/src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java 2011-07-16 11:48:28 UTC (rev 1269) +++ trunk/qdox/src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java 2011-07-21 20:53:40 UTC (rev 1270) @@ -1357,6 +1357,23 @@ builder.addSource(new StringReader( source )); JavaClass cls = builder.getClassByName( "GreetingService" ); assertEquals( 1, cls.getAnnotations().size() ); - } + + public void testCanonicalName() + throws Exception + { + String source = + "package com.foo;\b" + "public class Outer {\n" + "public class Inner {\n" + "public class Core {}\n" + + "}\n" + "}\n"; + builder.addSource( new StringReader( source ) ); + JavaClass cls = builder.getClasses().get( 0 ); + assertEquals( "com.foo.Outer", cls.getFullyQualifiedName() ); + assertEquals( "com.foo.Outer", cls.getCanonicalName() ); + cls = cls.getNestedClassByName( "Inner" ); + assertEquals( "com.foo.Outer$Inner", cls.getFullyQualifiedName() ); + assertEquals( "com.foo.Outer.Inner", cls.getCanonicalName() ); + cls = cls.getNestedClassByName( "Core" ); + assertEquals( "com.foo.Outer$Inner$Core", cls.getFullyQualifiedName() ); + assertEquals( "com.foo.Outer.Inner.Core", cls.getCanonicalName() ); + } } \ No newline at end of file
To unsubscribe from this list please visit:
