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 JavaBeans IndexedProperty.
057: * @author Petr Hrebejk
058: */
059: public final class IdxPropertyPattern extends PropertyPattern {
060:
061: /** Getter method of this indexed property */
062: protected ElementHandle<ExecutableElement> indexedGetterMethod;
063: /** Setter method of this indexed property */
064: protected ElementHandle<ExecutableElement> indexedSetterMethod;
065: /** Holds the indexed type of the property resolved from methods. */
066: protected TypeMirrorHandle<TypeMirror> indexedType;
067:
068: protected String indexedTypeName;
069:
070: public IdxPropertyPattern(PatternAnalyser analyser,
071: ExecutableElement getterMethod,
072: ExecutableElement setterMethod,
073: ExecutableElement indexedGetterMethod,
074: ExecutableElement indexedSetterMethod,
075: VariableElement estimatedField, TypeMirror type,
076: TypeMirror indexedType, String name)
077: throws IntrospectionException {
078: super (analyser, getterMethod, setterMethod, estimatedField,
079: type, name);
080:
081: this .indexedGetterMethod = indexedGetterMethod == null ? null
082: : ElementHandle.create(indexedGetterMethod);
083: this .indexedSetterMethod = indexedSetterMethod == null ? null
084: : ElementHandle.create(indexedSetterMethod);
085: this .indexedType = TypeMirrorHandle.create(indexedType);
086: this .indexedTypeName = typeAsString(indexedType);
087: }
088:
089: @Override
090: public Image getIcon() {
091: switch (getMode()) {
092: case READ_ONLY:
093: return IDX_PROPERTY_READ;
094: case WRITE_ONLY:
095: return IDX_PROPERTY_WRITE;
096: case READ_WRITE:
097: return IDX_PROPERTY_READ_WRITE;
098: }
099: return null;
100: }
101:
102: @Override
103: public String getHtmlDisplayName() {
104: return name + " : <font color=" + TYPE_COLOR + "> "
105: + (typeName == null ? "" : typeName)
106: + (typeName == null && indexedType != null ? "" : " ,")
107: + (indexedType == null ? "" : indexedTypeName)
108: + "</font>"; // NOI18N
109: }
110:
111: /** Returns the indexed getter method
112: * @return Getter method of the property
113: */
114: public ElementHandle<ExecutableElement> getIndexedGetterMethod() {
115: return indexedGetterMethod;
116: }
117:
118: /** Returns the indexed setter method
119: * @return Getter method of the property
120: */
121: public ElementHandle<ExecutableElement> getIndexedSetterMethod() {
122: return indexedSetterMethod;
123: }
124:
125: /** Gets the name of IdxPropertyPattern
126: * @return Name of the Indexed Property
127: */
128: public TypeMirrorHandle<TypeMirror> getIndexedType() {
129: return indexedType;
130: }
131:
132: /** Returns the mode of the property {@link PropertyPattern#READ_WRITE READ_WRITE},
133: * {@link PropertyPattern#READ_ONLY READ_ONLY} or {@link PropertyPattern#WRITE_ONLY WRITE_ONLY}
134: * @return Mode of the property
135: */
136: public int getMode() {
137: if (indexedSetterMethod != null && indexedGetterMethod != null)
138: return READ_WRITE;
139: else if (indexedGetterMethod != null
140: && indexedSetterMethod == null)
141: return READ_ONLY;
142: else if (indexedSetterMethod != null
143: && indexedGetterMethod == null)
144: return WRITE_ONLY;
145: else
146: return super .getMode();
147: }
148:
149: /** Sets the name of IdxPropertyPattern
150: * @param name New name of the property.
151: * @throws JmiException If the modification of source code is impossible.
152: */
153: public void setName(String name) throws IllegalArgumentException {
154: throw new UnsupportedOperationException();
155: // String oldName = this.name;
156: // super.setName( name );
157: //
158: // name = capitalizeFirstLetter( name );
159: //
160: // if ( indexedGetterMethod != null ) {
161: // String idxGetterMethodID = ( indexedGetterMethod.getName().startsWith("get") ? // NOI18N
162: // "get" : "is" ) + name ; // NOI18N
163: // indexedGetterMethod.setName( idxGetterMethodID );
164: // String oldGetterComment = MessageFormat.format( PatternNode.getString( "COMMENT_IdxPropertyGetter" ),
165: // new Object[] { oldName } );
166: // String newGetterComment = MessageFormat.format( PatternNode.getString( "COMMENT_IdxPropertyGetter" ),
167: // new Object[] { getName() } );
168: // String indexedGetterJavadoc = indexedGetterMethod.getJavadocText();
169: // if (indexedGetterJavadoc != null &&
170: // oldGetterComment.trim().equals(indexedGetterJavadoc.trim())) {
171: // indexedGetterMethod.setJavadocText( newGetterComment );
172: // }
173: // }
174: // if ( indexedSetterMethod != null ) {
175: // String idxSetterMethodID = "set" + name; // NOI18N
176: // indexedSetterMethod.setName( idxSetterMethodID );
177: // String oldSetterComment = MessageFormat.format( PatternNode.getString( "COMMENT_IdxPropertySetter" ),
178: // new Object[] { oldName, oldName } );
179: // String newSetterComment = MessageFormat.format( PatternNode.getString( "COMMENT_IdxPropertySetter" ),
180: // new Object[] { getName(), getName() } );
181: // String indexedSetterJavadoc = indexedSetterMethod.getJavadocText();
182: // if (indexedSetterJavadoc != null &&
183: // oldSetterComment.trim().equals(indexedSetterJavadoc.trim())) {
184: // indexedSetterMethod.setJavadocText( newSetterComment );
185: // }
186: // }
187: //
188: // // change body and javadoc of idx accessors if the field has been changed
189: // if ( estimatedField != null && estimatedField.getName().equals(getName())) {
190: // int mode = getMode();
191: // if ( mode == READ_WRITE || mode == READ_ONLY ) {
192: // String existingGetterBody = indexedGetterMethod.getBodyText().trim();
193: // String oldGetterBody1 = BeanPatternGenerator.idxPropertyGetterBody( oldName, true, true ).trim();
194: // String oldGetterBody2 = BeanPatternGenerator.idxPropertyGetterBody( oldName, true, false ).trim();
195: // if (existingGetterBody.equals(oldGetterBody1)) {
196: // indexedGetterMethod.setBodyText(BeanPatternGenerator.idxPropertyGetterBody( getName(), true, true));
197: // } else if (existingGetterBody.equals(oldGetterBody2)) {
198: // indexedGetterMethod.setBodyText(BeanPatternGenerator.idxPropertyGetterBody( getName(), true, false));
199: // }
200: // }
201: // if ( mode == READ_WRITE || mode == WRITE_ONLY ) {
202: // String existingSetterBody = indexedSetterMethod.getBodyText().trim();
203: // String oldSetterBody = BeanPatternGenerator.idxPropertySetterBody (oldName, this.type, false, false, true, false, null, null).trim();
204: // if (existingSetterBody.equals(oldSetterBody)) {
205: // indexedSetterMethod.setBodyText(BeanPatternGenerator.idxPropertySetterBody (getName(), getType(), false, false, true, false, null, null));
206: //
207: // if ( indexedSetterMethod != null ) {
208: // List/*<Parameter>*/ params = indexedSetterMethod.getParameters();
209: // Parameter param = (Parameter) params.get(1);
210: // param.setName(Introspector.decapitalize( name ));
211: // }
212: // }
213: // }
214: // }
215: }
216:
217: // /** Creates new indexed property pattern with extended options
218: // * @param patternAnalyser patternAnalyser which creates this Property.
219: // * @param name Name of the Property.
220: // * @param type Type of the Property.
221: // * @param mode {@link #READ_WRITE Mode} of the new property.
222: // * @param bound Is the Property bound?
223: // * @param constrained Is the property constrained?
224: // * @param withField Should be the private field for this property genareted?
225: // * @param withReturn Generate return statement in getter?
226: // * @param withSet Generate seter statement for private field in setter.
227: // * @param withSupport Generate PropertyChange support?
228: // * @param niGetter Non-indexed getter method
229: // * @param niWithReturn Generate return statement in non-indexed getter?
230: // * @param niSetter Non-indexed setter method
231: // * @param niWithSet Generate set field statement in non-indexed setter?
232: // * @throws JmiException If the Property can't be created in the source.
233: // * @return Newly created PropertyPattern.
234: // */
235: // static IdxPropertyPattern create( PatternAnalyser patternAnalyser,
236: // String name, String type,
237: // int mode, boolean bound, boolean constrained,
238: // boolean withField, boolean withReturn,
239: // boolean withSet, boolean withSupport,
240: // boolean niGetter, boolean niWithReturn,
241: // boolean niSetter, boolean niWithSet ) throws JmiException, GenerateBeanException {
242: //
243: // return create(patternAnalyser, name, type, mode, bound, constrained, withField, withReturn, withSet, withSupport, niGetter, niWithReturn, niSetter, niWithSet, false, false );
244: // }
245: // /** Creates new indexed property pattern with extended options
246: // * @param patternAnalyser patternAnalyser which creates this Property.
247: // * @param name Name of the Property.
248: // * @param type Type of the Property.
249: // * @param mode {@link #READ_WRITE Mode} of the new property.
250: // * @param bound Is the Property bound?
251: // * @param constrained Is the property constrained?
252: // * @param withField Should be the private field for this property genareted?
253: // * @param withReturn Generate return statement in getter?
254: // * @param withSet Generate seter statement for private field in setter.
255: // * @param withSupport Generate PropertyChange support?
256: // * @param niGetter Non-indexed getter method
257: // * @param niWithReturn Generate return statement in non-indexed getter?
258: // * @param niSetter Non-indexed setter method
259: // * @param niWithSet Generate set field statement in non-indexed setter?
260: // * @param useSupport use change support without prompting
261: // * @param fromField signalize that all action are activatet on field
262: // * @throws JmiException If the Property can't be created in the source.
263: // * @return Newly created PropertyPattern.
264: // */
265: // static IdxPropertyPattern create( PatternAnalyser patternAnalyser,
266: // String name, String type,
267: // int mode, boolean bound, boolean constrained,
268: // boolean withField, boolean withReturn,
269: // boolean withSet, boolean withSupport,
270: // boolean niGetter, boolean niWithReturn,
271: // boolean niSetter, boolean niWithSet,
272: // boolean useSupport, boolean fromField ) throws JmiException, GenerateBeanException {
273: //
274: // assert JMIUtils.isInsideTrans();
275: // IdxPropertyPattern ipp = new IdxPropertyPattern( patternAnalyser );
276: //
277: // ipp.name = name;
278: // ipp.type = null;
279: // ipp.indexedType = patternAnalyser.findType(type);
280: //
281: // // Set the non-indexed type when needed
282: // if ( withField || withSupport || niGetter || niSetter ) {
283: // JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(ipp.indexedType);
284: // ipp.type = jmodel.getArray().resolveArray(ipp.indexedType);
285: // }
286: //
287: // // Generate field
288: // if ( ( withField || withSupport ) && !fromField ) {
289: // if ( ipp.type != null ) {
290: // try {
291: // ipp.generateField( true );
292: // } catch (GenerateBeanException e) {
293: // DialogDisplayer.getDefault().notify(
294: // new NotifyDescriptor.Message(
295: // PatternNode.getString("MSG_Cannot_Create_Field"),
296: // NotifyDescriptor.WARNING_MESSAGE));
297: // }
298: // }
299: // }
300: //
301: //
302: // // Ensure property change support field and methods exist
303: // String supportName = null;
304: // String vetoSupportName = null;
305: //
306: // if ( withSupport ) {
307: //
308: // boolean boundSupport = bound;
309: // boolean constrainedSupport = constrained;
310: //
311: //
312: // if( !useSupport ){
313: // if( boundSupport )
314: // if( ( supportName = EventSetInheritanceAnalyser.showInheritanceEventDialog(EventSetInheritanceAnalyser.detectPropertyChangeSupport( ipp.getDeclaringClass()), "PropertyChangeSupport")) != null ) // NOI18N
315: // boundSupport = false;
316: // if( constrainedSupport )
317: // if( ( vetoSupportName = EventSetInheritanceAnalyser.showInheritanceEventDialog(EventSetInheritanceAnalyser.detectVetoableChangeSupport( ipp.getDeclaringClass()), "VetoableChangeSupport")) != null ) // NOI18N
318: // constrainedSupport = false;
319: // }
320: // else {
321: // if( boundSupport )
322: // if( ( supportName = EventSetInheritanceAnalyser.getInheritanceEventSupportName(EventSetInheritanceAnalyser.detectPropertyChangeSupport( ipp.getDeclaringClass()), "PropertyChangeSupport")) != null ) // NOI18N
323: // boundSupport = false;
324: // if( constrainedSupport )
325: // if( ( vetoSupportName = EventSetInheritanceAnalyser.getInheritanceEventSupportName(EventSetInheritanceAnalyser.detectVetoableChangeSupport( ipp.getDeclaringClass()), "VetoableChangeSupport")) != null ) // NOI18N
326: // constrainedSupport = false;
327: // }
328: //
329: // if ( boundSupport )
330: // supportName = BeanPatternGenerator.supportField( ipp.getDeclaringClass() );
331: // if ( constrainedSupport )
332: // vetoSupportName = BeanPatternGenerator.vetoSupportField( ipp.getDeclaringClass() );
333: //
334: // if ( boundSupport )
335: // BeanPatternGenerator.supportListenerMethods( ipp.getDeclaringClass(), supportName );
336: // if ( constrainedSupport )
337: // BeanPatternGenerator.vetoSupportListenerMethods( ipp.getDeclaringClass(), vetoSupportName );
338: // }
339: //
340: // if ( mode == READ_WRITE || mode == READ_ONLY ) {
341: // if( (fromField && withReturn) || !fromField )
342: // ipp.generateIndexedGetterMethod( BeanPatternGenerator.idxPropertyGetterBody( name, withReturn ), true );
343: // if ( ipp.type != null && niGetter )
344: // ipp.generateGetterMethod( BeanPatternGenerator.propertyGetterBody( name, niWithReturn), true );
345: // }
346: // if ( mode == READ_WRITE || mode == WRITE_ONLY ) {
347: // /*
348: // ipp.generateIndexedSetterMethod( BeanPatternGenerator.idxPropertySetterBody( name, ipp.getType(),
349: // bound, constrained, withSet, withSupport, supportName, vetoSupportName ), constrained, true );
350: // */
351: // if( (fromField && withSet) || !fromField )
352: // ipp.generateIndexedSetterMethod( BeanPatternGenerator.idxPropertySetterBody( name, ipp.getIndexedType(),
353: // bound, constrained, withSet, withSupport, supportName, vetoSupportName ), constrained, true );
354: //
355: // if ( ipp.type != null && niSetter )
356: // ipp.generateSetterMethod( BeanPatternGenerator.propertySetterBody( name, ipp.getType(),
357: // bound, constrained, niWithSet, withSupport, supportName, vetoSupportName ), constrained, true );
358: // }
359: // return ipp;
360: // }
361: //
362: //
363: //
364: //
365: //
366: //
367: //
368: //
369: //
370: // /** Sets the non-indexed type of IdxPropertyPattern
371: // * @param type New non-indexed type of the indexed property
372: // * @throws JmiException If the modification of source code is impossible
373: // */
374: // public void setType(Type type) throws JmiException {
375: // JMIUtils.beginTrans(true);
376: // boolean rollback = true;
377: // try {
378: // setTypeImpl(type);
379: // rollback = false;
380: // } finally {
381: // JMIUtils.endTrans(rollback);
382: // }
383: // }
384: //
385: // private void setTypeImpl(Type type) throws JmiException {
386: // assert JMIUtils.isInsideTrans();
387: // if ( this.type != null && this.type.equals( type ) )
388: // return;
389: //
390: // // Remember the old type & old indexed type
391: // Type oldIndexedType = this.indexedType;
392: // Type oldType = this.type;
393: //
394: // if ( oldType == null ) {
395: // this.type = type;
396: // oldType = type;
397: // int mode = getMode();
398: // if ( mode == READ_WRITE || mode == READ_ONLY ) {
399: // try {
400: // generateGetterMethod();
401: // } catch (GenerateBeanException e) {
402: // ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
403: // }
404: // }
405: // if ( mode == READ_WRITE || mode == WRITE_ONLY ) {
406: // try {
407: // generateSetterMethod();
408: // } catch (GenerateBeanException e) {
409: // ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
410: // }
411: // }
412: // }
413: // else
414: // // Change the type
415: // super.setType( type );
416: //
417: // // Test if the idexedType is the type of array and change it if so
418: // if ( type instanceof Array && oldType instanceof Array && oldIndexedType.equals(((Array) oldType).getType()) ) {
419: // Type newType = ((Array) type).getType();
420: //
421: // // Set the type to new type
422: // setIndexedTypeImpl( newType, false );
423: // }
424: // }
425: //
426: // /** Sets the indexed type of IdxPropertyPattern
427: // * @param type New indexed type of the indexed property
428: // * @throws JmiException If the modification of source code is impossible
429: // */
430: // public void setIndexedType(Type type) throws JmiException {
431: // JMIUtils.beginTrans(true);
432: // boolean rollback = true;
433: // try {
434: // setIndexedTypeImpl(type, true);
435: // rollback = false;
436: // } finally {
437: // JMIUtils.endTrans(rollback);
438: // }
439: // }
440: //
441: // private void setIndexedTypeImpl(Type type, boolean changeType) throws JmiException {
442: // assert JMIUtils.isInsideTrans();
443: // if ( this.indexedType.equals( type ) )
444: // return;
445: //
446: // // Remember the old type
447: // Type oldType = this.type;
448: //
449: // // Change the indexed type
450: // if (indexedGetterMethod != null ) {
451: // indexedGetterMethod.setType( type );
452: // }
453: // if (indexedSetterMethod != null ) {
454: // List/*<Parameter>*/ params = indexedSetterMethod.getParameters();
455: // if ( params.size() > 1 ) {
456: // Parameter param = (Parameter) params.get(1);
457: // param.setType( type );
458: //
459: // String body = indexedSetterMethod.getBodyText();
460: //
461: // //test if body contains change support
462: // if( body != null && ( body.indexOf(PropertyPattern.PROPERTY_CHANGE) != -1 || body.indexOf(PropertyPattern.VETOABLE_CHANGE) != -1 ) ) {
463: // String mssg = MessageFormat.format( PatternNode.getString( "FMT_ChangeMethodBody" ),
464: // new Object[] { setterMethod.getName() } );
465: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation ( mssg, NotifyDescriptor.YES_NO_OPTION );
466: // DialogDisplayer.getDefault().notify( nd );
467: // if( nd.getValue().equals( NotifyDescriptor.YES_OPTION ) ) {
468: // String newBody = regeneratePropertySupport( indexedSetterMethod.getBodyText(), null, param.getName(), type, oldType );
469: // if( newBody != null )
470: // indexedSetterMethod.setBodyText(newBody);
471: // }
472: // }
473: // }
474: // }
475: //
476: // // Test if the old type of getter and seter was an array of indexedType
477: // // if so change the type of that array.
478: // if (changeType) {
479: // JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(type);
480: // Type newArrayType = jmodel.getArray().resolveArray(type);
481: // super.setType( newArrayType );
482: // }
483: //
484: // indexedType = type;
485: // }
486: //
487: //
488: //
489: // /** Sets the property to be writable
490: // * @param mode New Mode {@link PropertyPattern#READ_WRITE READ_WRITE},
491: // * {@link PropertyPattern#READ_ONLY READ_ONLY} or {@link PropertyPattern#WRITE_ONLY WRITE_ONLY}
492: // * @throws GenerateBeanException If the modification of source code is impossible.
493: // */
494: // public void setMode( int mode ) throws GenerateBeanException, JmiException {
495: // if ( getMode() == mode )
496: // return;
497: //
498: // JMIUtils.beginTrans(true);
499: // boolean rollback = true;
500: // try {
501: // switch (mode) {
502: // case READ_WRITE:
503: // if (getterMethod == null)
504: // generateGetterMethod();
505: // if (setterMethod == null)
506: // generateSetterMethod();
507: // if (indexedGetterMethod == null)
508: // generateIndexedGetterMethod();
509: // if (indexedSetterMethod == null)
510: // generateIndexedSetterMethod();
511: // break;
512: // case READ_ONLY:
513: // if (getterMethod == null)
514: // generateGetterMethod();
515: // if (indexedGetterMethod == null)
516: // generateIndexedGetterMethod();
517: //
518: // if (setterMethod != null || indexedSetterMethod != null) {
519: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation(PatternNode.getString("MSG_Delete_Setters") + PatternNode.getString("MSG_Continue_Confirm"), NotifyDescriptor.YES_NO_OPTION);
520: // DialogDisplayer.getDefault().notify(nd);
521: // if (nd.getValue().equals(NotifyDescriptor.YES_OPTION)) {
522: // if (setterMethod != null)
523: // deleteSetterMethod();
524: // if (indexedSetterMethod != null)
525: // deleteIndexedSetterMethod();
526: // }
527: // }
528: // break;
529: // case WRITE_ONLY:
530: // if (setterMethod == null)
531: // generateSetterMethod();
532: // if (indexedSetterMethod == null)
533: // generateIndexedSetterMethod();
534: // if (getterMethod != null || indexedGetterMethod != null) {
535: // NotifyDescriptor nd = new NotifyDescriptor.Confirmation(PatternNode.getString("MSG_Delete_Getters") + PatternNode.getString("MSG_Continue_Confirm"), NotifyDescriptor.YES_NO_OPTION);
536: // DialogDisplayer.getDefault().notify(nd);
537: // if (nd.getValue().equals(NotifyDescriptor.YES_OPTION)) {
538: // if (getterMethod != null)
539: // deleteGetterMethod();
540: // if (indexedGetterMethod != null)
541: // deleteIndexedGetterMethod();
542: // }
543: // }
544: // break;
545: // }
546: // rollback = false;
547: // } finally {
548: // JMIUtils.endTrans(rollback);
549: // }
550: //
551: // }
552: //
553: // /** Gets the cookie of the first available inderxed method
554: // * @param cookieType Class of the Cookie
555: // * @return Cookie of indexedGetter or indexedSetter MethodElement
556: // */
557: // public Node.Cookie getCookie( Class cookieType ) {
558: // return super.getCookie(cookieType);
559: // }
560: //
561: // /** Destroys methods associated methods with the pattern in source
562: // */
563: // public void destroy() throws JmiException {
564: // assert JMIUtils.isInsideTrans();
565: // deleteIndexedSetterMethod();
566: // deleteIndexedGetterMethod();
567: // super.destroy();
568: // }
569: //
570: // // METHODS FOR GENERATING AND DELETING METHODS AND FIELDS--------------------
571: //
572: //
573: // /** Generates non-indexed getter method without body and without Javadoc comment.
574: // * @throws GenerateBeanException if modification of source code is impossible.
575: // */
576: // void generateGetterMethod() throws GenerateBeanException, JmiException {
577: // if ( type != null )
578: // super.generateGetterMethod();
579: // }
580: //
581: // /** Generates non-indexed setter method without body and without Javadoc comment.
582: // * @throws GenerateBeanException If modification of source code is impossible.
583: // */
584: // void generateSetterMethod() throws GenerateBeanException, JmiException {
585: // if ( type != null )
586: // super.generateSetterMethod();
587: // }
588: //
589: // /** Generates indexed getter method without body and without Javadoc comment.
590: // * @throws GenerateBeanException If modification of source code is impossible.
591: // */
592: // void generateIndexedGetterMethod() throws GenerateBeanException, JmiException {
593: // generateIndexedGetterMethod( null, false );
594: // }
595: //
596: // /** Generates indexed getter method with body and optionaly with Javadoc comment.
597: // * @param body Body of the method
598: // * @param javadoc Generate Javadoc comment?
599: // * @throws GenerateBeanException If modification of source code is impossible.
600: // */
601: // void generateIndexedGetterMethod( String body, boolean javadoc ) throws GenerateBeanException, JmiException {
602: // assert JMIUtils.isInsideTrans();
603: // JavaClass declaringClass = getDeclaringClass();
604: // JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(declaringClass);
605: // Method newGetter = jmodel.getMethod().createMethod();
606: // Parameter newParameter = jmodel.getParameter().createParameter();
607: // newParameter.setName("index"); // NOI18N
608: // newParameter.setType(jmodel.getType().resolve("int")); // NOI18N
609: //
610: // newGetter.setName( "get" + capitalizeFirstLetter( getName() ) ); // NOI18N
611: // newGetter.setType( indexedType );
612: // newGetter.setModifiers( Modifier.PUBLIC );
613: // newGetter.getParameters().add(newParameter);
614: // if ( declaringClass.isInterface() ) {
615: // newGetter.setBodyText( null );
616: // }
617: // else if ( body != null )
618: // newGetter.setBodyText( body );
619: //
620: // if ( javadoc ) {
621: // String comment = MessageFormat.format( PatternNode.getString( "COMMENT_IdxPropertyGetter" ),
622: // new Object[] { getName() } );
623: // newGetter.setJavadocText( comment );
624: // }
625: //
626: // //System.out.println ("Generating getter" ); // NOI18N
627: //
628: // if ( declaringClass == null )
629: // throw new GenerateBeanException();
630: // else {
631: // //System.out.println ( "Adding getter method" ); // NOI18N
632: // declaringClass.getFeatures().add( newGetter );
633: // indexedGetterMethod = newGetter;
634: // }
635: // }
636: //
637: // /** Generates indexed setter method without body and without Javadoc comment.
638: // * @throws GenerateBeanException If modification of source code is impossible.
639: // */
640: // void generateIndexedSetterMethod() throws GenerateBeanException, JmiException {
641: // generateIndexedSetterMethod(null, false, false );
642: // }
643: //
644: // /** Generates indexed setter method with body and optionaly with Javadoc comment.
645: // * @param body Body of the method
646: // * @param javadoc Generate Javadoc comment?
647: // * @param constrained Is the property constrained?
648: // * @throws GenerateBeanException If modification of source code is impossible.
649: // */
650: // void generateIndexedSetterMethod( String body, boolean constrained, boolean javadoc ) throws GenerateBeanException, JmiException {
651: // assert JMIUtils.isInsideTrans();
652: // JavaClass declaringClass = getDeclaringClass();
653: // JavaModelPackage jmodel = JavaMetamodel.getManager().getJavaExtent(declaringClass);
654: // Method newSetter = jmodel.getMethod().createMethod();
655: // Parameter newParamIndex = jmodel.getParameter().createParameter();
656: // newParamIndex.setName("index"); // NOI18N
657: // newParamIndex.setType(jmodel.getType().resolve("int")); // NOI18N
658: // Parameter newParamValue = jmodel.getParameter().createParameter();
659: // newParamValue.setName(name);
660: // newParamValue.setType(indexedType);
661: //
662: // newSetter.setName( "set" + capitalizeFirstLetter( getName() ) ); // NOI18N
663: // newSetter.setType(jmodel.getType().resolve("void")); // NOI18N
664: // newSetter.setModifiers( Modifier.PUBLIC );
665: // List/*<Parameter>*/ params = newSetter.getParameters();
666: // params.add(newParamIndex);
667: // params.add(newParamValue);
668: //
669: // if ( constrained ) {
670: // MultipartId propVetoEx = jmodel.getMultipartId().
671: // createMultipartId("java.beans.PropertyVetoException", null, null); // NOI18N
672: // if (propVetoEx == null)
673: // throw new GenerateBeanException("cannot resolve java.beans.PropertyVetoException"); // NOI18N
674: // newSetter.getExceptionNames().add(propVetoEx);
675: // }
676: // if ( declaringClass.isInterface() ) {
677: // newSetter.setBodyText( null );
678: // }
679: // else if ( body != null ) {
680: // newSetter.setBodyText( body );
681: // }
682: //
683: // if ( javadoc ) {
684: // String comment = MessageFormat.format( PatternNode.getString( "COMMENT_IdxPropertySetter" ),
685: // new Object[] { getName(), name } );
686: // if ( constrained )
687: // comment = comment + PatternNode.getString( "COMMENT_Tag_ThrowsPropertyVeto" );
688: // newSetter.setJavadocText( comment );
689: // }
690: //
691: // if ( declaringClass == null )
692: // throw new GenerateBeanException();
693: // else {
694: // declaringClass.getFeatures().add(newSetter);
695: // indexedSetterMethod = newSetter;
696: // }
697: // }
698: //
699: //
700: // /** Deletes the indexed getter method in source
701: // * @throws JmiException If modification of source code is impossible.
702: // */
703: // void deleteIndexedGetterMethod() throws JmiException {
704: // assert JMIUtils.isInsideTrans();
705: // if ( indexedGetterMethod == null )
706: // return;
707: //
708: // JavaClass declaringClass = getDeclaringClass();
709: // declaringClass.getFeatures().remove( indexedGetterMethod );
710: // indexedGetterMethod = null;
711: // }
712: //
713: // /** Deletes the indexed setter method in source
714: // * @throws JmiException If modification of source code is impossible.
715: // */
716: // void deleteIndexedSetterMethod() throws JmiException {
717: // assert JMIUtils.isInsideTrans();
718: // if ( indexedSetterMethod == null )
719: // return;
720: //
721: // JavaClass declaringClass = getDeclaringClass();
722: // declaringClass.getFeatures().remove( indexedSetterMethod );
723: // indexedSetterMethod = null;
724: // }
725: //
726: // // Property change support ----------------------------------
727: //
728: // /** Sets the properties to values of other indexed property pattern. If the
729: // * properties change fires PropertyChange event.
730: // * @param src Source IdxPropertyPattern it's properties will be copied.
731: // */
732: // void copyProperties( IdxPropertyPattern src ) throws JmiException {
733: // assert JMIUtils.isInsideTrans();
734: // boolean changed = !src.getIndexedType().equals( getIndexedType() ) ||
735: // !( src.getType() == null ? getType() == null : src.getType().equals( getType() ) ) ||
736: // !src.getName().equals( getName() ) ||
737: // !(src.getMode() == getMode()) ||
738: // !(src.getEstimatedField() == null ? estimatedField == null : src.getEstimatedField().equals( estimatedField ) );
739: //
740: // if ( src.getIndexedGetterMethod() != indexedGetterMethod )
741: // indexedGetterMethod = src.getIndexedGetterMethod();
742: // if ( src.getIndexedSetterMethod() != indexedSetterMethod )
743: // indexedSetterMethod = src.getIndexedSetterMethod();
744: //
745: // if ( src.getGetterMethod() != getterMethod ) {
746: // changed = true;
747: // getterMethod = src.getGetterMethod();
748: // }
749: // if ( src.getSetterMethod() != setterMethod ) {
750: // changed = true;
751: // setterMethod = src.getSetterMethod();
752: // }
753: // if ( src.getEstimatedField() != estimatedField )
754: // estimatedField = src.getEstimatedField();
755: //
756: // if ( changed ) {
757: // try {
758: // type = findPropertyType();
759: // findIndexedPropertyType();
760: // }
761: // catch ( java.beans.IntrospectionException e ) {
762: // // User's error
763: // }
764: // name = findIndexedPropertyName();
765: //
766: // // XXX cannot be fired inside mdr transaction; post to dedicated thread or redesigne somehow
767: // firePropertyChange( new java.beans.PropertyChangeEvent( this, null, null, null ) );
768: // }
769: // }
770:
771: }
|