KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > codegen > bean > ExplicitPropsConstructorGeneratorExtension


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.codegen.bean;
25
26 import java.util.*;
27 import com.mchange.v2.log.*;
28
29 import java.lang.reflect.Modifier JavaDoc;
30 import java.io.IOException JavaDoc;
31 import com.mchange.v2.codegen.CodegenUtils;
32 import com.mchange.v2.codegen.IndentedWriter;
33
34
35 /**
36  * Writes a constructor that takes an explicitly listed subset of a bean's properties
37  * for its arguments, and sets these properties initial values appropriately.
38  * Skips any specified names for properties that are not found in a bean being generated.
39  * Writes nothing if there are none of the property names are properties of the bean.
40  */

41 public class ExplicitPropsConstructorGeneratorExtension implements GeneratorExtension
42 {
43     final static MLogger logger = MLog.getLogger( ExplicitPropsConstructorGeneratorExtension.class );
44
45     String JavaDoc[] propNames;
46
47     boolean skips_silently = false;
48
49     public ExplicitPropsConstructorGeneratorExtension()
50     {}
51
52     public ExplicitPropsConstructorGeneratorExtension(String JavaDoc[] propNames)
53     { this.propNames = propNames; }
54
55     public String JavaDoc[] getPropNames()
56     { return (String JavaDoc[]) propNames.clone(); }
57
58     public void setPropNames(String JavaDoc[] propNames)
59     { this.propNames = (String JavaDoc[]) propNames.clone(); }
60
61     public boolean isSkipsSilently()
62     { return skips_silently; }
63
64     public void setsSkipsSilently( boolean skips_silently )
65     { this.skips_silently = skips_silently; }
66
67     int ctor_modifiers = Modifier.PUBLIC;
68
69     public Collection extraGeneralImports()
70     { return Collections.EMPTY_SET; }
71
72     public Collection extraSpecificImports()
73     { return Collections.EMPTY_SET; }
74
75     public Collection extraInterfaceNames()
76     { return Collections.EMPTY_SET; }
77
78     public void generate(ClassInfo info, Class JavaDoc superclassType, Property[] props, Class JavaDoc[] propTypes, IndentedWriter iw)
79     throws IOException JavaDoc
80     {
81     Map propNamesToProps = new HashMap();
82     for (int i = 0, len = props.length; i < len; ++i)
83         propNamesToProps.put( props[i].getName(), props[i] );
84
85     List subPropsList = new ArrayList( propNames.length );
86     for (int i = 0, len = propNames.length; i < len; ++i)
87         {
88         Property p = (Property) propNamesToProps.get( propNames[i] );
89         if ( p == null )
90             logger.warning("Could not include property '" + propNames[i] +"' in explicit-props-constructor generated for bean class '" +
91                    info.getClassName() +"' because the property is not defined for the bean. Skipping.");
92         else
93             subPropsList.add(p);
94         }
95
96     if (subPropsList.size() > 0)
97         {
98         Property[] subProps = (Property[]) subPropsList.toArray( new Property[ subPropsList.size() ] );
99
100         iw.print( CodegenUtils.getModifierString( ctor_modifiers ) );
101         iw.print( info.getClassName() + "( ");
102         BeangenUtils.writeArgList(subProps, true, iw);
103         iw.println(" )");
104         iw.println("{");
105         iw.upIndent();
106         
107         for (int i = 0, len = subProps.length; i < len; ++i)
108             {
109             iw.print("this." + subProps[i].getName() + " = ");
110             String JavaDoc setExp = subProps[i].getDefensiveCopyExpression();
111             if (setExp == null)
112                 setExp = subProps[i].getName();
113             iw.println(setExp + ';');
114             }
115         
116         iw.downIndent();
117         iw.println("}");
118         }
119     }
120 }
121
Popular Tags