001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.codegen.bean;
024:
025: import java.util.*;
026: import com.mchange.v2.log.*;
027:
028: import java.lang.reflect.Modifier;
029: import java.io.IOException;
030: import com.mchange.v2.codegen.CodegenUtils;
031: import com.mchange.v2.codegen.IndentedWriter;
032:
033: /**
034: * Writes a constructor that takes an explicitly listed subset of a bean's properties
035: * for its arguments, and sets these properties initial values appropriately.
036: * Skips any specified names for properties that are not found in a bean being generated.
037: * Writes nothing if there are none of the property names are properties of the bean.
038: */
039: public class ExplicitPropsConstructorGeneratorExtension implements
040: GeneratorExtension {
041: final static MLogger logger = MLog
042: .getLogger(ExplicitPropsConstructorGeneratorExtension.class);
043:
044: String[] propNames;
045:
046: boolean skips_silently = false;
047:
048: public ExplicitPropsConstructorGeneratorExtension() {
049: }
050:
051: public ExplicitPropsConstructorGeneratorExtension(String[] propNames) {
052: this .propNames = propNames;
053: }
054:
055: public String[] getPropNames() {
056: return (String[]) propNames.clone();
057: }
058:
059: public void setPropNames(String[] propNames) {
060: this .propNames = (String[]) propNames.clone();
061: }
062:
063: public boolean isSkipsSilently() {
064: return skips_silently;
065: }
066:
067: public void setsSkipsSilently(boolean skips_silently) {
068: this .skips_silently = skips_silently;
069: }
070:
071: int ctor_modifiers = Modifier.PUBLIC;
072:
073: public Collection extraGeneralImports() {
074: return Collections.EMPTY_SET;
075: }
076:
077: public Collection extraSpecificImports() {
078: return Collections.EMPTY_SET;
079: }
080:
081: public Collection extraInterfaceNames() {
082: return Collections.EMPTY_SET;
083: }
084:
085: public void generate(ClassInfo info, Class super classType,
086: Property[] props, Class[] propTypes, IndentedWriter iw)
087: throws IOException {
088: Map propNamesToProps = new HashMap();
089: for (int i = 0, len = props.length; i < len; ++i)
090: propNamesToProps.put(props[i].getName(), props[i]);
091:
092: List subPropsList = new ArrayList(propNames.length);
093: for (int i = 0, len = propNames.length; i < len; ++i) {
094: Property p = (Property) propNamesToProps.get(propNames[i]);
095: if (p == null)
096: logger
097: .warning("Could not include property '"
098: + propNames[i]
099: + "' in explicit-props-constructor generated for bean class '"
100: + info.getClassName()
101: + "' because the property is not defined for the bean. Skipping.");
102: else
103: subPropsList.add(p);
104: }
105:
106: if (subPropsList.size() > 0) {
107: Property[] subProps = (Property[]) subPropsList
108: .toArray(new Property[subPropsList.size()]);
109:
110: iw.print(CodegenUtils.getModifierString(ctor_modifiers));
111: iw.print(info.getClassName() + "( ");
112: BeangenUtils.writeArgList(subProps, true, iw);
113: iw.println(" )");
114: iw.println("{");
115: iw.upIndent();
116:
117: for (int i = 0, len = subProps.length; i < len; ++i) {
118: iw.print("this." + subProps[i].getName() + " = ");
119: String setExp = subProps[i]
120: .getDefensiveCopyExpression();
121: if (setExp == null)
122: setExp = subProps[i].getName();
123: iw.println(setExp + ';');
124: }
125:
126: iw.downIndent();
127: iw.println("}");
128: }
129: }
130: }
|