001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.beans;
043:
044: import java.awt.Image;
045: import java.beans.IntrospectionException;
046: import javax.lang.model.element.ExecutableElement;
047: import javax.lang.model.element.VariableElement;
048: import javax.lang.model.type.DeclaredType;
049: import javax.lang.model.type.TypeKind;
050: import javax.lang.model.type.TypeMirror;
051: import org.netbeans.api.java.source.ElementHandle;
052: import org.netbeans.api.java.source.TypeMirrorHandle;
053:
054: import static org.netbeans.modules.beans.BeanUtils.*;
055:
056: /** Class representing a JavaBeans Property
057: * @author Petr Hrebejk
058: */
059: ;
060: public class PropertyPattern extends Pattern {
061:
062: /** Constant for READ/WRITE mode of properties */
063: public static final int READ_WRITE = 1;
064: /** Constant for READ ONLY mode of properties */
065: public static final int READ_ONLY = 2;
066: /** Constant for WRITE ONLY mode of properties */
067: public static final int WRITE_ONLY = 4;
068:
069: /** Constant for PropertyChange */
070: static final String PROPERTY_CHANGE = "firePropertyChange"; // NOI18N
071: /** Constant for VetoableChange */
072: static final String VETOABLE_CHANGE = "fireVetoableChange"; // NOI18N
073:
074: /** Getter method of this property */
075: protected ElementHandle<ExecutableElement> getterMethod;
076: /** Setter method of this property */
077: protected ElementHandle<ExecutableElement> setterMethod;
078: /** Field which probably belongs to this property */
079: protected ElementHandle<VariableElement> estimatedField;
080:
081: protected String typeName;
082:
083: /** Creates new PropertyPattern one of the methods may be null.
084: * @param patternAnalyser patternAnalyser which creates this Property.
085: * @param getterMethod getterMethod of the property or <CODE>null</CODE>.
086: * @param setterMethod setterMethod of the property or <CODE>null</CODE>.
087: * @throws IntrospectionException If specified methods do not follow beans Property rules.
088: */
089: public PropertyPattern(PatternAnalyser patternAnalyser,
090: ExecutableElement getterMethod,
091: ExecutableElement setterMethod,
092: VariableElement estimatedField, TypeMirror type, String name)
093: throws IntrospectionException {
094:
095: super (patternAnalyser, Pattern.Kind.PROPERTY, name,
096: TypeMirrorHandle.create(type));
097:
098: this .getterMethod = getterMethod == null ? null : ElementHandle
099: .create(getterMethod);
100: this .setterMethod = setterMethod == null ? null : ElementHandle
101: .create(setterMethod);
102: this .estimatedField = estimatedField == null ? null
103: : ElementHandle.create(estimatedField);
104: this .typeName = typeAsString(type);
105:
106: }
107:
108: // Getters and setters -----------------------------------------------------
109:
110: @Override
111: public Image getIcon() {
112: switch (getMode()) {
113: case READ_ONLY:
114: return PROPERTY_READ;
115: case WRITE_ONLY:
116: return PROPERTY_WRITE;
117: case READ_WRITE:
118: return PROPERTY_READ_WRITE;
119: }
120: return null;
121: }
122:
123: @Override
124: public String getHtmlDisplayName() {
125: return name + " : <font color=" + TYPE_COLOR + "> " + typeName
126: + "</font>"; // NOI18N
127: }
128:
129: /** Gets the estimated field
130: * @return Field which (probably) belongs to the property.
131: */
132: public ElementHandle<VariableElement> getEstimatedField() {
133: return estimatedField;
134: }
135:
136: /** Returns the getter method
137: * @return Getter method of the property
138: */
139: public ElementHandle<ExecutableElement> getGetterMethod() {
140: return getterMethod;
141: }
142:
143: /** Returns the setter method
144: * @return Setter method of the property
145: */
146: public ElementHandle<ExecutableElement> getSetterMethod() {
147: return setterMethod;
148: }
149:
150: /** Sets the name of PropertyPattern
151: * @param name New name of the property.
152: * @throws JmiException If the modification of source code is impossible.
153: */
154: public void setName(String name) throws IllegalArgumentException {
155: throw new UnsupportedOperationException();
156: // if ( !Utilities.isJavaIdentifier( name ) )
157: // throw new IllegalArgumentException( "Invalid event source name" ); // NOI18N
158: //
159: // JMIUtils.beginTrans(true);
160: // boolean rollback = true;
161: // try {
162: // setNameImpl(name);
163: // rollback = false;
164: // } finally {
165: // JMIUtils.endTrans(rollback);
166: // }
167: }
168:
169: /** Returns the mode of the property {@link #READ_WRITE READ_WRITE}, {@link #READ_ONLY READ_ONLY}
170: * or {@link #WRITE_ONLY WRITE_ONLY}
171: * @return Mode of the property
172: */
173: public int getMode() {
174:
175: if (setterMethod != null && getterMethod != null)
176: return READ_WRITE;
177: else if (getterMethod != null && setterMethod == null)
178: return READ_ONLY;
179: else if (setterMethod != null && getterMethod == null)
180: return WRITE_ONLY;
181: else
182: return 0;
183: }
184:
185: // /** Creates new property pattern with extended options
186: // * @param patternAnalyser patternAnalyser which creates this Property.
187: // * @param name Name of the Property.
188: // * @param type Type of the Property.
189: // * @param mode {@link #READ_WRITE Mode} of the new property.
190: // * @param bound Is the Property bound?
191: // * @param constrained Is the property constrained?
192: // * @param withField Should be the private field for this property genareted?
193: // * @param withReturn Generate return statement in getter?
194: // * @param withSet Generate seter statement for private field in setter.
195: // * @param withSupport Generate PropertyChange support?
196: // * @return Newly created PropertyPattern.
197: // */
198: // static PropertyPattern create( PatternAnalyser patternAnalyser,
199: // String name, String type,
200: // int mode, boolean bound, boolean constrained,
201: // boolean withField, boolean withReturn,
202: // boolean withSet, boolean withSupport ) throws GenerateBeanException {
203: //
204: // return create(patternAnalyser, name, type, mode, bound, constrained, withField, withReturn, withSet, withSupport, false, false);
205: // }
206: //
207: // /** Creates new property pattern with extended options
208: // * @param patternAnalyser patternAnalyser which creates this Property.
209: // * @param name Name of the Property.
210: // * @param type Type of the Property.
211: // * @param mode {@link #READ_WRITE Mode} of the new property.
212: // * @param bound Is the Property bound?
213: // * @param constrained Is the property constrained?
214: // * @param withField Should be the private field for this property genareted?
215: // * @param withReturn Generate return statement in getter?
216: // * @param withSet Generate seter statement for private field in setter.
217: // * @param withSupport Generate PropertyChange support?
218: // * @param useSupport use change support without prompting
219: // * @param fromField signalize that all action are activatet on field
220: // * @return Newly created PropertyPattern.
221: // */
222: // static PropertyPattern create( PatternAnalyser patternAnalyser,
223: // String name, String type,
224: // int mode, boolean bound, boolean constrained,
225: // boolean withField, boolean withReturn,
226: // boolean withSet, boolean withSupport,
227: // boolean useSupport, boolean fromField ) throws GenerateBeanException {
228: //
229: // assert JMIUtils.isInsideTrans();
230: // PropertyPattern pp = new PropertyPattern( patternAnalyser );
231: //
232: // pp.name = name;
233: // pp.type = patternAnalyser.findType(type);
234: //
235: // // Generate field
236: // if ( ( withField || withSupport ) && !fromField ) {
237: // try {
238: // pp.generateField( true );
239: // } catch (GenerateBeanException e) {
240: // DialogDisplayer.getDefault().notify(
241: // new NotifyDescriptor.Message(
242: // PatternNode.getString("MSG_Cannot_Create_Field"), // NOI18N
243: // NotifyDescriptor.WARNING_MESSAGE));
244: // }
245: // }
246: //
247: // // Ensure property change support field and methods exist
248: // String supportName = null;
249: // String vetoSupportName = null;
250: //
251: // if ( withSupport ) {
252: // // i try to generate support - try to look up if not in the parent defined
253: //
254: // boolean boundSupport = bound;
255: // boolean constrainedSupport = constrained;
256: //
257: // if( !useSupport ){
258: // if( boundSupport )
259: // if( ( supportName = EventSetInheritanceAnalyser.showInheritanceEventDialog(EventSetInheritanceAnalyser.detectPropertyChangeSupport( pp.getDeclaringClass()), "PropertyChangeSupport")) != null ) // NOI18N
260: // boundSupport = false;
261: // if( constrainedSupport )
262: // if( ( vetoSupportName = EventSetInheritanceAnalyser.showInheritanceEventDialog(EventSetInheritanceAnalyser.detectVetoableChangeSupport( pp.getDeclaringClass()), "VetoableChangeSupport")) != null ) // NOI18N
263: // constrainedSupport = false;
264: // }
265: // else{
266: // if( boundSupport )
267: // if( ( supportName = EventSetInheritanceAnalyser.getInheritanceEventSupportName(EventSetInheritanceAnalyser.detectPropertyChangeSupport( pp.getDeclaringClass()), "PropertyChangeSupport")) != null ) // NOI18N
268: // boundSupport = false;
269: // if( constrainedSupport )
270: // if( ( vetoSupportName = EventSetInheritanceAnalyser.getInheritanceEventSupportName(EventSetInheritanceAnalyser.detectVetoableChangeSupport( pp.getDeclaringClass()), "VetoableChangeSupport")) != null ) // NOI18N
271: // constrainedSupport = false;
272: // }
273: //
274: // if ( boundSupport )
275: // supportName = BeanPatternGenerator.supportField( pp.getDeclaringClass() );
276: // if ( constrainedSupport )
277: // vetoSupportName = BeanPatternGenerator.vetoSupportField( pp.getDeclaringClass() );
278: //
279: // if ( boundSupport )
280: // BeanPatternGenerator.supportListenerMethods( pp.getDeclaringClass(), supportName );
281: // if ( constrainedSupport )
282: // BeanPatternGenerator.vetoSupportListenerMethods( pp.getDeclaringClass(), vetoSupportName );
283: // }
284: //
285: // if ( mode == READ_WRITE || mode == READ_ONLY )
286: // pp.generateGetterMethod( BeanPatternGenerator.propertyGetterBody( name, withReturn, !fromField ), true );
287: //
288: // if ( mode == READ_WRITE || mode == WRITE_ONLY )
289: // pp.generateSetterMethod( BeanPatternGenerator.propertySetterBody( name, pp.getType(),
290: // bound, constrained, withSet, withSupport, supportName, vetoSupportName, !fromField ), constrained, true );
291: //
292: // return pp;
293: // }
294: //
295: //
296: //
297: //
298: //
299: // private void setNameImpl( String name ) throws JmiException {
300: // assert JMIUtils.isInsideTrans();
301: // String oldName = this.name;
302: // this.name = name;
303: // name = capitalizeFirstLetter( name );
304: //
305: // if ( getterMethod != null ) {
306: // String getterMethodID = ( getterMethod.getName().startsWith("get") ? // NOI18N
307: // "get" : "is" ) + name ; // NOI18N
308: // getterMethod.setName( getterMethodID );
309: // String oldGetterComment = MessageFormat.format( PatternNode.getString( "COMMENT_PropertyGetter" ),
310: // new Object[] { oldName } );
311: // String newGetterComment = MessageFormat.format( PatternNode.getString( "COMMENT_PropertyGetter" ),
312: // new Object[] { getName() } );
313: // String javadocText = getterMethod.getJavadocText();
314: // if (javadocText != null &&
315: // oldGetterComment.trim().equals(javadocText.trim())) {
316: // getterMethod.setJavadocText( newGetterComment );
317: // }
318: // }
319: // if ( setterMethod != null ) {
320: // String setterMethodID = "set" + name; // NOI18N
321: // setterMethod.setName( setterMethodID );
322: // String oldSetterComment = MessageFormat.format( PatternNode.getString( "COMMENT_PropertySetter" ),
323: // new Object[] { oldName, oldName } );
324: // String newSetterComment = MessageFormat.format( PatternNode.getString( "COMMENT_PropertySetter" ),
325: // new Object[] { getName(), getName() } );
326: // String javadocText = setterMethod.getJavadocText();
327: // if (javadocText != null &&
328: // oldSetterComment.trim().equals(javadocText.trim())) {
329: // setterMethod.setJavadocText( newSetterComment );
330: // }
331: // }
332: //
333: // // Ask if to set the estimated field
334: // if ( estimatedField != null ) {
335: // String oldFieldComment = MessageFormat.format( PatternNode.getString( "COMMENT_PropertyField" ),
336: // new Object[] { oldName } );
337: // String newFieldComment = MessageFormat.format( PatternNode.getString( "COMMENT_PropertyField" ),
338: // new Object[] { getName() } );
339: // String javadocText = estimatedField.getJavadocText();
340: // if (javadocText != null &&
341: // oldFieldComment.trim().equals(javadocText.trim())) {
342: // estimatedField.setJavadocText(newFieldComment);
343: // }
344: //
345: // int mode = getMode();
346: // Format fmt = SourceNodes.createElementFormat ("{m} {t} {n}"); // NOI18N
347: // String mssg = MessageFormat.format( PatternNode.getString( "FMT_ChangeFieldName" ),
348: // new Object[] { fmt.format (estimatedField) } );
349: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation ( mssg, NotifyDescriptor.YES_NO_OPTION );
350: // if ( DialogDisplayer.getDefault().notify( nd ).equals( NotifyDescriptor.YES_OPTION ) ) {
351: // String fieldName = Introspector.decapitalize(name);
352: //
353: // if( PropertyActionSettings.getDefault().getPropStyle().equals(PropertyActionSettings.GENERATE_UNDERSCORED))
354: // fieldName = PropertyActionSettings.GENERATE_UNDERSCORED + fieldName;
355: // estimatedField.setName(fieldName);
356: // if ( (mode == READ_WRITE || mode == READ_ONLY) && getterMethod !=null ) {
357: // String existingGetterBody = getterMethod.getBodyText().trim();
358: // String oldGetterBody1 = BeanPatternGenerator.propertyGetterBody( oldName, true, true ).trim();
359: // String oldGetterBody2 = BeanPatternGenerator.propertyGetterBody( oldName, true, false ).trim();
360: // if (existingGetterBody.equals(oldGetterBody1)) {
361: // getterMethod.setBodyText(BeanPatternGenerator.propertyGetterBody( fieldName, true, true));
362: // } else if (existingGetterBody.equals(oldGetterBody2)) {
363: // getterMethod.setBodyText(BeanPatternGenerator.propertyGetterBody( fieldName, true, false));
364: // }
365: // }
366: // if ( (mode == READ_WRITE || mode == WRITE_ONLY) && setterMethod != null) {
367: // String existingSetterBody = setterMethod.getBodyText().trim();
368: // String oldSetterBody = BeanPatternGenerator.propertySetterBody (oldName, this.type, false, false, true, false, null, null).trim();
369: // if (existingSetterBody.equals(oldSetterBody)) {
370: // setterMethod.setBodyText(BeanPatternGenerator.propertySetterBody (fieldName, getType(), false, false, true, false, null, null));
371: // if ( setterMethod != null ) {
372: // Parameter param = (Parameter) setterMethod.getParameters().get(0);
373: // param.setName(Introspector.decapitalize( name ));
374: // }
375: // }
376: // }
377: // }
378: // }
379: // }
380: //
381: //
382: // /** Sets the property to be writable
383: // * @param mode New Mode {@link #READ_WRITE READ_WRITE}, {@link #READ_ONLY READ_ONLY}
384: // * or {@link #WRITE_ONLY WRITE_ONLY}
385: // * @throws GenerateBeanException If the modification of source code is impossible.
386: // */
387: // public void setMode( int mode ) throws GenerateBeanException, JmiException {
388: //
389: // if ( getMode() == mode )
390: // return;
391: //
392: // JMIUtils.beginTrans(true);
393: // boolean rollback = true;
394: // try {
395: // switch (mode) {
396: // case READ_WRITE:
397: // if (getterMethod == null)
398: // generateGetterMethod(null, true);
399: // if (setterMethod == null)
400: // generateSetterMethod(null, false, true);
401: // break;
402: // case READ_ONLY:
403: // if (getterMethod == null)
404: // generateGetterMethod(null, true);
405: // if (setterMethod != null) {
406: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation(PatternNode.getString("MSG_Delete_Setter") + PatternNode.getString("MSG_Continue_Confirm"), NotifyDescriptor.YES_NO_OPTION);
407: // DialogDisplayer.getDefault().notify(nd);
408: // if (nd.getValue().equals(NotifyDescriptor.YES_OPTION)) {
409: // deleteSetterMethod();
410: // }
411: // }
412: // break;
413: // case WRITE_ONLY:
414: // if (setterMethod == null)
415: // generateSetterMethod(null, false, true);
416: // if (getterMethod != null) {
417: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation(PatternNode.getString("MSG_Delete_Getter") + PatternNode.getString("MSG_Continue_Confirm"), NotifyDescriptor.YES_NO_OPTION);
418: // DialogDisplayer.getDefault().notify(nd);
419: // if (nd.getValue().equals(NotifyDescriptor.YES_OPTION)) {
420: // deleteGetterMethod();
421: // }
422: // }
423: // break;
424: // }
425: // rollback = false;
426: // } finally {
427: // JMIUtils.endTrans(rollback);
428: // }
429: // }
430: //
431: //
432: // /** Sets the type of propertyPattern
433: // * @param type New type of the property
434: // * @throws JmiException If the modification of source code is impossible
435: // */
436: // public void setType(Type type) throws JmiException {
437: // JMIUtils.beginTrans(true);
438: // boolean rollback = true;
439: // try {
440: // setTypeImpl(type);
441: // rollback = false;
442: // } finally {
443: // JMIUtils.endTrans(rollback);
444: // }
445: // }
446: //
447: // private void setTypeImpl(Type type) throws JmiException {
448: // assert JMIUtils.isInsideTrans();
449: // if ( this.type.equals( type ) )
450: // return;
451: //
452: // if (getterMethod != null ) {
453: // if (JMIUtils.isPrimitiveType(this.type, PrimitiveTypeKindEnum.BOOLEAN)) {
454: // getterMethod.setName("get" + capitalizeFirstLetter( getName() ) ); // NOI18N
455: // } else if (JMIUtils.isPrimitiveType(type, PrimitiveTypeKindEnum.BOOLEAN)) {
456: // String mssg = MessageFormat.format( PatternNode.getString( "FMT_ChangeToIs" ),
457: // new Object[] { capitalizeFirstLetter( getName() ) } );
458: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation ( mssg, NotifyDescriptor.YES_NO_OPTION );
459: // DialogDisplayer.getDefault().notify( nd );
460: // if( nd.getValue().equals( NotifyDescriptor.YES_OPTION ) ) {
461: // getterMethod.setName( "is" + capitalizeFirstLetter( getName() ) ); // NOI18N
462: // }
463: // }
464: // getterMethod.setType( type );
465: // }
466: //
467: // if (setterMethod != null ) {
468: // List/*<Parameter>*/ params = setterMethod.getParameters();
469: // for (Iterator it = params.iterator(); it.hasNext();) {
470: // Parameter param = (Parameter) it.next();
471: // Type oldType = param.getType();
472: // param.setType( type );
473: //
474: // String body = setterMethod.getBodyText();
475: // //test if body contains change support
476: // if( body != null && ( body.indexOf(PROPERTY_CHANGE) != -1 || body.indexOf(VETOABLE_CHANGE) != -1 ) ) {
477: // String mssg = MessageFormat.format( PatternNode.getString( "FMT_ChangeMethodBody" ),
478: // new Object[] { setterMethod.getName() } );
479: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation ( mssg, NotifyDescriptor.YES_NO_OPTION );
480: // DialogDisplayer.getDefault().notify( nd );
481: // if( nd.getValue().equals( NotifyDescriptor.YES_OPTION ) ) {
482: // String newBody = regeneratePropertySupport( setterMethod.getBodyText(), null, param.getName(), type, oldType );
483: // if( newBody != null )
484: // setterMethod.setBodyText(newBody);
485: //
486: // newBody = regeneratePropertySupport( setterMethod.getBodyText(), PROPERTY_CHANGE, param.getName(), type, oldType );
487: // if( newBody != null )
488: // setterMethod.setBodyText(newBody);
489: //
490: // newBody = regeneratePropertySupport( setterMethod.getBodyText(), VETOABLE_CHANGE, param.getName(), type, oldType );
491: // if( newBody != null )
492: // setterMethod.setBodyText(newBody);
493: // }
494: // }
495: // }
496: // }
497: //
498: // this.type = type;
499: //
500: // // Ask if to change estimated field Type
501: //
502: // if ( estimatedField != null ) {
503: // Format fmt = SourceNodes.createElementFormat("{m} {t} {n}"); // NOI18N
504: // String mssg = MessageFormat.format( PatternNode.getString( "FMT_ChangeFieldType" ),
505: // new Object[] { fmt.format (estimatedField) } );
506: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation ( mssg, NotifyDescriptor.YES_NO_OPTION );
507: // if ( DialogDisplayer.getDefault().notify( nd ).equals( NotifyDescriptor.YES_OPTION ) ) {
508: // estimatedField.setType(type);
509: // }
510: // else {
511: // estimatedField = null;
512: // }
513: // }
514: //
515: // }
516: //
517: // /**
518: // * @param methodBody old method body
519: // * @param changeType .. propertyChange, vetoableChange or null if need to change only support field
520: // * @param name of property
521: // * @param type new type of property value
522: // * @param oldType old type of property value
523: // * @return null if no change is possible or new body if it is
524: // */
525: // protected String regeneratePropertySupport( String methodBody, String changeType, String name, Type type, Type oldType ) throws JmiException {
526: // if( methodBody == null )
527: // return null;
528: //
529: // int first = -1;
530: // boolean post_index = false;
531: // boolean pre_index = false;
532: // String propertyStyle = PropertyActionSettings.getDefault().getPropStyle();
533: //
534: // assert JMIUtils.isInsideTrans();
535: // if( oldType instanceof Array)
536: // oldType = getPrimitiveType((Array) oldType);
537: // //will search for line containing property support or field
538: // if( changeType != null ){
539: // if( (first = methodBody.indexOf(changeType)) == -1 )
540: // return null;
541: // }
542: // else{
543: // String oldVarLine = " old" + Pattern.capitalizeFirstLetter( name ) + " = " + propertyStyle + name; // NOI18N
544: // if( (first = methodBody.indexOf( (oldType.getName() + oldVarLine + ";") )) == -1 ) { //non indexed // NOI18N
545: // if( (first = methodBody.indexOf( (oldType.getName() + oldVarLine + "[index];") )) == -1 ) { //indexed // NOI18N
546: // if( (first = methodBody.indexOf( (oldType.getName() + "[]" + oldVarLine + ";") )) == -1 ) { //indexed // NOI18N
547: // return null;
548: // }
549: // else
550: // pre_index = true;
551: // }
552: // else
553: // post_index = true;
554: // }
555: // }
556: //
557: // if( first == -1 )
558: // return null;
559: //
560: // //find end of statement
561: // int last = methodBody.indexOf(';', first);
562: // if( first >= last )
563: // return null;
564: //
565: // StringBuffer newBody = new StringBuffer(100);
566: // if( changeType != null ){
567: // newBody.append( changeType + " (\"").append( name ).append( "\", " ); // NOI18N
568: //
569: // if ( type instanceof PrimitiveType ) {
570: // newBody.append( "new ").append( BeanPatternGenerator.getWrapperClassName( type )).append( " (" ); // NOI18N
571: // newBody.append( "old" ).append( Pattern.capitalizeFirstLetter( name ) ); // NOI18N
572: // newBody.append( "), " ); // NOI18N
573: // newBody.append( "new ").append( BeanPatternGenerator.getWrapperClassName( type )).append( " (" ); // NOI18N
574: // newBody.append( name ).append( "))" ); // NOI18N
575: // }
576: // else {
577: // newBody.append( "old" ).append( Pattern.capitalizeFirstLetter( name ) ); // NOI18N
578: // newBody.append( ", " ).append( name ).append( ")" ); // NOI18N
579: // }
580: // }
581: // else{
582: // newBody.append( type.getName() );
583: // //if( pre_index ){
584: // // newBody.append( "[]" );
585: // //}
586: // newBody.append( " old" ).append( Pattern.capitalizeFirstLetter( name ) ); // NOI18N
587: // newBody.append( " = " ).append( propertyStyle ).append( name ); // NOI18N
588: // if( post_index ){
589: // newBody.append( "[index]" ); // NOI18N
590: // }
591: // }
592: //
593: // StringBuffer sb = new StringBuffer(methodBody);
594: // sb.delete(first, last);
595: // sb.insert(first, newBody);
596: // return sb.toString();
597: // }
598: //
599: // private static Type getPrimitiveType(Array atype) throws JmiException {
600: // assert JMIUtils.isInsideTrans();
601: // Type type = atype.getType();
602: // while (type instanceof Array) {
603: // type = ((Array) type).getType();
604: // }
605: // return type;
606: // }
607: //
608: // /** Gets the cookie of the first available method
609: // * @param cookieType Class of the Cookie
610: // * @return Cookie of Getter or Setter MethodElement
611: // */
612: // public Node.Cookie getCookie( Class cookieType ) {
613: // return super.getCookie(cookieType);
614: // }
615: //
616: //
617: //
618: // /** Sets the estimated field
619: // * @param field Field for the property
620: // */
621: // void setEstimatedField( Field field ) {
622: // estimatedField = field;
623: // }
624: //
625: // /** Destroys methods associated methods with the pattern in source
626: // */
627: // public void destroy() throws JmiException {
628: // assert JMIUtils.isInsideTrans();
629: // if (estimatedField != null) {
630: // Format fmt = SourceNodes.createElementFormat("{m} {t} {n}"); // NOI18N
631: // String mssg = MessageFormat.format(PatternNode.getString("FMT_DeleteField"),
632: // new Object[]{fmt.format(estimatedField)});
633: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation(mssg, NotifyDescriptor.YES_NO_OPTION);
634: // if (DialogDisplayer.getDefault().notify(nd).equals(NotifyDescriptor.YES_OPTION)) {
635: // deleteEstimatedField();
636: // }
637: // }
638: //
639: // deleteGetterMethod();
640: // deleteSetterMethod();
641: // }
642: //
643: //
644: // // METHODS FOR GENERATING AND DELETING METHODS AND FIELDS--------------------
645: //
646: // /** Generates getter method without body and without Javadoc comment.
647: // * @throws GenerateBeanException if modification of source code is impossible.
648: // */
649: // void generateGetterMethod() throws GenerateBeanException, JmiException {
650: // generateGetterMethod( null, false );
651: // }
652: //
653: // /** Generates getter method with body and optionaly with Javadoc comment.
654: // * @param body Body of the method
655: // * @param javadoc Generate Javadoc comment?
656: // * @throws GenerateBeanException if modification of source code is impossible.
657: // */
658: // void generateGetterMethod( String body, boolean javadoc ) throws GenerateBeanException, JmiException {
659: // assert JMIUtils.isInsideTrans();
660: // JavaClass declaringClass = getDeclaringClass();
661: // JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(declaringClass);
662: // Method newGetter = jmodel.getMethod().createMethod();
663: //
664: // String namePrefix = JMIUtils.isPrimitiveType(type, PrimitiveTypeKindEnum.BOOLEAN) ? "is" : "get"; // NOI18N
665: // newGetter.setName( namePrefix + capitalizeFirstLetter( getName() ) );
666: // newGetter.setType( type );
667: // newGetter.setModifiers( Modifier.PUBLIC );
668: //
669: // if ( declaringClass.isInterface() ) {
670: // newGetter.setBody( null );
671: // }
672: // else if ( body != null ) {
673: // newGetter.setBodyText( body );
674: // }
675: //
676: // if ( javadoc ) {
677: // String comment = MessageFormat.format( PatternNode.getString( "COMMENT_PropertyGetter" ),
678: // new Object[] { getName() } );
679: // newGetter.setJavadocText( comment );
680: // }
681: //
682: // if ( declaringClass == null ) {
683: // //System.out.println ("nodecl - gen getter"); // NOI18N
684: // throw new GenerateBeanException();
685: // }
686: // else {
687: // declaringClass.getFeatures().add(newGetter);
688: // getterMethod = newGetter;
689: // }
690: //
691: // }
692: //
693: // /** Generates setter method without body and without Javadoc comment.
694: // * @throws GenerateBeanException If modification of source code is impossible.
695: // */
696: // void generateSetterMethod() throws GenerateBeanException, JmiException {
697: // generateSetterMethod( null, false, false );
698: // }
699: //
700: // /** Generates setter method with body and optionaly with Javadoc comment.
701: // * @param body Body of the method
702: // * @param javadoc Generate Javadoc comment?
703: // * @param constrained Is the property constrained?
704: // * @throws GenerateBeanException If modification of source code is impossible.
705: // */
706: // void generateSetterMethod( String body, boolean constrained, boolean javadoc ) throws GenerateBeanException, JmiException {
707: // assert JMIUtils.isInsideTrans();
708: // JavaClass declaringClass = getDeclaringClass();
709: // JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(declaringClass);
710: // Method newSetter = jmodel.getMethod().createMethod();
711: //
712: // newSetter.setName( "set" + capitalizeFirstLetter( getName() ) ); // NOI18N
713: // newSetter.setType( patternAnalyser.findType("void") ); // NOI18N
714: // newSetter.setModifiers( Modifier.PUBLIC );
715: // Parameter param = jmodel.getParameter().createParameter();
716: // param.setName(name);
717: // param.setType(type);
718: // newSetter.getParameters().add(param);
719: // if ( constrained ) {
720: // MultipartId exception = jmodel.getMultipartId().
721: // createMultipartId("java.beans.PropertyVetoException", null, null); // NOI18N
722: // newSetter.getExceptionNames().add(exception);
723: // }
724: //
725: // if ( declaringClass.isInterface() ) {
726: // newSetter.setBody( null );
727: // }
728: // else if ( body != null ) {
729: // newSetter.setBodyText( body );
730: // }
731: //
732: // if ( javadoc ) {
733: // String comment = MessageFormat.format( PatternNode.getString( "COMMENT_PropertySetter" ),
734: // new Object[] { getName(), name } );
735: // if ( constrained )
736: // comment = comment + PatternNode.getString( "COMMENT_Tag_ThrowsPropertyVeto" );
737: // newSetter.setJavadocText( comment );
738: // }
739: //
740: //
741: // if ( declaringClass == null ) {
742: // //System.out.println ("nodecl - gen setter"); // NOI18N
743: // throw new GenerateBeanException();
744: // }
745: // else {
746: // declaringClass.getFeatures().add(newSetter);
747: // setterMethod = newSetter;
748: // }
749: // }
750: //
751: // /** Generates fied for the property. No javadoc comment is generated.
752: // * @throws GenerateBeanException If modification of source code is impossible.
753: // */
754: // void generateField() throws GenerateBeanException, JmiException {
755: // generateField( false );
756: // }
757: //
758: // /** Generates field for the property.
759: // * @param javadoc Generate javadoc comment?
760: // * @throws GenerateBeanException If modification of source code is impossible.
761: // */
762: // void generateField( boolean javadoc ) throws GenerateBeanException, JmiException {
763: // assert JMIUtils.isInsideTrans();
764: // JavaClass declaringClass = getDeclaringClass();
765: // JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(declaringClass);
766: // Field newField = jmodel.getField().createField();
767: //
768: // String name = getName();
769: // if( PropertyActionSettings.getDefault().getPropStyle().equals(PropertyActionSettings.GENERATE_UNDERSCORED))
770: // name = PropertyActionSettings.GENERATE_UNDERSCORED + name;
771: // name = Introspector.decapitalize( name );
772: // newField.setName( name );
773: // newField.setType( type );
774: // newField.setModifiers( Modifier.PRIVATE );
775: // if ( javadoc ) {
776: // String comment = MessageFormat.format( PatternNode.getString( "COMMENT_PropertyField" ),
777: // new Object[] { name } );
778: // newField.setJavadocText( comment );
779: // }
780: // if ( declaringClass == null ) {
781: // //System.out.println ("nodecl - gen setter"); // NOI18N
782: // throw new IllegalStateException("Missing declaring class"); // NOI18N
783: // } else {
784: // String newFieldName = newField.getName();
785: // Field f = declaringClass.getField(newFieldName, false);
786: // if ( f == null ){
787: // declaringClass.getFeatures().add(newField);
788: // estimatedField = newField;
789: // } else{
790: // if( (f.getModifiers() & Modifier.STATIC) != 0 ) //static can not be accessed via property
791: // throw new GenerateBeanException();
792: // if( !f.getType().equals(newField.getType()) ) //type not equal
793: // throw new GenerateBeanException();
794: // }
795: // }
796: // }
797: //
798: // /** Deletes the estimated field in source
799: // * @throws JmiException If modification of source code is impossible.
800: // */
801: // void deleteEstimatedField() {
802: //
803: // if ( estimatedField == null )
804: // return;
805: //
806: // JavaClass declaringClass = getDeclaringClass();
807: // declaringClass.getFeatures().remove(estimatedField);
808: // estimatedField = null;
809: // }
810: //
811: //
812: // /** Deletes the setter method in source
813: // * @throws JmiException If modification of source code is impossible.
814: // */
815: // void deleteGetterMethod() {
816: //
817: // if ( getterMethod == null )
818: // return;
819: //
820: // JavaClass declaringClass = getDeclaringClass();
821: // declaringClass.getFeatures().remove(getterMethod);
822: // getterMethod = null;
823: // }
824: //
825: // /** Deletes the setter method in source
826: // * @throws JmiException If modification of source code is impossible.
827: // */
828: // void deleteSetterMethod() {
829: //
830: // if ( setterMethod == null )
831: // return;
832: //
833: // JavaClass declaringClass = getDeclaringClass();
834: // declaringClass.getFeatures().remove(setterMethod);
835: // setterMethod = null;
836: //
837: // }
838: //
839: // // UTILITY METHODS ----------------------------------------------------------
840: //
841: // /** Sets the properties to values of other property pattern. If the
842: // * properties change fires PropertyChange event.
843: // * @param src Source PropertyPattern it's properties will be copied.
844: // */
845: // void copyProperties( PropertyPattern src ) {
846: // boolean changed = !src.getType().equals( getType() ) ||
847: // !src.getName().equals( getName() ) ||
848: // !(src.getMode() == getMode()) ||
849: // !(src.getEstimatedField() == null ? estimatedField == null : src.getEstimatedField().equals( estimatedField ) );
850: //
851: // if ( src.getGetterMethod() != getterMethod )
852: // getterMethod = src.getGetterMethod();
853: // if ( src.getSetterMethod() != setterMethod )
854: // setterMethod = src.getSetterMethod();
855: // if ( src.getEstimatedField() != estimatedField )
856: // estimatedField = src.getEstimatedField();
857: //
858: // if ( changed ) {
859: // try {
860: // type = findPropertyType();
861: // }
862: // catch ( java.beans.IntrospectionException e ) {
863: // }
864: // name = findPropertyName();
865: //
866: // // XXX cannot be fired inside mdr transaction; post to dedicated thread or redesigne somehow
867: // firePropertyChange( new java.beans.PropertyChangeEvent( this, null, null, null ) );
868: // }
869: // }
870: }
|