From 565928e8d95c346be4eb221cd6866de7d9a5afd6 Mon Sep 17 00:00:00 2001 From: rfscholte Date: Mon, 31 Dec 2018 13:22:24 +0100 Subject: [PATCH 1/3] Replace Vector with ArrayList Deprecate JNamedMap in favor of LinkedHashMap --- .../plugin/java/javasource/JClass.java | 132 +++++------------- .../plugin/java/javasource/JCompUnit.java | 49 +------ .../plugin/java/javasource/JConstructor.java | 43 ++++-- .../plugin/java/javasource/JDocComment.java | 27 ++-- .../plugin/java/javasource/JInterface.java | 71 +++------- .../plugin/java/javasource/JMethod.java | 12 +- .../java/javasource/JMethodSignature.java | 78 ++++++----- .../plugin/java/javasource/JNamedMap.java | 54 +++---- .../plugin/java/javasource/JSourceCode.java | 28 ++-- .../plugin/java/javasource/JStructure.java | 34 ++--- 10 files changed, 205 insertions(+), 323 deletions(-) diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JClass.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JClass.java index 76930fd30..f14068d2b 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JClass.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JClass.java @@ -76,8 +76,9 @@ import java.util.ArrayList; import java.util.Enumeration; +import java.util.LinkedHashMap; import java.util.List; -import java.util.Vector; +import java.util.Map; /** * A representation of the Java Source code for a Java Class. This is @@ -96,21 +97,21 @@ public class JClass extends JStructure /** * The list of constructors for this JClass */ - private Vector _constructors = null; + private List _constructors = null; /** * The list of member variables (fields) of this JClass */ - private JNamedMap _fields = null; + private Map _fields = null; - private Vector _innerClasses = null; + private List _innerClasses = null; /** * The list of methods of this JClass */ - private Vector _methods = null; + private List _methods = null; /** * The superclass for this JClass @@ -133,10 +134,10 @@ public JClass( String name ) throws IllegalArgumentException { super( name ); - _constructors = new Vector(); - _fields = new JNamedMap(); - _methods = new Vector(); - _innerClasses = new Vector(); + _constructors = new ArrayList(); + _fields = new LinkedHashMap<>(); + _methods = new ArrayList(); + _innerClasses = new ArrayList(); //-- initialize default Java doc getJDocComment().appendComment( "Class " + getLocalName() + "." ); @@ -163,7 +164,7 @@ public void addConstructor( JConstructor constructor ) /** check signatures (add later) **/ if ( !_constructors.contains( constructor ) ) { - _constructors.addElement( constructor ); + _constructors.add( constructor ); } } else @@ -276,13 +277,13 @@ public void addMethod( JMethod jMethod, boolean importReturnType ) for ( int i = 0; i < _methods.size(); i++ ) { - JMethod tmp = (JMethod) _methods.elementAt( i ); + JMethod tmp = (JMethod) _methods.get( i ); //-- first compare modifiers if ( tmp.getModifiers().isPrivate() ) { if ( !modifiers.isPrivate() ) { - _methods.insertElementAt( jMethod, i ); + _methods.add( i, jMethod ); added = true; break; } @@ -290,13 +291,13 @@ public void addMethod( JMethod jMethod, boolean importReturnType ) //-- compare names if ( jMethod.getName().compareTo( tmp.getName() ) < 0 ) { - _methods.insertElementAt( jMethod, i ); + _methods.add( i, jMethod ); added = true; break; } } //-- END SORT - if ( !added ) _methods.addElement( jMethod ); + if ( !added ) _methods.add( jMethod ); } //-- addMethod @@ -378,7 +379,7 @@ public JClass createInnerClass( String localname ) } JClass innerClass = new JInnerClass( classname ); - _innerClasses.addElement( innerClass ); + _innerClasses.add( innerClass ); return innerClass; } //-- createInnerClass @@ -391,7 +392,7 @@ public JClass createInnerClass( String localname ) */ public JConstructor getConstructor( int index ) { - return (JConstructor) _constructors.elementAt( index ); + return (JConstructor) _constructors.get( index ); } //-- getConstructor /** @@ -407,7 +408,7 @@ public JConstructor[] getConstructors() for ( int i = 0; i < _constructors.size(); i++ ) { - jcArray[i] = (JConstructor) _constructors.elementAt( i ); + jcArray[i] = _constructors.get( i ); } return jcArray; } //-- getConstructors @@ -431,13 +432,7 @@ public JField getField( String name ) **/ public JField[] getFields() { - int size = _fields.size(); - JField[] farray = new JField[size]; - for ( int i = 0; i < size; i++ ) - { - farray[i] = (JField) _fields.get( i ); - } - return farray; + return _fields.values().toArray( new JField[0] ); } //-- getFields /** @@ -448,10 +443,7 @@ public JField[] getFields() */ public JClass[] getInnerClasses() { - int size = _innerClasses.size(); - JClass[] carray = new JClass[size]; - _innerClasses.copyInto( carray ); - return carray; + return _innerClasses.toArray( new JClass[0] ); } //-- getInnerClasses; /** @@ -466,7 +458,7 @@ public JMethod[] getMethods() for ( int i = 0; i < _methods.size(); i++ ) { - marray[i] = (JMethod) _methods.elementAt( i ); + marray[i] = _methods.get( i ); } return marray; } //-- getMethods @@ -483,7 +475,7 @@ public JMethod getMethod( String name, int startIndex ) { for ( int i = startIndex; i < _methods.size(); i++ ) { - JMethod jMethod = (JMethod) _methods.elementAt( i ); + JMethod jMethod = _methods.get( i ); if ( jMethod.getName().equals( name ) ) return jMethod; } return null; @@ -497,7 +489,7 @@ public JMethod getMethod( String name, int startIndex ) */ public JMethod getMethod( int index ) { - return (JMethod) _methods.elementAt( index ); + return _methods.get( index ); } //-- getMethod @@ -556,13 +548,12 @@ public void print( JSourceWriter jsw, boolean classOnly ) printPackageDeclaration( jsw ); //-- get imports from inner-classes - Vector removeImports = null; + List removeImports = null; if ( _innerClasses.size() > 0 ) { - removeImports = new Vector(); - for ( int i = 0; i < _innerClasses.size(); i++ ) + removeImports = new ArrayList(); + for ( JClass iClass : _innerClasses ) { - JClass iClass = (JClass) _innerClasses.elementAt( i ); Enumeration e = iClass.getImports(); while ( e.hasMoreElements() ) { @@ -570,7 +561,7 @@ public void print( JSourceWriter jsw, boolean classOnly ) if ( !hasImport( classname ) ) { addImport( classname ); - removeImports.addElement( classname ); + removeImports.add( classname ); } } } @@ -582,7 +573,7 @@ public void print( JSourceWriter jsw, boolean classOnly ) { for ( int i = 0; i < removeImports.size(); i++ ) { - removeImport( (String) removeImports.elementAt( i ) ); + removeImport( removeImports.get( i ) ); } } @@ -673,11 +664,8 @@ else if ( modifiers.isPublic() ) jsw.writeln(); } - for ( int i = 0; i < _fields.size(); i++ ) + for ( JField jField : _fields.values() ) { - - JField jField = (JField) _fields.get( i ); - //-- print Java comment JDocComment comment = jField.getComment(); if ( comment != null ) comment.print( jsw ); @@ -736,7 +724,7 @@ else if ( modifiers.isPublic() ) } for ( int i = 0; i < _constructors.size(); i++ ) { - JConstructor jConstructor = (JConstructor) _constructors.elementAt( i ); + JConstructor jConstructor = _constructors.get( i ); jConstructor.print( jsw ); jsw.writeln(); } @@ -753,7 +741,7 @@ else if ( modifiers.isPublic() ) for ( int i = 0; i < _methods.size(); i++ ) { - JMethod jMethod = (JMethod) _methods.elementAt( i ); + JMethod jMethod = _methods.get( i ); jMethod.print( jsw ); jsw.writeln(); } @@ -769,7 +757,7 @@ else if ( modifiers.isPublic() ) } for ( int i = 0; i < _innerClasses.size(); i++ ) { - JClass jClass = (JClass) _innerClasses.elementAt( i ); + JClass jClass = _innerClasses.get( i ); jClass.print( jsw, true ); jsw.writeln(); } @@ -800,7 +788,7 @@ public void addSourceCode( String sourceCode ) */ public boolean removeConstructor( JConstructor constructor ) { - return _constructors.removeElement( constructor ); + return _constructors.remove( constructor ); } //-- removeConstructor /** @@ -850,7 +838,7 @@ public boolean removeField( JField jField ) */ public boolean removeInnerClass( JClass jClass ) { - return _innerClasses.removeElement( jClass ); + return _innerClasses.remove( jClass ); } //-- removeInnerClass /** @@ -862,58 +850,6 @@ public void setSuperClass( String superClass ) _superClass = superClass; } //-- setSuperClass - - /** - * Test drive method...to be removed or commented out - **/ - /*public static void main( String[] args ) - { - JClass testClass = new JClass( "org.acme.Test" ); - - testClass.addImport( "java.util.Vector" ); - testClass.addMember( new JField( JType.INT, "x" ) ); - JClass jcString = new JClass( "String" ); - - JField field = null; - field = new JField( JType.INT, "_z" ); - field.getModifiers().setStatic( true ); - testClass.addField( field ); - - testClass.getStaticInitializationCode().add( "_z = 75;" ); - - field = new JField( jcString, "myString" ); - field.getModifiers().makePrivate(); - testClass.addMember( field ); - - //-- create constructor - JConstructor cons = testClass.createConstructor(); - cons.getSourceCode().add( "this.x = 6;" ); - - JMethod jMethod = new JMethod( "getX", JType.INT, null ); - jMethod.setSourceCode( "return this.x;" ); - testClass.addMethod( jMethod ); - - //-- create inner-class - JClass innerClass = testClass.createInnerClass( "Foo" ); - innerClass.addImport( "java.util.Hashtable" ); - innerClass.addMember( new JField( JType.INT, "_type" ) ); - - field = new JField( jcString, "_name" ); - field.getModifiers().makePrivate(); - innerClass.addMember( field ); - - //-- create constructor - cons = innerClass.createConstructor(); - cons.getSourceCode().add( "_name = \"foo\";" ); - - jMethod = new JMethod( "getName", jcString, null ); - jMethod.setSourceCode( "return _name;" ); - innerClass.addMethod( jMethod ); - - testClass.print(); - } //-- main - */ - final class JInnerClass extends JClass { diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JCompUnit.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JCompUnit.java index 9ba2cecd7..149ff859e 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JCompUnit.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JCompUnit.java @@ -76,10 +76,11 @@ */ import java.io.File; +import java.util.ArrayList; import java.util.Enumeration; +import java.util.List; import java.util.SortedSet; import java.util.TreeSet; -import java.util.Vector; import org.codehaus.plexus.util.WriterFactory; @@ -119,13 +120,13 @@ public class JCompUnit * The set of top-level classes that live in this compilation unit. **/ //private TypeList classes = null; - private Vector classes = null; + private List classes = null; /** * The set of top-level interfaces that live in this compilation unit. **/ //private TypeList interfaces = null; - private Vector interfaces = null; + private List interfaces = null; /** * Creates a new JCompUnit @@ -193,8 +194,8 @@ public JCompUnit( JInterface jInterface ) private void init() { - classes = new Vector(); - interfaces = new Vector(); + classes = new ArrayList(); + interfaces = new ArrayList(); } /** @@ -538,44 +539,6 @@ private void resolveNames() */ } //-- resolveNames - /** - * Test drive method...to be removed or commented out - ** - public static void main(String[] args) { - JCompUnit unit = new JCompUnit("com.acme", "Test.java"); - - JClass testClass = new JClass("Test"); - - testClass.addImport("java.util.Vector"); - testClass.addMember(new JField(JType.Int, "x")); - - JField field = null; - field = new JField(JType.Int, "_z"); - field.getModifiers().setStatic(true); - testClass.addField(field); - - testClass.getStaticInitializationCode().add("_z = 75;"); - - JClass jcString = new JClass("String"); - field = new JField(jcString, "myString"); - field.getModifiers().makePrivate(); - testClass.addMember(field); - - //-- create constructor - JConstructor cons = testClass.createConstructor(); - testClass.addConstructor(cons); - cons.getSourceCode().add("this.x = 6;"); - - JMethod jMethod = new JMethod(JType.Int, "getX"); - jMethod.setSourceCode("return this.x;"); - testClass.addMethod(jMethod); - - unit.addClass (testClass); - - unit.print(); - - } //-- main - /* */ } //-- JCompUnit diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JConstructor.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JConstructor.java index 479140601..98d5132ab 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JConstructor.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JConstructor.java @@ -45,6 +45,11 @@ package org.codehaus.modello.plugin.java.javasource; +import java.util.Collections; +import java.util.Enumeration; +import java.util.LinkedHashMap; +import java.util.Map; + /* * Copyright (c) 2004, Codehaus.org * @@ -83,7 +88,7 @@ public class JConstructor /** * List of parameters for this Constructor **/ - private JNamedMap params = null; + private Map params = null; /** * The Class in this JMember has been declared @@ -104,7 +109,7 @@ public JConstructor( JClass declaringClass ) { this.declaringClass = declaringClass; this.modifiers = new JModifiers(); - this.params = new JNamedMap(); + this.params = new LinkedHashMap<>(); this.sourceCode = new JSourceCode(); } @@ -160,13 +165,7 @@ public JModifiers getModifiers() **/ public JParameter[] getParameters() { - JParameter[] jpArray = new JParameter[params.size()]; - for ( int i = 0; i < jpArray.length; i++ ) - { - jpArray[i] = (JParameter) params.get( i ); - } - return jpArray; - + return params.values().toArray( new JParameter[0] ); } //-- getParameters public JSourceCode getSourceCode() @@ -190,10 +189,20 @@ else if ( modifiers.isProtected() ) jsw.write( '(' ); //-- print parameters + if ( !params.isEmpty() ) + { + Enumeration paramEnum = Collections.enumeration( params.values() ); + jsw.write( paramEnum.nextElement() ); + while( paramEnum.hasMoreElements()) + { + jsw.write( ", " ); + jsw.write( paramEnum.nextElement() ); + } + } + + for ( int i = 0; i < params.size(); i++ ) { - if ( i > 0 ) jsw.write( ", " ); - jsw.write( params.get( i ) ); } jsw.writeln( ')' ); jsw.writeln( '{' ); @@ -228,11 +237,15 @@ public String toString() sb.append( '(' ); //-- print parameters - for ( int i = 0; i < params.size(); i++ ) + if ( !params.isEmpty() ) { - JParameter jp = (JParameter) params.get( i ); - if ( i > 0 ) sb.append( ", " ); - sb.append( jp.getType().getName() ); + Enumeration paramEnum = Collections.enumeration( params.values() ); + sb.append( paramEnum.nextElement().getType().getName() ); + while( paramEnum.hasMoreElements()) + { + sb.append( ", " ); + sb.append( paramEnum.nextElement().getType().getName() ); + } } sb.append( ')' ); return sb.toString(); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocComment.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocComment.java index db7507b2e..bc841e009 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocComment.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocComment.java @@ -46,6 +46,9 @@ package org.codehaus.modello.plugin.java.javasource; +import java.util.ArrayList; +import java.util.Collections; + /* * Copyright (c) 2004, Codehaus.org * @@ -69,7 +72,7 @@ */ import java.util.Enumeration; -import java.util.Vector; +import java.util.List; /** * A class that "SOMEWHAT" represents a Java Doc Comment. @@ -84,7 +87,7 @@ public class JDocComment /** * An ordered list of descriptors */ - private Vector _descriptors = null; + private List _descriptors = null; /** * The internal buffer for this JDocComment @@ -97,7 +100,7 @@ public class JDocComment public JDocComment() { super(); - _descriptors = new Vector(); + _descriptors = new ArrayList(); _comment = new StringBuffer(); } //-- JDocComment @@ -113,24 +116,24 @@ public void addDescriptor( JDocDescriptor jdesc ) //-- on the fly sorting of descriptors if ( _descriptors.size() == 0 ) { - _descriptors.addElement( jdesc ); + _descriptors.add( jdesc ); return; } for ( int i = 0; i < _descriptors.size(); i++ ) { JDocDescriptor jdd - = (JDocDescriptor) _descriptors.elementAt( i ); + = _descriptors.get( i ); short compare = jdesc.compareTo( jdd ); switch ( compare ) { case 0: // equal - _descriptors.insertElementAt( jdesc, i + 1 ); + _descriptors.add( i + 1, jdesc ); return; case -1: //-- less than - _descriptors.insertElementAt( jdesc, i ); + _descriptors.add( i, jdesc ); return; case 1: //-- keep looking @@ -139,7 +142,7 @@ public void addDescriptor( JDocDescriptor jdesc ) } //-- if we make it here we need to add - _descriptors.addElement( jdesc ); + _descriptors.add( jdesc ); } //-- addException @@ -170,7 +173,7 @@ public String getComment() */ public Enumeration getDescriptors() { - return _descriptors.elements(); + return Collections.enumeration( _descriptors ); } //-- getDescriptors /** @@ -195,10 +198,8 @@ public JDocDescriptor getParamDescriptor( String name ) { if ( name == null ) return null; - for ( int i = 0; i < _descriptors.size(); i++ ) + for ( JDocDescriptor jdd : _descriptors ) { - JDocDescriptor jdd - = (JDocDescriptor) _descriptors.elementAt( i ); if ( jdd.getType() == JDocDescriptor.PARAM ) { if ( name.equals( jdd.getName() ) ) @@ -232,7 +233,7 @@ public void print( JSourceWriter jsw ) for ( int i = 0; i < _descriptors.size(); i++ ) { jComment.appendComment( "\n" ); - jComment.appendComment( _descriptors.elementAt( i ).toString() ); + jComment.appendComment( _descriptors.get( i ).toString() ); } jComment.print( jsw ); } //-- print diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JInterface.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JInterface.java index 4be3b4abb..28b70693d 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JInterface.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JInterface.java @@ -69,8 +69,9 @@ import java.util.ArrayList; import java.util.Enumeration; +import java.util.LinkedHashMap; import java.util.List; -import java.util.Vector; +import java.util.Map; /** * A representation of the Java Source code for a Java Interface. @@ -89,12 +90,12 @@ public final class JInterface extends JStructure /** * The fields for this JInterface */ - private JNamedMap fields = null; + private Map fields = null; /** * The list of methods of this JInterface */ - private Vector methods = null; + private List methods = null; /** @@ -108,7 +109,7 @@ public JInterface( String name ) throws IllegalArgumentException { super( name ); - methods = new Vector(); + methods = new ArrayList(); //-- initialize default Java doc getJDocComment().appendComment( "Interface " + getLocalName() + "." ); @@ -160,7 +161,7 @@ public void addField( JField jField ) //-- don't contain any fields, no need to waste space if ( fields == null ) { - fields = new JNamedMap( 3 ); + fields = new LinkedHashMap<>( 3 ); } fields.put( name, jField ); @@ -225,13 +226,13 @@ public void addMethod( JMethodSignature jMethodSig ) JModifiers modifiers = jMethodSig.getModifiers(); for ( int i = 0; i < methods.size(); i++ ) { - JMethodSignature tmp = (JMethodSignature) methods.elementAt( i ); + JMethodSignature tmp = methods.get( i ); //-- first compare modifiers if ( tmp.getModifiers().isProtected() ) { if ( !modifiers.isProtected() ) { - methods.insertElementAt( jMethodSig, i ); + methods.add( i, jMethodSig ); added = true; break; } @@ -239,13 +240,13 @@ public void addMethod( JMethodSignature jMethodSig ) //-- compare names if ( jMethodSig.getName().compareTo( tmp.getName() ) < 0 ) { - methods.insertElementAt( jMethodSig, i ); + methods.add( i, jMethodSig ); added = true; break; } } //-- END SORT - if ( !added ) methods.addElement( jMethodSig ); + if ( !added ) methods.add( jMethodSig ); //-- check return type to make sure it's included in the //-- import list @@ -291,13 +292,7 @@ public JField[] getFields() { return new JField[0]; } - int size = fields.size(); - JField[] farray = new JField[size]; - for ( int i = 0; i < size; i++ ) - { - farray[i] = (JField) fields.get( i ); - } - return farray; + return fields.values().toArray( new JField[0] ); } //-- getFields @@ -308,9 +303,7 @@ public JField[] getFields() **/ public JMethodSignature[] getMethods() { - JMethodSignature[] marray = new JMethodSignature[methods.size()]; - methods.copyInto( marray ); - return marray; + return methods.toArray( new JMethodSignature[0] ); } //-- getMethods /** @@ -324,9 +317,8 @@ public JMethodSignature[] getMethods() **/ public JMethodSignature getMethod( String name, int startIndex ) { - for ( int i = startIndex; i < methods.size(); i++ ) + for ( JMethodSignature jMethod : methods ) { - JMethodSignature jMethod = (JMethodSignature) methods.elementAt( i ); if ( jMethod.getName().equals( name ) ) return jMethod; } return null; @@ -340,7 +332,7 @@ public JMethodSignature getMethod( String name, int startIndex ) **/ public JMethodSignature getMethod( int index ) { - return (JMethodSignature) methods.elementAt( index ); + return methods.get( index ); } //-- getMethod @@ -446,11 +438,8 @@ else if ( modifiers.isPublic() ) } - for ( int i = 0; i < fields.size(); i++ ) + for ( JField jField : fields.values() ) { - - JField jField = (JField) fields.get( i ); - //-- print Java comment JDocComment comment = jField.getComment(); if ( comment != null ) comment.print( jsw ); @@ -493,9 +482,8 @@ else if ( modifiers.isPublic() ) jsw.writeln(); } - for ( int i = 0; i < methods.size(); i++ ) + for ( JMethodSignature signature : methods ) { - JMethodSignature signature = (JMethodSignature) methods.elementAt( i ); signature.print( jsw ); jsw.writeln( ';' ); } @@ -518,31 +506,4 @@ public void addSourceCode( String sourceCode ) sourceCodeEntries.add( sourceCode ); } - /** - * Test drive method...to be removed or commented out - ** - public static void main(String[] args) { - JInterface jInterface = new JInterface("Test"); - - //-- add an import - jInterface.addImport("java.util.Vector"); - JClass jString = new JClass("String"); - - //-- add an interface - jInterface.addInterface("java.io.Serializable"); - - //-- add a static field - JField jField = new JField(new JClass("java.lang.String"), "TEST"); - jField.setInitString("\"Test\""); - jField.getModifiers().setStatic(true); - jField.getModifiers().makePublic(); - jInterface.addField(jField); - - //-- add a method signature - JMethodSignature jMethodSig = new JMethodSignature("getName", jString); - jInterface.addMethod(jMethodSig); - jInterface.print(); - } //-- main - /* */ - } //-- JInterface diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethod.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethod.java index 00284a1dc..e1ac27142 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethod.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethod.java @@ -45,7 +45,6 @@ package org.codehaus.modello.plugin.java.javasource; - /* * Copyright (c) 2004, Codehaus.org * @@ -68,7 +67,8 @@ * SOFTWARE. */ -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; /** * A class which holds information about the methods of @@ -86,7 +86,7 @@ public class JMethod implements JMember /** * The set of classes that contain this JMethod. **/ - private Vector _classes = null; + private List _classes = null; /** * The JavaDoc comment for this JMethod. This @@ -151,7 +151,7 @@ public JMethod( final String name, final JType returnType, final String returnDo throw new IllegalArgumentException( err ); } - _classes = new Vector( 1 ); + _classes = new ArrayList( 1 ); this.source = new JSourceCode(); _signature = new JMethodSignature( name, returnType ); this.jdc = _signature.getJDocComment(); @@ -415,7 +415,7 @@ public String toString() **/ protected void addDeclaringClass( JClass jClass ) { - _classes.addElement( jClass ); + _classes.add( jClass ); } //-- addDeclaringClass /** @@ -427,7 +427,7 @@ protected void addDeclaringClass( JClass jClass ) **/ protected void removeDeclaringClass( JClass jClass ) { - _classes.removeElement( jClass ); + _classes.remove( jClass ); } //-- removeDeclaringClass protected String[] getParameterClassNames() diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethodSignature.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethodSignature.java index bf936410a..215b60d81 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethodSignature.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethodSignature.java @@ -45,7 +45,6 @@ package org.codehaus.modello.plugin.java.javasource; - /* * Copyright (c) 2004, Codehaus.org * @@ -68,7 +67,13 @@ * SOFTWARE. */ -import java.util.Vector; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; /** * A class which holds information about the signature @@ -102,7 +107,7 @@ public final class JMethodSignature * The list of parameters of this JMethodSignature in declared * order **/ - private JNamedMap params = null; + private Map params = null; /** * The JavaDoc comment for this method signature. @@ -112,7 +117,7 @@ public final class JMethodSignature /** * The exceptions that this method throws **/ - private Vector exceptions = null; + private List exceptions = null; /** * Creates a new method with the given name and return type. @@ -134,8 +139,8 @@ public JMethodSignature( String name, JType returnType ) this.returnType = returnType; this.name = name; this.modifiers = new JModifiers(); - this.params = new JNamedMap( 3 ); - this.exceptions = new Vector( 1 ); + this.params = new LinkedHashMap<>( 3 ); + this.exceptions = new ArrayList( 1 ); } //-- JMethodSignature /** @@ -150,13 +155,12 @@ public void addException( JClass exp ) //-- make sure exception is not already added String expClassName = exp.getName(); - for ( int i = 0; i < exceptions.size(); i++ ) + for ( JClass jClass : exceptions ) { - JClass jClass = (JClass) exceptions.elementAt( i ); if ( expClassName.equals( jClass.getName() ) ) return; } //-- add exception - exceptions.addElement( exp ); + exceptions.add( exp ); //-- create comment jdc.addDescriptor( JDocDescriptor.createExceptionDesc( expClassName, null ) ); @@ -207,10 +211,7 @@ public void addParameter( JParameter parameter ) **/ public JClass[] getExceptions() { - - JClass[] jclasses = new JClass[exceptions.size()]; - exceptions.copyInto( jclasses ); - return jclasses; + return exceptions.toArray( new JClass[0] ); } //-- getExceptions /** @@ -251,7 +252,13 @@ public String getName() **/ public JParameter getParameter( int index ) { - return (JParameter) params.get( index ); + Iterator> paramIter = params.entrySet().iterator(); + Map.Entry selected = null; + for( int i = 0; i <= index; i++) + { + selected = paramIter.next(); + } + return selected.getValue(); } //-- getParameter /** @@ -263,12 +270,7 @@ public JParameter getParameter( int index ) **/ public synchronized JParameter[] getParameters() { - JParameter[] pArray = new JParameter[params.size()]; - for ( int i = 0; i < pArray.length; i++ ) - { - pArray[i] = (JParameter) params.get( i ); - } - return pArray; + return params.values().toArray( new JParameter[0] ); } //-- getParameters /** @@ -358,11 +360,15 @@ public void print( JSourceWriter jsw, boolean printJavaDoc ) if ( params.size() > 0 ) { jsw.write( ' ' ); - for ( int i = 0; i < params.size(); i++ ) + + Enumeration paramEnum = Collections.enumeration( params.values() ); + jsw.write( paramEnum.nextElement() ); + while( paramEnum.hasMoreElements() ) { - if ( i > 0 ) jsw.write( ", " ); - jsw.write( params.get( i ) ); + jsw.write( ", " ); + jsw.write( paramEnum.nextElement() ); } + jsw.write( ' ' ); } @@ -375,7 +381,7 @@ public void print( JSourceWriter jsw, boolean printJavaDoc ) for ( int i = 0; i < exceptions.size(); i++ ) { if ( i > 0 ) jsw.write( ", " ); - JClass jClass = (JClass) exceptions.elementAt( i ); + JClass jClass = exceptions.get( i ); jsw.write( jClass.getName() ); } } @@ -406,12 +412,15 @@ public String toString() if ( params.size() > 0 ) { sb.append( ' ' ); - for ( int i = 0; i < params.size(); i++ ) + + Enumeration paramEnum = Collections.enumeration( params.values() ); + sb.append( paramEnum.nextElement().getType().getName() ); + while( paramEnum.hasMoreElements() ) { - JParameter jParam = (JParameter) params.get( i ); - if ( i > 0 ) sb.append( ", " ); - sb.append( jParam.getType().getName() ); + sb.append( ", " ); + sb.append( paramEnum.nextElement().getType().getName() ); } + sb.append( ' ' ); } @@ -422,23 +431,20 @@ public String toString() protected String[] getParameterClassNames() { - Vector names = new Vector( params.size() ); + List names = new ArrayList( params.size() ); - for ( int i = 0; i < params.size(); i++ ) + for ( JParameter param : params.values() ) { - - JType jType = ( (JParameter) params.get( i ) ).getType(); + JType jType = param.getType(); while ( jType.isArray() ) jType = jType.getComponentType(); if ( !jType.isPrimitive() ) { JClass jclass = (JClass) jType; - names.addElement( jclass.getName() ); + names.add( jclass.getName() ); } } - String[] names_array = new String[names.size()]; - names.copyInto( names_array ); - return names_array; + return names.toArray( new String[0] ); } //-- getParameterClassNames } //-- JMethodSignature \ No newline at end of file diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JNamedMap.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JNamedMap.java index e74a1abb8..15c575ac7 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JNamedMap.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JNamedMap.java @@ -66,7 +66,9 @@ * SOFTWARE. */ -import java.util.Vector; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; /** *

@@ -76,12 +78,14 @@ * This class is not synchronized. So be careful. :-) * * @author Keith Visco + * @deprecated use {@link LinkedHashMap} instead **/ +@Deprecated public class JNamedMap { - private Vector names = null; - private Vector objects = null; + private List names = null; + private List objects = null; /** * Creates a new JNamedMap @@ -89,8 +93,8 @@ public class JNamedMap public JNamedMap() { - names = new Vector(); - objects = new Vector(); + names = new ArrayList(); + objects = new ArrayList(); } //-- JNamedMap @@ -102,8 +106,8 @@ public JNamedMap() public JNamedMap( int size ) { - names = new Vector( size ); - objects = new Vector( size ); + names = new ArrayList( size ); + objects = new ArrayList( size ); } //-- JNamedMap @@ -117,7 +121,7 @@ public JNamedMap( int size ) public Object get( String name ) { int i = indexOf( name ); - if ( i >= 0 ) return objects.elementAt( i ); + if ( i >= 0 ) return objects.get( i ); return null; } //-- get @@ -130,7 +134,7 @@ public Object get( String name ) public Object get( int index ) throws IndexOutOfBoundsException { - return objects.elementAt( index ); + return objects.get( index ); } //-- get /** @@ -142,7 +146,7 @@ public Object get( int index ) public String getNameByObject( Object obj ) { int i = objects.indexOf( obj ); - if ( i >= 0 ) return (String) names.elementAt( i ); + if ( i >= 0 ) return (String) names.get( i ); return null; } //-- getNameByObject @@ -151,10 +155,9 @@ public String getNameByObject( Object obj ) * * @return a Vector of names **/ - @SuppressWarnings( "unchecked" ) - public Vector getNames() + public java.util.Vector getNames() { - return (Vector) names.clone(); + return new java.util.Vector( names ); } //-- getNames /** @@ -162,10 +165,9 @@ public Vector getNames() * * @return a Vector of Objects **/ - @SuppressWarnings( "unchecked" ) - public Vector getObjects() + public java.util.Vector getObjects() { - return (Vector) objects.clone(); + return new java.util.Vector( objects ); } //-- getObjects /** @@ -181,7 +183,7 @@ public int indexOf( String name ) for ( int i = 0; i < names.size(); i++ ) { - String iName = (String) names.elementAt( i ); + String iName = (String) names.get( i ); if ( iName.equals( name ) ) return i; } return -1; @@ -200,13 +202,13 @@ public void put( String name, Object obj ) int idx = indexOf( name ); if ( idx >= 0 ) - objects.setElementAt( obj, idx ); + objects.add( idx, obj ); else { //-- we may need some synchronization here //-- if we are in a multithreaded environment - names.addElement( name ); - objects.addElement( obj ); + names.add( name ); + objects.add( obj ); } } //-- put @@ -219,9 +221,9 @@ public void put( String name, Object obj ) public Object remove( int index ) throws IndexOutOfBoundsException { - Object obj = objects.elementAt( index ); - objects.removeElementAt( index ); - names.removeElementAt( index ); + Object obj = objects.get( index ); + objects.remove( index ); + names.remove( index ); return obj; } //-- remove @@ -239,9 +241,9 @@ public Object remove( String name ) int idx = indexOf( name ); if ( idx >= 0 ) { - obj = objects.elementAt( idx ); - objects.removeElementAt( idx ); - names.removeElementAt( idx ); + obj = objects.get( idx ); + objects.remove( idx ); + names.remove( idx ); } return obj; } //-- remove diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JSourceCode.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JSourceCode.java index 524b224c6..d785eca8e 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JSourceCode.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JSourceCode.java @@ -45,6 +45,7 @@ package org.codehaus.modello.plugin.java.javasource; +import java.util.ArrayList; /* * Copyright (c) 2004, Codehaus.org @@ -68,10 +69,9 @@ * SOFTWARE. */ +import java.util.List; import org.codehaus.modello.ModelloRuntimeException; -import java.util.Vector; - /** * A class for holding in-memory Java source code. * @@ -85,7 +85,7 @@ public class JSourceCode /** * A list of JCodeStatements **/ - private Vector source = null; + private List source = null; /** * The indent size @@ -103,7 +103,7 @@ public class JSourceCode public JSourceCode() { super(); - source = new Vector(); + source = new ArrayList(); } //-- JSourceCode /** @@ -114,7 +114,7 @@ public JSourceCode() public JSourceCode( String sourceCode ) { this(); - this.source.addElement( new JCodeStatement( sourceCode, currentIndent ) ); + this.source.add( new JCodeStatement( sourceCode, currentIndent ) ); } //-- JSourceCode /** @@ -125,7 +125,7 @@ public JSourceCode( String sourceCode ) public void add( String statement ) { JCodeStatement jcs = new JCodeStatement( statement, currentIndent ); - source.addElement( jcs ); + source.add( jcs ); } //-- add /** @@ -140,7 +140,7 @@ public void add( String statement ) public void add( String statement, short indentSize ) { JCodeStatement jcs = new JCodeStatement( statement, indentSize ); - source.addElement( jcs ); + source.add( jcs ); } //-- add /** @@ -158,7 +158,7 @@ public void addIndented( String statement ) { indent(); JCodeStatement jcs = new JCodeStatement( statement, currentIndent ); - source.addElement( jcs ); + source.add( jcs ); unindent(); } //-- add @@ -174,7 +174,7 @@ public void append( String segment ) add( segment ); else { - JCodeStatement jcs = (JCodeStatement) source.lastElement(); + JCodeStatement jcs = (JCodeStatement) source.get( source.size() - 1 ); jcs.append( segment ); } } //-- append(String) @@ -184,7 +184,7 @@ public void append( String segment ) **/ public void clear() { - source.removeAllElements(); + source.clear(); } //-- clear(); /** @@ -195,7 +195,7 @@ public void copyInto( JSourceCode jsc ) { for ( int i = 0; i < source.size(); i++ ) { - jsc.addCodeStatement( (JCodeStatement) source.elementAt( i ) ); + jsc.addCodeStatement( source.get( i ) ); } } //-- copyInto @@ -223,7 +223,7 @@ public boolean isEmpty() public void print( JSourceWriter jsw ) { for ( int i = 0; i < source.size(); i++ ) - jsw.writeln( source.elementAt( i ).toString() ); + jsw.writeln( source.get( i ).toString() ); } //-- print /** @@ -249,7 +249,7 @@ public String toString() String lineSeparator = System.getProperty( "line.separator" ); for ( int i = 0; i < source.size(); i++ ) { - sb.append( source.elementAt( i ).toString() ); + sb.append( source.get( i ).toString() ); sb.append( lineSeparator ); } return sb.toString(); @@ -262,7 +262,7 @@ public String toString() private void addCodeStatement( JCodeStatement jcs ) { short indent = (short) ( jcs.getIndent() + currentIndent - JCodeStatement.DEFAULT_INDENTSIZE ); - source.addElement( new JCodeStatement( jcs.getStatement(), indent ) ); + source.add( new JCodeStatement( jcs.getStatement(), indent ) ); } //-- addCodeStatement(JCodeStatement) } //-- JSourceCode diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JStructure.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JStructure.java index a3371d3f3..971e4ffa3 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JStructure.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JStructure.java @@ -68,8 +68,10 @@ */ import java.io.File; +import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; -import java.util.Vector; +import java.util.List; import org.codehaus.plexus.util.WriterFactory; @@ -111,12 +113,12 @@ public abstract class JStructure extends JType /** * List of imported classes and packages */ - private Vector imports = null; + private List imports = null; /** * The set of interfaces implemented/extended by this JStructure */ - private Vector interfaces = null; + private List interfaces = null; /** * The Javadoc for this JStructure @@ -162,8 +164,8 @@ protected JStructure( String name ) throw new IllegalArgumentException( err ); } this.packageName = getPackageFromClassName( name ); - imports = new Vector(); - interfaces = new Vector(); + imports = new ArrayList(); + interfaces = new ArrayList(); jdc = new JDocComment(); modifiers = new JModifiers(); //-- initialize default Java doc @@ -231,15 +233,15 @@ public void addImport( String className ) //-- we do not include more than one of the same import for ( int i = 0; i < imports.size(); i++ ) { - String imp = (String) imports.elementAt( i ); + String imp = imports.get( i ); if ( imp.equals( className ) ) return; if ( imp.compareTo( className ) > 0 ) { - imports.insertElementAt( className, i ); + imports.add( i, className ); return; } } - imports.addElement( className ); + imports.add( className ); } } //-- addImport @@ -254,7 +256,7 @@ public void addImport( String className ) public void addInterface( String interfaceName ) { if ( !interfaces.contains( interfaceName ) ) - interfaces.addElement( interfaceName ); + interfaces.add( interfaceName ); } //-- addInterface /** @@ -270,7 +272,7 @@ public void addInterface( JInterface jInterface ) String interfaceName = jInterface.getName(); if ( !interfaces.contains( interfaceName ) ) { - interfaces.addElement( interfaceName ); + interfaces.add( interfaceName ); } } //-- addInterface @@ -418,7 +420,7 @@ public JComment getHeader() */ public Enumeration getImports() { - return imports.elements(); + return Collections.enumeration( imports ); } //-- getImports /** @@ -430,7 +432,7 @@ public Enumeration getImports() */ public Enumeration getInterfaces() { - return interfaces.elements(); + return Collections.enumeration( interfaces ); } //-- getInterfaces /** @@ -548,8 +550,7 @@ public boolean removeImport( String className ) if ( className == null ) return result; if ( className.length() == 0 ) return result; - result = imports.removeElement( className ); - return result; + return imports.remove( className ); } //-- removeImport @@ -677,11 +678,10 @@ public void printImportDeclarations( JSourceWriter jsw ) jsw.writeln( " //- Imported classes and packages -/" ); jsw.writeln( "//---------------------------------/" ); jsw.writeln(); - Enumeration e = imports.elements(); - while ( e.hasMoreElements() ) + for( String imp : imports ) { jsw.write( "import " ); - jsw.write( e.nextElement() ); + jsw.write( imp ); jsw.writeln( ';' ); } jsw.writeln(); From 30ceb00822da090b043960bfe73c41867da8f3b5 Mon Sep 17 00:00:00 2001 From: rfscholte Date: Mon, 31 Dec 2018 13:29:31 +0100 Subject: [PATCH 2/3] Replace StringBuffer with StringBuilder --- .../codehaus/modello/plugin/AbstractModelloGenerator.java | 4 ++-- .../src/it/maven-model/src/main/mdo/maven.mdo | 4 ++-- .../src/test/resources/models/maven.mdo | 2 +- .../modello-plugin-dom4j/src/test/resources/maven.mdo | 2 +- .../modello-plugin-jackson/src/test/resources/maven.mdo | 2 +- .../modello/plugin/java/JavaModelloGenerator.java | 6 +++--- .../codehaus/modello/plugin/java/javasource/JComment.java | 8 ++++---- .../modello/plugin/java/javasource/JConstructor.java | 4 ++-- .../modello/plugin/java/javasource/JDocComment.java | 6 +++--- .../modello/plugin/java/javasource/JDocDescriptor.java | 2 +- .../codehaus/modello/plugin/java/javasource/JField.java | 2 +- .../modello/plugin/java/javasource/JMethodSignature.java | 4 ++-- .../modello/plugin/java/javasource/JModifiers.java | 2 +- .../modello/plugin/java/javasource/JParameter.java | 2 +- .../modello/plugin/java/javasource/JSourceCode.java | 8 ++++---- .../modello/plugin/java/javasource/JStructure.java | 2 +- .../src/test/resources/models/maven.mdo | 2 +- .../modello-plugin-sax/src/test/resources/maven.mdo | 2 +- .../codehaus/modello/plugin/stax/StaxReaderGenerator.java | 4 ++-- .../modello-plugin-stax/src/test/resources/maven.mdo | 2 +- .../org/codehaus/modello/plugin/xdoc/XdocGenerator.java | 6 +++--- .../modello-plugin-xdoc/src/test/resources/maven.mdo | 2 +- .../codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java | 4 ++-- .../modello-plugin-xpp3/src/test/resources/maven.mdo | 2 +- .../modello-plugin-xsd/src/test/resources/maven.mdo | 2 +- 25 files changed, 43 insertions(+), 43 deletions(-) diff --git a/modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java b/modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java index 278f4ca12..e7df7e0d0 100644 --- a/modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java +++ b/modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java @@ -180,7 +180,7 @@ protected String capitalise( String str ) return str; } - return new StringBuffer( str.length() ) + return new StringBuilder( str.length() ) .append( Character.toTitleCase( str.charAt( 0 ) ) ) .append( str.substring( 1 ) ) .toString(); @@ -220,7 +220,7 @@ public static String uncapitalise( String str ) return str; } - return new StringBuffer( str.length() ) + return new StringBuilder( str.length() ) .append( Character.toLowerCase( str.charAt( 0 ) ) ) .append( str.substring( 1 ) ) .toString(); diff --git a/modello-maven-plugin/src/it/maven-model/src/main/mdo/maven.mdo b/modello-maven-plugin/src/it/maven-model/src/main/mdo/maven.mdo index 25bf73532..e07e0e2aa 100644 --- a/modello-maven-plugin/src/it/maven-model/src/main/mdo/maven.mdo +++ b/modello-maven-plugin/src/it/maven-model/src/main/mdo/maven.mdo @@ -494,7 +494,7 @@ public String getId() { - StringBuffer id = new StringBuffer(); + StringBuilder id = new StringBuilder(); id.append( getGroupId() ); id.append( ":" ); diff --git a/modello-plugins/modello-plugin-dom4j/src/test/resources/maven.mdo b/modello-plugins/modello-plugin-dom4j/src/test/resources/maven.mdo index 71d35f4cf..7f7c1218b 100644 --- a/modello-plugins/modello-plugin-dom4j/src/test/resources/maven.mdo +++ b/modello-plugins/modello-plugin-dom4j/src/test/resources/maven.mdo @@ -491,7 +491,7 @@ public String getId() { - StringBuffer id = new StringBuffer(); + StringBuilder id = new StringBuilder(); id.append( getGroupId() ); id.append( ":" ); diff --git a/modello-plugins/modello-plugin-jackson/src/test/resources/maven.mdo b/modello-plugins/modello-plugin-jackson/src/test/resources/maven.mdo index 97a7e84a5..065c32ca8 100644 --- a/modello-plugins/modello-plugin-jackson/src/test/resources/maven.mdo +++ b/modello-plugins/modello-plugin-jackson/src/test/resources/maven.mdo @@ -515,7 +515,7 @@ public String getId() { - StringBuffer id = new StringBuffer(); + StringBuilder id = new StringBuilder(); id.append( getGroupId() ); id.append( ":" ); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java index 951433bd1..7cadd74a2 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java @@ -428,7 +428,7 @@ private JMethod generateToString( ModelClass modelClass, boolean onlyIdentifierF } else { - sc.add( "StringBuffer buf = new StringBuffer( 128 );" ); + sc.add( "StringBuilder buf = new StringBuilder( 128 );" ); } sc.add( "" ); @@ -1246,7 +1246,7 @@ private JMethod createGetter( JField field, ModelField modelField ) JMethod getter = new JMethod( prefix + propertyName, returnType, null ); - StringBuffer comment = new StringBuffer( "Get " ); + StringBuilder comment = new StringBuilder( "Get " ); if ( StringUtils.isEmpty( modelField.getDescription() ) ) { comment.append( "the " ); @@ -1286,7 +1286,7 @@ private JMethod createSetter( JField field, ModelField modelField, boolean isBui setter = new JMethod( "set" + propertyName ); } - StringBuffer comment = new StringBuffer( "Set " ); + StringBuilder comment = new StringBuilder( "Set " ); if ( StringUtils.isEmpty( modelField.getDescription() ) ) { comment.append( "the " ); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JComment.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JComment.java index 3f2e791e7..2e7072d7f 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JComment.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JComment.java @@ -124,7 +124,7 @@ public class JComment /** * The main comment for this JDocComment **/ - private StringBuffer _comment = null; + private StringBuilder _comment = null; /** * The maximum number of characters per line @@ -138,7 +138,7 @@ public class JComment public JComment() { super(); - _comment = new StringBuffer(); + _comment = new StringBuilder(); } //-- JComment /** @@ -279,7 +279,7 @@ class LineFormatter String prefix = null; - private StringBuffer sb = null; + private StringBuilder sb = null; /** * Creates a LineFormatter for the given comment @@ -289,7 +289,7 @@ class LineFormatter { this.comment = comment; if ( comment != null ) this.length = comment.length(); - sb = new StringBuffer(); + sb = new StringBuilder(); } //-- LineFormatter diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JConstructor.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JConstructor.java index 98d5132ab..b58033eea 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JConstructor.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JConstructor.java @@ -127,7 +127,7 @@ public void addParameter( JParameter parameter ) //-- check current params if ( params.get( parameter.getName() ) != null ) { - StringBuffer err = new StringBuffer(); + StringBuilder err = new StringBuilder(); err.append( "A parameter already exists for the constructor, " ); err.append( this.declaringClass.getName() ); err.append( ", with the name: " ); @@ -232,7 +232,7 @@ public void setSourceCode( JSourceCode sourceCode ) public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append( declaringClass.getName() ); sb.append( '(' ); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocComment.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocComment.java index bc841e009..52d9dd072 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocComment.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocComment.java @@ -92,7 +92,7 @@ public class JDocComment /** * The internal buffer for this JDocComment */ - private StringBuffer _comment = null; + private StringBuilder _comment = null; /** * Creates a new JavaDoc Comment @@ -101,7 +101,7 @@ public JDocComment() { super(); _descriptors = new ArrayList(); - _comment = new StringBuffer(); + _comment = new StringBuilder(); } //-- JDocComment /** @@ -256,7 +256,7 @@ public void setComment( String comment ) */ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append( "/**\n" ); sb.append( " * " ); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocDescriptor.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocDescriptor.java index 601ddd5e0..161378546 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocDescriptor.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JDocDescriptor.java @@ -354,7 +354,7 @@ public void setName( String name ) **/ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); boolean allowName = true; switch ( type ) { diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JField.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JField.java index 5d3cc0c46..0bfe24f9f 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JField.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JField.java @@ -236,7 +236,7 @@ protected void setDeclaringClass( JClass declaringClass ) public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append( modifiers.toString() ); sb.append( ' ' ); sb.append( type ); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethodSignature.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethodSignature.java index 215b60d81..9c2316e6b 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethodSignature.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JMethodSignature.java @@ -186,7 +186,7 @@ public void addParameter( JParameter parameter ) //-- check current params if ( params.get( pName ) != null ) { - StringBuffer err = new StringBuffer(); + StringBuilder err = new StringBuilder(); err.append( "A parameter already exists for this method, " ); err.append( name ); err.append( ", with the name: " ); @@ -397,7 +397,7 @@ public void print( JSourceWriter jsw, boolean printJavaDoc ) public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); if ( returnType != null ) { sb.append( returnType ); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JModifiers.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JModifiers.java index 140733d06..1f036ea8f 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JModifiers.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JModifiers.java @@ -313,7 +313,7 @@ public void setTransient( boolean isTransient ) **/ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); //-- visibility switch ( visibility ) diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JParameter.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JParameter.java index 0b29db104..b2f2aa20c 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JParameter.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JParameter.java @@ -150,7 +150,7 @@ public void setType( JType type ) **/ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); if ( annotations != null ) { sb.append( annotations.toString() ); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JSourceCode.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JSourceCode.java index d785eca8e..d96d59d9f 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JSourceCode.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JSourceCode.java @@ -245,7 +245,7 @@ public void unindent() **/ public String toString() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); String lineSeparator = System.getProperty( "line.separator" ); for ( int i = 0; i < source.size(); i++ ) { @@ -274,14 +274,14 @@ private void addCodeStatement( JCodeStatement jcs ) class JCodeStatement { - private StringBuffer value = null; + private StringBuilder value = null; static public short DEFAULT_INDENTSIZE = 4; private short indentSize = DEFAULT_INDENTSIZE; JCodeStatement() { super(); - value = new StringBuffer(); + value = new StringBuilder(); } //-- JCodeStatement JCodeStatement( String statement ) @@ -319,7 +319,7 @@ public String toString() return ""; } - StringBuffer sb = new StringBuffer( indentSize + value.length() ); + StringBuilder sb = new StringBuilder( indentSize + value.length() ); for ( int i = 0; i < indentSize; i++ ) sb.append( ' ' ); sb.append( value.toString() ); return sb.toString(); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JStructure.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JStructure.java index 971e4ffa3..0fae2234c 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JStructure.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JStructure.java @@ -723,7 +723,7 @@ public void printPackageDeclaration( JSourceWriter jsw ) public abstract void print(JSourceWriter jsw); - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); printHeader(); diff --git a/modello-plugins/modello-plugin-java/src/test/resources/models/maven.mdo b/modello-plugins/modello-plugin-java/src/test/resources/models/maven.mdo index 99e715b99..42edd1f5f 100644 --- a/modello-plugins/modello-plugin-java/src/test/resources/models/maven.mdo +++ b/modello-plugins/modello-plugin-java/src/test/resources/models/maven.mdo @@ -484,7 +484,7 @@ public String getId() { - StringBuffer id = new StringBuffer(); + StringBuilder id = new StringBuilder(); id.append( getGroupId() ); id.append( ":" ); diff --git a/modello-plugins/modello-plugin-sax/src/test/resources/maven.mdo b/modello-plugins/modello-plugin-sax/src/test/resources/maven.mdo index 97a7e84a5..065c32ca8 100644 --- a/modello-plugins/modello-plugin-sax/src/test/resources/maven.mdo +++ b/modello-plugins/modello-plugin-sax/src/test/resources/maven.mdo @@ -515,7 +515,7 @@ public String getId() { - StringBuffer id = new StringBuffer(); + StringBuilder id = new StringBuilder(); id.append( getGroupId() ); id.append( ":" ); diff --git a/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxReaderGenerator.java b/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxReaderGenerator.java index 48079a55a..172628762 100644 --- a/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxReaderGenerator.java +++ b/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxReaderGenerator.java @@ -1544,7 +1544,7 @@ private void writeBuildDomMethod( JClass jClass ) sc.add( "}" ); sc.add( "else" ); sc.add( "{" ); - sc.addIndented( "values.push( new StringBuffer() );" ); + sc.addIndented( "values.push( new StringBuilder() );" ); sc.add( "}" ); sc.add( "int attributesSize = xmlStreamReader.getAttributeCount();" ); @@ -1566,7 +1566,7 @@ private void writeBuildDomMethod( JClass jClass ) sc.add( "else if ( eventType == XMLStreamConstants.CHARACTERS )" ); sc.add( "{" ); sc.indent(); - sc.add( "StringBuffer valueBuffer = (StringBuffer) values.peek();" ); + sc.add( "StringBuilder valueBuffer = (StringBuilder) values.peek();" ); sc.add( "String text = xmlStreamReader.getText();" ); diff --git a/modello-plugins/modello-plugin-stax/src/test/resources/maven.mdo b/modello-plugins/modello-plugin-stax/src/test/resources/maven.mdo index a9245869c..fa1ea8aea 100644 --- a/modello-plugins/modello-plugin-stax/src/test/resources/maven.mdo +++ b/modello-plugins/modello-plugin-stax/src/test/resources/maven.mdo @@ -492,7 +492,7 @@ public String getId() { - StringBuffer id = new StringBuffer(); + StringBuilder id = new StringBuilder(); id.append( getGroupId() ); id.append( ":" ); diff --git a/modello-plugins/modello-plugin-xdoc/src/main/java/org/codehaus/modello/plugin/xdoc/XdocGenerator.java b/modello-plugins/modello-plugin-xdoc/src/main/java/org/codehaus/modello/plugin/xdoc/XdocGenerator.java index 5e9e031d4..9764c1228 100644 --- a/modello-plugins/modello-plugin-xdoc/src/main/java/org/codehaus/modello/plugin/xdoc/XdocGenerator.java +++ b/modello-plugins/modello-plugin-xdoc/src/main/java/org/codehaus/modello/plugin/xdoc/XdocGenerator.java @@ -488,7 +488,7 @@ private String getModelXmlDescriptor( ModelClass rootModelClass ) private String getElementXmlDescriptor( ModelClass modelClass, ModelAssociation association, Stack stack ) throws ModelloRuntimeException { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); appendSpacer( sb, stack.size() ); @@ -727,11 +727,11 @@ else if ( xmlFieldMetadata.getTagName() != null ) } /** - * Appends the required spacers to the given StringBuffer. + * Appends the required spacers to the given StringBuilder. * @param sb where to append the spacers * @param depth the depth of spacers to generate */ - private static void appendSpacer( StringBuffer sb, int depth ) + private static void appendSpacer( StringBuilder sb, int depth ) { for ( int i = 0; i < depth; i++ ) { diff --git a/modello-plugins/modello-plugin-xdoc/src/test/resources/maven.mdo b/modello-plugins/modello-plugin-xdoc/src/test/resources/maven.mdo index 7a2a360ec..6fca9db88 100644 --- a/modello-plugins/modello-plugin-xdoc/src/test/resources/maven.mdo +++ b/modello-plugins/modello-plugin-xdoc/src/test/resources/maven.mdo @@ -492,7 +492,7 @@ public String getId() { - StringBuffer id = new StringBuffer(); + StringBuilder id = new StringBuilder(); id.append( getGroupId() ); id.append( ":" ); diff --git a/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java b/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java index e68605dd6..6c2a78248 100644 --- a/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java +++ b/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java @@ -1163,7 +1163,7 @@ private void writeBuildDomMethod( JClass jClass ) sc.add( "}" ); sc.add( "else" ); sc.add( "{" ); - sc.addIndented( "values.push( new StringBuffer() );" ); + sc.addIndented( "values.push( new StringBuilder() );" ); sc.add( "}" ); sc.add( "int attributesSize = parser.getAttributeCount();" ); @@ -1184,7 +1184,7 @@ private void writeBuildDomMethod( JClass jClass ) sc.add( "else if ( eventType == XmlPullParser.TEXT )" ); sc.add( "{" ); sc.indent(); - sc.add( "StringBuffer valueBuffer = (StringBuffer) values.peek();" ); + sc.add( "StringBuilder valueBuffer = (StringBuilder) values.peek();" ); sc.add( "String text = parser.getText();" ); diff --git a/modello-plugins/modello-plugin-xpp3/src/test/resources/maven.mdo b/modello-plugins/modello-plugin-xpp3/src/test/resources/maven.mdo index 97a7e84a5..065c32ca8 100644 --- a/modello-plugins/modello-plugin-xpp3/src/test/resources/maven.mdo +++ b/modello-plugins/modello-plugin-xpp3/src/test/resources/maven.mdo @@ -515,7 +515,7 @@ public String getId() { - StringBuffer id = new StringBuffer(); + StringBuilder id = new StringBuilder(); id.append( getGroupId() ); id.append( ":" ); diff --git a/modello-plugins/modello-plugin-xsd/src/test/resources/maven.mdo b/modello-plugins/modello-plugin-xsd/src/test/resources/maven.mdo index 545a5e273..42c80f848 100644 --- a/modello-plugins/modello-plugin-xsd/src/test/resources/maven.mdo +++ b/modello-plugins/modello-plugin-xsd/src/test/resources/maven.mdo @@ -499,7 +499,7 @@ public String getId() { - StringBuffer id = new StringBuffer(); + StringBuilder id = new StringBuilder(); id.append( getGroupId() ); id.append( ":" ); From c09397cec7984f16f58e3bdffcf34c2c291f0bca Mon Sep 17 00:00:00 2001 From: rfscholte Date: Mon, 31 Dec 2018 21:27:23 +0100 Subject: [PATCH 3/3] Support TypeVariables --- .../codehaus/modello/core/io/ModelReader.java | 24 +++++++ .../codehaus/modello/model/ModelClass.java | 24 ++++++- .../codehaus/modello/model/ModelField.java | 17 ++++- .../modello/model/ModelInterface.java | 20 ++++++ .../plugin/java/JavaModelloGenerator.java | 21 +++++++ .../plugin/java/javasource/JClass.java | 46 ++++++++++---- .../plugin/java/javasource/JInterface.java | 36 ++++++++--- .../plugin/java/javasource/JTypeVariable.java | 24 +++++++ .../java/TypeVariablesJavaGeneratorTest.java | 62 +++++++++++++++++++ .../test/resources/models/typevariables.mdo | 59 ++++++++++++++++++ 10 files changed, 311 insertions(+), 22 deletions(-) create mode 100644 modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JTypeVariable.java create mode 100644 modello-plugins/modello-plugin-java/src/test/java/org/codehaus/modello/plugin/java/TypeVariablesJavaGeneratorTest.java create mode 100644 modello-plugins/modello-plugin-java/src/test/resources/models/typevariables.mdo diff --git a/modello-core/src/main/java/org/codehaus/modello/core/io/ModelReader.java b/modello-core/src/main/java/org/codehaus/modello/core/io/ModelReader.java index bafb3ab83..985044a96 100644 --- a/modello-core/src/main/java/org/codehaus/modello/core/io/ModelReader.java +++ b/modello-core/src/main/java/org/codehaus/modello/core/io/ModelReader.java @@ -246,6 +246,18 @@ else if ( "codeSegments".equals( parser.getName() ) ) { parseCodeSegment( modelInterface, parser ); } + else if ( "typeParameters".equals( parser.getName() ) ) + { + List typeParametersList = new ArrayList(); + while ( parser.nextTag() == XmlPullParser.START_TAG ) + { + if ( "typeParameter".equals( parser.getName() ) ) + { + typeParametersList.add( parser.nextText() ); + } + } + modelInterface.setTypeParameters( typeParametersList ); + } else { parser.nextText(); @@ -298,6 +310,18 @@ else if ( "codeSegments".equals( parser.getName() ) ) { parseCodeSegment( modelClass, parser ); } + else if ( "typeParameters".equals( parser.getName() ) ) + { + List typeParametersList = new ArrayList(); + while ( parser.nextTag() == XmlPullParser.START_TAG ) + { + if ( "typeParameter".equals( parser.getName() ) ) + { + typeParametersList.add( parser.nextText() ); + } + } + modelClass.setTypeParameters( typeParametersList ); + } else { parser.nextText(); diff --git a/modello-core/src/main/java/org/codehaus/modello/model/ModelClass.java b/modello-core/src/main/java/org/codehaus/modello/model/ModelClass.java index 163c8be01..17f5b4369 100644 --- a/modello-core/src/main/java/org/codehaus/modello/model/ModelClass.java +++ b/modello-core/src/main/java/org/codehaus/modello/model/ModelClass.java @@ -43,6 +43,8 @@ public class ModelClass private boolean isInternalSuperClass; private List interfaces; + + private List typeParameters; private List fields; @@ -67,7 +69,7 @@ public void setSuperClass( String superClass ) { this.superClass = superClass; } - + // ---------------------------------------------------------------------- // Interfaces // ---------------------------------------------------------------------- @@ -96,6 +98,26 @@ public void addInterface( String modelInterface ) getInterfaces().add( modelInterface ); } + + // ---------------------------------------------------------------------- + // TypeVariables + // ---------------------------------------------------------------------- + + public List getTypeParameters() + { + if ( typeParameters == null ) + { + return new ArrayList(); + } + return typeParameters; + } + + public void setTypeParameters( List typeParameters ) + { + this.typeParameters = typeParameters; + } + + // ---------------------------------------------------------------------- // Field diff --git a/modello-core/src/main/java/org/codehaus/modello/model/ModelField.java b/modello-core/src/main/java/org/codehaus/modello/model/ModelField.java index abb49707d..fcc515587 100644 --- a/modello-core/src/main/java/org/codehaus/modello/model/ModelField.java +++ b/modello-core/src/main/java/org/codehaus/modello/model/ModelField.java @@ -210,6 +210,21 @@ else if ( "char".equals( type ) ) } } + public boolean isTypeVariable() + { + String type = getType(); + for ( String t : modelClass.getTypeParameters() ) + { + String name = t.split( " " )[0]; + if ( type.equals( name ) ) + { + return true; + } + } + return false; + } + + public void validateElement() throws ModelValidationException { @@ -219,7 +234,7 @@ public void validateElement() // TODO: these definitions are duplicated throughout. Defined centrally, and loop through in the various uses - if ( !isPrimitive() && !isPrimitiveArray() ) + if ( !isPrimitive() && !isPrimitiveArray() && !isTypeVariable() ) { throw new ModelValidationException( "Field '" + getName() + "': Illegal type: '" + type + "'." ); } diff --git a/modello-core/src/main/java/org/codehaus/modello/model/ModelInterface.java b/modello-core/src/main/java/org/codehaus/modello/model/ModelInterface.java index 34d3a6a60..20b5c5b03 100644 --- a/modello-core/src/main/java/org/codehaus/modello/model/ModelInterface.java +++ b/modello-core/src/main/java/org/codehaus/modello/model/ModelInterface.java @@ -34,6 +34,8 @@ public class ModelInterface extends ModelType { private String superInterface; + + private List typeParameters; public ModelInterface() { @@ -54,6 +56,24 @@ public String getSuperInterface() { return superInterface; } + + // ---------------------------------------------------------------------- + // TypeVariables + // ---------------------------------------------------------------------- + + public List getTypeParameters() + { + if ( typeParameters == null ) + { + return new ArrayList(); + } + return typeParameters; + } + + public void setTypeParameters( List typeParameters ) + { + this.typeParameters = typeParameters; + } /** * {@inheritDoc} diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java index 7cadd74a2..b9d967c68 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/JavaModelloGenerator.java @@ -55,6 +55,7 @@ import org.codehaus.modello.plugin.java.javasource.JSourceCode; import org.codehaus.modello.plugin.java.javasource.JSourceWriter; import org.codehaus.modello.plugin.java.javasource.JType; +import org.codehaus.modello.plugin.java.javasource.JTypeVariable; import org.codehaus.modello.plugin.java.metadata.JavaAssociationMetadata; import org.codehaus.modello.plugin.java.metadata.JavaClassMetadata; import org.codehaus.modello.plugin.java.metadata.JavaFieldMetadata; @@ -126,6 +127,8 @@ private void generateJava() JClass jClass = new JClass( packageName + '.' + modelClass.getName() ); + addTypeParameters( jClass, modelClass ); + initHeader( jClass ); suppressAllWarnings( objectModel, jClass ); @@ -294,6 +297,22 @@ else if ( locationTrackerClass != null && modelClass != sourceTrackerClass && !s } } + private void addTypeParameters( JClass jClass, ModelClass modelClass ) + { + for ( String value : modelClass.getTypeParameters() ) + { + jClass.addTypeParameter( new JTypeVariable( value ) ); + } + } + + private void addTypeParameters( JInterface jInterface, ModelInterface modelInterface) + { + for ( String value : modelInterface.getTypeParameters() ) + { + jInterface.addTypeParameter( new JTypeVariable( value ) ); + } + } + private void generateInterface( ModelInterface modelInterface ) throws ModelloException, IOException { @@ -307,6 +326,8 @@ private void generateInterface( ModelInterface modelInterface ) initHeader( jInterface ); + addTypeParameters( jInterface, modelInterface ); + suppressAllWarnings( objectModel, jInterface ); if ( modelInterface.getSuperInterface() != null ) diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JClass.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JClass.java index f14068d2b..133d276e7 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JClass.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JClass.java @@ -75,6 +75,7 @@ */ import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; import java.util.LinkedHashMap; import java.util.List; @@ -93,6 +94,7 @@ public class JClass extends JStructure { + private List _typeParameters; /** * The list of constructors for this JClass @@ -134,10 +136,11 @@ public JClass( String name ) throws IllegalArgumentException { super( name ); - _constructors = new ArrayList(); + _typeParameters = new ArrayList<>(); + _constructors = new ArrayList<>(); _fields = new LinkedHashMap<>(); - _methods = new ArrayList(); - _innerClasses = new ArrayList(); + _methods = new ArrayList<>(); + _innerClasses = new ArrayList<>(); //-- initialize default Java doc getJDocComment().appendComment( "Class " + getLocalName() + "." ); @@ -200,6 +203,8 @@ public void addField( JField jField ) _fields.put( name, jField ); } //-- addField + + /** * Adds the given JMember to this JClass @@ -513,6 +518,11 @@ public String getSuperClass() { return _superClass; } //-- getSuperClass + + public void addTypeParameter( JTypeVariable typeParameter ) + { + this._typeParameters.add( typeParameter ); + } /** * Prints the source code for this JClass to the given JSourceWriter @@ -596,31 +606,45 @@ public void print( JSourceWriter jsw, boolean classOnly ) JModifiers modifiers = getModifiers(); if ( modifiers.isPrivate() ) { - buffer.append( "private " ); + jsw.write( "private " ); } else if ( modifiers.isPublic() ) { - buffer.append( "public " ); + jsw.write( "public " ); } if ( modifiers.isAbstract() ) { - buffer.append( "abstract " ); + jsw.write( "abstract " ); } if ( this instanceof JInnerClass && modifiers.isStatic() ) { - buffer.append( "static " ); + jsw.write( "static " ); } if ( modifiers.isFinal() ) { - buffer.append( "final " ); + jsw.write( "final " ); } - buffer.append( "class " ); - buffer.append( getLocalName() ); - jsw.writeln( buffer.toString() ); + jsw.write( "class " ); + jsw.write( getLocalName() ); + if ( !_typeParameters.isEmpty() ) + { + jsw.write( '<' ); + Enumeration typeParamEnum = Collections.enumeration( _typeParameters ); + typeParamEnum.nextElement().print( jsw ); + while ( typeParamEnum.hasMoreElements() ) + { + jsw.write( ", " ); + typeParamEnum.nextElement().print( jsw ); + } + jsw.write( '>' ); + } + + jsw.writeln(); + buffer.setLength( 0 ); jsw.indent(); diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JInterface.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JInterface.java index 28b70693d..605fc197a 100644 --- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JInterface.java +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JInterface.java @@ -68,6 +68,7 @@ */ import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; import java.util.LinkedHashMap; import java.util.List; @@ -85,7 +86,7 @@ **/ public final class JInterface extends JStructure { - + private List _typeParameters; /** * The fields for this JInterface @@ -109,7 +110,8 @@ public JInterface( String name ) throws IllegalArgumentException { super( name ); - methods = new ArrayList(); + methods = new ArrayList<>(); + _typeParameters = new ArrayList<>(); //-- initialize default Java doc getJDocComment().appendComment( "Interface " + getLocalName() + "." ); @@ -335,7 +337,11 @@ public JMethodSignature getMethod( int index ) return methods.get( index ); } //-- getMethod - + public void addTypeParameter( JTypeVariable typeParameter ) + { + this._typeParameters.add( typeParameter ); + } + /** * Prints the source code for this JInterface to the given JSourceWriter * @@ -386,22 +392,34 @@ public void print( JSourceWriter jsw, boolean classOnly ) JModifiers modifiers = getModifiers(); if ( modifiers.isPrivate() ) { - buffer.append( "private " ); + jsw.write( "private " ); } else if ( modifiers.isPublic() ) { - buffer.append( "public " ); + jsw.write( "public " ); } if ( modifiers.isAbstract() ) { - buffer.append( "abstract " ); + jsw.write( "abstract " ); } - buffer.append( "interface " ); - buffer.append( getLocalName() ); + jsw.write( "interface " ); + jsw.write( getLocalName() ); jsw.writeln( buffer.toString() ); - buffer.setLength( 0 ); + if ( !_typeParameters.isEmpty() ) + { + jsw.write( '<' ); + Enumeration typeParamEnum = Collections.enumeration( _typeParameters ); + typeParamEnum.nextElement().print( jsw ); + while ( typeParamEnum.hasMoreElements() ) + { + jsw.write( ", " ); + typeParamEnum.nextElement().print( jsw ); + } + jsw.write( '>' ); + } + jsw.indent(); if ( getInterfaceCount() > 0 ) diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JTypeVariable.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JTypeVariable.java new file mode 100644 index 000000000..c74756c03 --- /dev/null +++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/javasource/JTypeVariable.java @@ -0,0 +1,24 @@ +package org.codehaus.modello.plugin.java.javasource; + +public class JTypeVariable +{ + private final String value; + + private final String name; + + public JTypeVariable( String value ) + { + this.value = value; + this.name = value.split( " " )[0]; + } + + public String getName() + { + return name; + } + + public void print( JSourceWriter jsw ) + { + jsw.write( value ); + } +} diff --git a/modello-plugins/modello-plugin-java/src/test/java/org/codehaus/modello/plugin/java/TypeVariablesJavaGeneratorTest.java b/modello-plugins/modello-plugin-java/src/test/java/org/codehaus/modello/plugin/java/TypeVariablesJavaGeneratorTest.java new file mode 100644 index 000000000..898890c54 --- /dev/null +++ b/modello-plugins/modello-plugin-java/src/test/java/org/codehaus/modello/plugin/java/TypeVariablesJavaGeneratorTest.java @@ -0,0 +1,62 @@ +package org.codehaus.modello.plugin.java; + +/* + * Copyright (c) 2004, Codehaus.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import java.util.Properties; + +import org.codehaus.modello.AbstractModelloJavaGeneratorTest; +import org.codehaus.modello.core.ModelloCore; +import org.codehaus.modello.model.Model; + +/** + * @version $Id: TmpJavaGeneratorTest.java 1125 2009-01-10 20:29:32Z hboutemy $ + */ +public class TypeVariablesJavaGeneratorTest + extends AbstractModelloJavaGeneratorTest +{ + public TypeVariablesJavaGeneratorTest() + { + super( "typevariables" ); + } + + public void testJavaGeneratorWithAnnotations() + throws Throwable + { + if ( skipJava5FeatureTest() ) + { + return; + } + + ModelloCore modello = (ModelloCore) lookup( ModelloCore.ROLE ); + + Model model = modello.loadModel( getXmlResourceReader( "/models/typevariables.mdo" ) ); + + Properties parameters = getModelloParameters( "1.0.0", true ); + + modello.generate( model, "java", parameters ); + + compileGeneratedSources( true ); + +// verifyCompiledGeneratedSources( "AnnotationsVerifier" ); + } +} \ No newline at end of file diff --git a/modello-plugins/modello-plugin-java/src/test/resources/models/typevariables.mdo b/modello-plugins/modello-plugin-java/src/test/resources/models/typevariables.mdo new file mode 100644 index 000000000..46d9d20c2 --- /dev/null +++ b/modello-plugins/modello-plugin-java/src/test/resources/models/typevariables.mdo @@ -0,0 +1,59 @@ + + + 1.0.0 + untitledModel + + + + Box + 1.0.0 + + T + + + + t + T + + + + + OrderedPair + + K + V + + + Pair<K, V> + + + + key + K + + + value + V + + + + + + + + Pair + + K + V + + + + + K getKey(); + V getValue(); + + + + + +