KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > impl > ClassAction


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.impl;
26
27 //import java.io.*;
28

29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.ArrayList JavaDoc;
35
36 import com.sun.jdo.api.persistence.enhancer.classfile.*;
37
38 import com.sun.jdo.api.persistence.enhancer.util.Support;
39
40 //@olsen: added import
41
import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaData;
42
43
44 //@olsen: cosmetics
45
//@olsen: moved: this class -> package impl
46
//@olsen: subst: (object)state -> flags
47
//@olsen: subst: JDOFlags -> jdoFlags
48
//@olsen: subst: makeJDO[gs]etFlags -> makeJDO[GS]etFlags
49
//@olsen: subst: JDO[gs]etFlags -> jdo[GS]etFlags
50
//@olsen: subst: [Nn]eedsJDORefMethods -> [Nn]eedsJDOStateManagerMethods
51
//@olsen: subst: JDORef -> jdoStateManager
52
//@olsen: subst: makeJDO[gs]etRef -> makeJDO[GS]etStateManager
53
//@olsen: subst: JDO[gs]etRef -> jdo[GS]etStateManager
54
//@olsen: subst: [iI]Persistent -> [pP]ersistenceCapable
55
//@olsen: subst: PersistentAux -> StateManager
56
//@olsen: subst: jdo/ -> com/sun/forte4j/persistence/internal/
57
//@olsen: subst: jdo. -> com.sun.forte4j.persistence.internal.
58
//@olsen: subst: /* ... */ -> // ...
59
//@olsen: subst: filterEnv -> env
60
//@olsen: subst: FilterEnv -> Environment
61
//@olsen: made MethodBuilder's methods non-static
62
//@olsen: dropped parameter 'Environment env', use association instead
63
//@olsen: subst: Hashtable -> Map, HashMap
64
//@olsen: subst: Vector -> Collection, List, ArrayList
65
//@olsen: subst: Enumeration,... -> Iterator, hasNext(), next()
66
//@olsen: subst: absolut jdo types and names -> constants from JDOMetaData
67
//@olsen: subst: refMember, flagsMember -> JDOStateManagerName, JDOFlagsName
68
//@olsen: added: support for I18N
69
//@olsen: subst: FilterError -> UserException, affirm()
70
//@olsen: removed: proprietary support for HashCode
71
//@olsen: removed: proprietary support for TypeSummary
72
//@olsen: removed: proprietary support for ClassInfo
73
//@olsen: removed: proprietary support for {set,get}{Ref,Flags}{Fields,Methods}
74
//@olsen: simplified detection/generation of jdo[GS]etStateManager() methods
75
//@olsen: simplified detection/generation of jdo[GS]etFlags() methods
76
//@olsen: removed: old, disabled ODI code
77

78
79 /**
80  * ClassAction handles the persistence annotation actions for a class.
81  * The details specific to individual methods and fields are delegated
82  * to instances of FieldAction and MethodAction.
83  */

84 final class ClassAction
85     extends Support
86     implements VMConstants {
87
88     /* bit mask constants indicating what needs to be generated */
89 //@olsen: disabled feature
90
/*
91     private static final int GENInitContents = 0x02;
92     private static final int GENFlushContents = 0x04;
93     private static final int GENClearContents = 0x08;
94
95     private static final int GENAllContents =
96     GENInitContents | GENFlushContents | GENClearContents;
97 */

98
99     /* Constants for the class level annotation attribute */
100     private static final String JavaDoc AnnotatedAttribute = "com.sun.jdo.api.persistence.enhancer.annotated";//NOI18N
101
private static final short AnnotatedVersion = 1;
102
103     //@olsen: added method name constants
104
static private final String JavaDoc jdoGetStateManagerName
105     = "jdoGetStateManager";//NOI18N
106
static private final String JavaDoc jdoSetStateManagerName
107     = "jdoSetStateManager";//NOI18N
108
static private final String JavaDoc jdoGetFlagsName
109     = "jdoGetFlags";//NOI18N
110
static private final String JavaDoc jdoSetFlagsName
111     = "jdoSetFlags";//NOI18N
112
static private final String JavaDoc jdoMakeDirtyName
113     = "jdoMakeDirty";//NOI18N
114
static private final String JavaDoc jdoIsDirtyName
115     = "jdoIsDirty";//NOI18N
116
static private final String JavaDoc jdoIsTransactionalName
117     = "jdoIsTransactional";//NOI18N
118
static private final String JavaDoc jdoIsPersistentName
119     = "jdoIsPersistent";//NOI18N
120
static private final String JavaDoc jdoIsNewName
121     = "jdoIsNew";//NOI18N
122
static private final String JavaDoc jdoIsDeletedName
123     = "jdoIsDeleted";//NOI18N
124
static private final String JavaDoc jdoGetPersistenceManagerName
125     = "jdoGetPersistenceManager";//NOI18N
126
static private final String JavaDoc jdoGetObjectIdName
127     = "jdoGetObjectId";//NOI18N
128
static private final String JavaDoc jdoConstructorName
129     = "<init>";//NOI18N
130
static private final String JavaDoc jdoNewInstanceName
131     = "jdoNewInstance";//NOI18N
132
static private final String JavaDoc jdoClearName
133     = "jdoClear";//NOI18N
134
static private final String JavaDoc jdoCopyName
135     = "jdoCopy";//NOI18N
136
static private final String JavaDoc jdoGetFieldName
137     = "jdoGetField";//NOI18N
138
static private final String JavaDoc jdoSetFieldName
139     = "jdoSetField";//NOI18N
140
static private final String JavaDoc jdoCloneName
141     = "clone";//NOI18N
142

143     /* The class to be annotated */
144     //@olsen: made final
145
private final ClassControl control;
146
147     /* Central repository for the options and classes */
148     //@olsen: added association
149
//@olsen: made final
150
private final Environment env;
151
152     /* The method builder helper object. */
153     //@olsen: made MethodBuilder's methods non-static
154
//@olsen: made final
155
private final MethodBuilder methodBuilder;
156
157     /* Hash table mapping ClassMethod to MethodAction */
158     //@olsen: made final
159
//@olsen: subst: Hashtable -> HashMap
160
private final Map JavaDoc methodActionTable = new HashMap JavaDoc(11);
161
162     /* Vector of FieldAction */
163     //@olsen: made final
164
//@olsen: subst: Vector -> ArrayList
165
private final List JavaDoc fieldActionTable = new ArrayList JavaDoc();
166
167     /* What should we generate for this class?
168      * This is a combination of the GENXXXX constants defined above. */

169 //@olsen: disabled feature
170
/*
171     private int generate;
172 */

173
174     /* True if this has already been annotated */
175     private boolean previouslyAnnotated = false;
176
177     /* If true, this class will directly implement PersistenceCapable
178        interface. */

179     private boolean implementsPersistence = false;
180
181     /* If true, this class will directly implement PersistenceCapableHooks
182        interface if no base class does so. */

183 //@olsen: disabled feature
184
/*
185     private boolean implementsPersistenceHooks = false;
186     private boolean implementsPersistenceHooksKnown = false;
187 */

188
189     /* True if a user-defined jdo member has been seen in this class. */
190     //@olsen: added fields
191
private boolean sawImplementsPersistenceCapable = false;
192     private boolean sawImplementsCloneable = false;
193     private boolean sawFieldJDOStateManager = false;
194     private boolean sawFieldJDOFlags = false;
195     private boolean sawMethodJDOGetStateManager = false;
196     private boolean sawMethodJDOSetStateManager = false;
197     private boolean sawMethodJDOGetFlags = false;
198     private boolean sawMethodJDOSetFlags = false;
199     private boolean sawMethodJDOMakeDirty = false;
200     private boolean sawMethodJDOIsDirty = false;
201     private boolean sawMethodJDOIsTransactional = false;
202     private boolean sawMethodJDOIsPersistent = false;
203     private boolean sawMethodJDOIsNew = false;
204     private boolean sawMethodJDOIsDeleted = false;
205     private boolean sawMethodJDOGetPersistenceManager = false;
206     private boolean sawMethodJDOGetObjectId = false;
207     private boolean sawMethodJDOConstructor = false;
208     private boolean sawMethodJDONewInstance = false;
209     private boolean sawMethodJDOGetField = false;
210     private boolean sawMethodJDOSetField = false;
211     private boolean sawMethodJDOClear = false;
212     private boolean sawMethodJDOCopy = false;
213     private boolean sawMethodJDOClone = false;
214
215     /* True if the preDestroyPersistent() method needs to be generated
216        for PersistenceCapable. */

217 //@olsen: disabled feature
218
/*
219     private boolean needsPreDestroyPersistent = true;
220 */

221
222     /* True if the postInitializeContents() method needs to be generated
223        for PersistenceCapable. */

224 //@olsen: disabled feature
225
/*
226     private boolean needsPostInitializeContents = true;
227 */

228
229     /* True if the preFlushContents() method needs to be generated
230        for PersistenceCapable. */

231 //@olsen: disabled feature
232
/*
233     private boolean needsPreFlushContents = true;
234 */

235
236     /* True if the preClearContents() method needs to be generated
237        for PersistenceCapable. */

238 //@olsen: disabled feature
239
/*
240     private boolean needsPreClearContents = true;
241 */

242
243
244     // public accessors
245

246     /**
247      * Constructor
248      */

249     //@olsen: added parameter 'env' for association
250
public ClassAction(ClassControl control,
251                        Environment env) {
252         this.control = control;
253         this.env = env;
254         this.methodBuilder = new MethodBuilder(env);
255     }
256
257     /**
258      * Perform the pass1 scan of the class.
259      * Certain scan operations must be completed for all classes before
260      * the remaining operations can be completed.
261      */

262     //@olsen: dropped argument: boolean filterRequired
263
public void scan1() {
264         //@olsen: moved verbose output from ClassControl to ClassAction
265
env.message("scanning class " + control.userClassName());//NOI18N
266

267         //@olsen: added constraints; ensured by ClassControl
268
affirm(!classFile().isInterface());
269         affirm(control.persistType() > ClassControl.TransientOnly);
270
271         scanAttributes();
272
273         //@olsen: 4357074 skip previously enhanced files
274
if (previouslyAnnotated) {
275             return;
276         }
277
278         //@olsen: initialize 'implementsPersistence' flag from JDOMetaData
279
final String JavaDoc name = className();
280         implementsPersistence
281             = env.getJDOMetaData().isPersistenceCapableRootClass(name);
282
283         //@olsen: checks on persistence-capable classes
284
final boolean isPersistent
285             = (control.persistType() == ClassControl.PersistCapable);
286         if (isPersistent) {
287             // check whether this class directly implements PersistenceCapable
288
// or clonable.
289
scanForImplementsInterfaces();
290
291             // check only fields of persistence-capable classes
292
scanFields();
293         }
294
295         //@olsen: removed check, ensured before already
296
//if (!previouslyAnnotated && !classFile().isInterface())
297
scanMethods();
298     }
299
300     /**
301      * Add an PersistenceCapable implementation and PersistenceCapableHooks
302      * implementation if needed.
303      */

304     //@olsen: subst: augmentInterfaces -> augment
305
//@olsen: dropped argument: boolean filterRequired
306
public void augment() {
307         if (previouslyAnnotated)
308             return;
309
310         if (implementsPersistence) {
311             env.message("augmenting class " + control.userClassName());//NOI18N
312

313             if (!sawImplementsPersistenceCapable) {
314                 augmentClassInterface(JDOMetaData.JDOPersistenceCapablePath);
315             }
316
317             if (!sawImplementsCloneable) {
318                 augmentClassInterface(JDOMetaData.javaLangCloneablePath);
319             }
320
321             //@olsen: made fields to have public access
322
{
323                 insertPersistenceCapableFields(
324                     JDOMetaData.JDOStateManagerFieldName,
325                     JDOMetaData.JDOStateManagerFieldSig,
326                     JDOMetaData.JDOStateManagerFieldType,
327                     ACCTransient | ACCPublic);
328
329                 insertPersistenceCapableFields(
330                     JDOMetaData.JDOFlagsFieldName,
331                     JDOMetaData.JDOFlagsFieldSig,
332                     JDOMetaData.JDOFlagsFieldType,
333                     ACCTransient | ACCPublic);
334             }
335
336             insertPersistenceCapableMethods();
337         }
338
339 //@olsen: disabled feature
340
/*
341         if (getImplementsPersistenceHooks() &&
342             !control.hasPersistenceCapableHooksProvided()) {
343             env.message("modifying class " + control.userClassName() +
344                         " to implement " +
345                         ClassControl.userClassFromVMClass(
346                             JDOMetaData.JDOInstanceCallbacksName));
347
348             augmentClassInterface(JDOMetaData.JDOInstanceCallbacksName);
349             insertPersistenceCapableHooksMethods();
350         }
351 */

352     }
353
354     /**
355      * Modify the class references within this class according to the
356      * mappings in classTranslations.
357      */

358 //@olsen: disabled feature
359
/*
360     public void retarget(Map classTranslations,
361                          boolean filterRequired) {
362
363         //@olsen: added final modifiers
364         final ConstantPool pool = classFile().pool();
365
366         // First, translate the constant pool
367         final int nEntries = pool.nEntries();
368         for (int i=0; i<nEntries; i++) {
369             ConstBasic basic = pool.constantAt(i);
370             if (basic != null) {
371                 if (basic instanceof ConstClass) {
372                     ConstClass classRef = (ConstClass) basic;
373                     String classRefName = classRef.asString();
374                     String translation =
375                         Descriptor.translateClass(classRefName,
376                                                   classTranslations);
377                     if (translation != classRefName)
378                         classRef.changeClass(pool.addUtf8(translation));
379                 } else if (basic instanceof ConstNameAndType) {
380                     ConstNameAndType ntRef = (ConstNameAndType) basic;
381                     String sig = ntRef.signature().asString();
382                     String modSig = Descriptor.remapTypes(sig,
383                                                           classTranslations);
384                     if (!modSig.equals(sig))
385                         ntRef.changeSignature(pool.addUtf8(modSig));
386                 }
387             }
388         }
389
390         // Next, translate method signatures
391         for (Enumeration cme = classFile().methods().elements();
392              cme.hasMoreElements(); ) {
393             ClassMethod method = (ClassMethod) cme.nextElement();
394             String sig = method.signature().asString();
395             String newSig = Descriptor.remapTypes(sig, classTranslations);
396             if (!newSig.equals(sig))
397                 method.changeSignature(pool.addUtf8(newSig));
398         }
399
400         // Next, translate field signatures
401         for (Enumeration cfe = classFile().fields().elements();
402              cfe.hasMoreElements(); ) {
403             ClassField field = (ClassField) cfe.nextElement();
404             String sig = field.signature().asString();
405             String newSig = Descriptor.remapTypes(sig, classTranslations);
406             if (!newSig.equals(sig))
407                 field.changeSignature(pool.addUtf8(newSig));
408         }
409
410 //@olsen: disabled feature
411 //
412         InvokeAnnotation.retarget(env, classTranslations);
413 //
414
415         for (Iterator me = methodActionTable.values().iterator();
416              me.hasNext();){
417             MethodAction ma = (MethodAction)me.next();
418             ma.retarget(classTranslations);
419         }
420
421         for (Iterator fe = fieldActionTable.iterator(); fe.hasNext();){
422             FieldAction fa = (FieldAction)fe.next();
423             fa.retarget(classTranslations);
424         }
425
426         if (!filterRequired)
427             return;
428
429         // this should be correct most of the time, but may cause some classes
430         // to be updated unnecessarily.
431         control.noteUpdate();
432     }
433 */

434
435     /**
436      * Perform the annotation operations for this class
437      */

438     public void annotate() {
439         if (previouslyAnnotated)
440             return;
441
442         //@olsen: moved verbose output from ClassControl to ClassAction
443
env.message("annotating class " + control.userClassName());//NOI18N
444

445         boolean updates = false;
446
447         for (Iterator JavaDoc e = methodActions(); e.hasNext(); ) {
448             MethodAction methodAction = (MethodAction)e.next();
449             if (methodAction.needsAnnotation()) {
450                 methodAction.annotate();
451                 updates = true;
452             }
453         }
454
455 //@olsen: disabled feature
456
/*
457         if ((generate & GENInitContents) != 0) {
458             classFile().addMethod(methodBuilder.makeInitializeContents(this));
459             updates = true;
460         }
461
462         if ((generate & GENFlushContents) != 0) {
463             classFile().addMethod(methodBuilder.makeFlushContents(this));
464             updates = true;
465         }
466
467         if ((generate & GENClearContents) != 0) {
468             classFile().addMethod(methodBuilder.makeClearContents(this));
469             updates = true;
470         }
471 */

472
473         // Even if we haven't updated anything, we want to add an annotated
474
// attribute if we are doing in-place updates so that we don't
475
// rewrite the file every time.
476
if (updates || env.updateInPlace()) {
477             control.noteUpdate();
478
479             //^olsen: to test
480

481             // Leave a hint that we've been here before
482
final byte[] data = new byte[2];
483             data[0] = (byte)(AnnotatedVersion >>> 8);
484             data[1] = (byte)(AnnotatedVersion & 0xff);
485             final ClassAttribute annotatedAttr
486                 = new GenericAttribute(
487                     classFile().pool().addUtf8(AnnotatedAttribute), data);
488             classFile().attributes().addElement(annotatedAttr);
489         }
490     }
491
492     // package accessors
493

494     /**
495      * Get the control object
496      */

497     ClassControl classControl() {
498         return control;
499     }
500
501     /**
502      * Get the class file which we are operating on
503      */

504     ClassFile classFile() {
505         return control.classFile();
506     }
507
508     /**
509      * Return an Enumeration of the FieldActions for the class
510      */

511     Iterator JavaDoc fieldActions() {
512         return fieldActionTable.iterator();
513     }
514
515     /**
516      * Return an Enumeration of the MethodActions for the class
517      */

518     Iterator JavaDoc methodActions() {
519         return methodActionTable.values().iterator();
520     }
521
522     /**
523      * Return the class name in VM form
524      */

525     public String JavaDoc className() {
526         return control.className();
527     }
528
529     /**
530      * Return the class name in user ('.' delimited) form
531      */

532     public String JavaDoc userClassName() {
533         return control.userClassName();
534     }
535
536     /**
537      * Return true if this class will implement PersistenceCapable.
538      */

539     public boolean getImplementsPersistence() {
540         return implementsPersistence;
541     }
542
543     /**
544      * Return true if this class will implement PersistenceCapableHooks.
545      */

546 //@olsen: disabled feature
547
/*
548     public boolean getImplementsPersistenceHooks() {
549         if (!implementsPersistenceHooksKnown) {
550             if (!needsPreDestroyPersistent ||
551                 !needsPreClearContents ||
552                 !needsPreFlushContents ||
553                 !needsPostInitializeContents)
554                 implementsPersistenceHooks = true;
555             implementsPersistenceHooksKnown = true;
556         }
557         return implementsPersistenceHooks;
558     }
559 */

560
561     /**
562      * Return true if this method needs an implementation of clone().
563      */

564     public boolean hasCloneMethod() {
565         return sawMethodJDOClone;
566     }
567
568     /**
569      * Checks the class attributes for a filter.annotated attribute.
570      * This works even if scan1 hasn't run yet.
571      */

572     public boolean hasAnnotatedAttribute() {
573         if (previouslyAnnotated)
574             return true;
575
576         Enumeration JavaDoc e = classFile().attributes().elements();
577         while (e.hasMoreElements()) {
578             ClassAttribute attr = (ClassAttribute) e.nextElement();
579             if (attr.attrName().asString().equals(AnnotatedAttribute))
580                 return true;
581         }
582
583         return false;
584     }
585
586     /**
587      * Check whether this class has a persistent field of the
588      * specified name. This might be called when the FieldActions have
589      * not yet been built.
590      */

591 //@olsen: disabled feature
592
/*
593     boolean fieldIsPersistent(String fieldName) {
594         ClassField field = classFile().findField(fieldName);
595         if (field != null) {
596             String className = classFile().className().asString();
597             String fieldName = field.name().asString();
598             //@olsen: disabled feature
599             //return FieldAction.fieldIsPersistent(classFile(), field);
600             return env.getJDOMetaData().isPersistentField(className, fieldName);
601         }
602         return false;
603     }
604 */

605
606     // private methods
607

608     /**
609      * Scans the attributes of a ClassFile
610      */

611     private void scanAttributes() {
612         Enumeration JavaDoc e = classFile().attributes().elements();
613         while (e.hasMoreElements()) {
614             ClassAttribute attr = (ClassAttribute) e.nextElement();
615             if (attr.attrName().asString().equals(AnnotatedAttribute)) {
616                 previouslyAnnotated = true;
617
618 //@olsen: disabled feature
619
/*
620                 if (!control.isImplicitlyPersistent() && !env.updateInPlace()) {
621 */

622                 {
623                     // At some point we may want to consider stripping old
624
// annotations and re-annotating, but not yet
625
env.message("ignoring previously enhanced class "//NOI18N
626
+ control.userClassName());
627                 }
628                 break;
629             }
630         }
631     }
632
633     /**
634      * Scans the class to check whether it implemens interfaces
635      * PersistenceCapable and Clonable.
636      * Sets instance variables <code>sawImplementsPersistenceCapable</code> if
637      * the class implements <code>JDOMetaData.JDOPersistenceCapablePath</code>.
638      * Sets the instance variable <code>sawImplementsCloneable</code>
639      * if the class implements <code>JDOMetaData.javaLangCloneablePath</code>.
640      * Please note that only the current class is scanned for implemented
641      * interfaces by this method. Even if the super class implements
642      * one of the above interfaces, the corresponding instance variable will
643      * not be set.
644      */

645     //@olsen: added method
646
private void scanForImplementsInterfaces() {
647         for (Iterator JavaDoc ifc = classFile().interfaces().iterator();
648              ifc.hasNext();) {
649             final ConstClass i = (ConstClass)ifc.next();
650             String JavaDoc interfaceNamePath = i.asString();
651             if (interfaceNamePath.equals(JDOMetaData.JDOPersistenceCapablePath)) {
652                 sawImplementsPersistenceCapable = true;
653
654                 //@olsen: warn if user-defined 'implements PC' clause
655
env.warning(
656                     getI18N("enhancer.class_implements_jdo_pc",//NOI18N
657
new Object JavaDoc[]{
658                                 userClassName(),
659                                 JDOMetaData.JDOPersistenceCapableType
660                             }));
661             }
662             if(JDOMetaData.javaLangCloneablePath.equals(interfaceNamePath) ) {
663                 sawImplementsCloneable = true;
664             }
665         }
666
667 //@olsen: disabled feature
668
//@olsen: don't check whether this class implements PC indirectly
669
/*
670         if (control.implementsPersistenceCapable())
671             env.warning(
672                 getI18N("enhancer.class_implements_pc",
673                         userClassName(),
674                         meta.JDOPersistenceCapableType));
675 */

676     }
677
678     /**
679      * Scans the fields of a ClassFile
680      * If this is not a persistence capable class, do nothing.
681      */

682     private void scanFields() {
683         Enumeration JavaDoc e = classFile().fields().elements();
684         while (e.hasMoreElements()) {
685             final ClassField f = (ClassField)e.nextElement();
686             final String JavaDoc fieldName = f.name().asString();
687             final String JavaDoc fieldSig = f.signature().asString();
688
689             //@olsen: added check
690
scanForJDOFields(fieldName, fieldSig);
691
692             FieldAction action = new FieldAction(this, f, env);
693             action.check();
694             fieldActionTable.add(action);
695         }
696     }
697
698     /**
699      * Scan for JDO fields.
700      */

701     //@olsen: added method
702
private void scanForJDOFields(String JavaDoc fieldName,
703                                   String JavaDoc fieldSig) {
704         if (fieldName.equals(JDOMetaData.JDOStateManagerFieldName)) {
705             env.error(
706                 getI18N("enhancer.class_defines_jdo_field",//NOI18N
707
userClassName(),
708                         JDOMetaData.JDOStateManagerFieldName));
709             sawFieldJDOStateManager = true;
710             return;
711         }
712         if (fieldName.equals(JDOMetaData.JDOFlagsFieldName)) {
713             env.error(
714                 getI18N("enhancer.class_defines_jdo_field",//NOI18N
715
userClassName(),
716                         JDOMetaData.JDOFlagsFieldName));
717             sawFieldJDOFlags = true;
718             return;
719         }
720         //@olsen: check whether member starts with the reserved jdo prefix
721
if (fieldName.startsWith("jdo")) {//NOI18N
722
//@olsen: issue a warning only
723
env.warning(
724                 getI18N("enhancer.class_has_jdo_like_member",//NOI18N
725
userClassName(), fieldName));
726             return;
727         }
728     }
729
730     /**
731      * Scans the methods of a ClassFile.
732      */

733     private void scanMethods() {
734         final boolean isPersistent
735             = (control.persistType() == ClassControl.PersistCapable);
736
737         Enumeration JavaDoc e = classFile().methods().elements();
738         while (e.hasMoreElements()) {
739             final ClassMethod m = (ClassMethod)e.nextElement();
740             final String JavaDoc methodName = m.name().asString();
741             final String JavaDoc methodSig = m.signature().asString();
742
743             if (isPersistent) {
744                 scanForJDOMethods(methodName, methodSig);
745             }
746
747             final MethodAction action = new MethodAction(this, m, env);
748             action.check();
749             methodActionTable.put(m, action);
750         }
751     }
752
753
754     /**
755      * Scan for JDO methods.
756      */

757     //@olsen: moved code from scanMethods()
758
private void scanForJDOMethods(String JavaDoc methodName,
759                                    String JavaDoc methodSig) {
760         if (methodName.equals(jdoGetStateManagerName)) {
761             env.error(
762                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
763
userClassName(), methodName));
764             sawMethodJDOGetStateManager = true;
765             return;
766         }
767         if (methodName.equals(jdoSetStateManagerName)) {
768             env.error(
769                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
770
userClassName(), methodName));
771             sawMethodJDOSetStateManager = true;
772             return;
773         }
774         if (methodName.equals(jdoGetFlagsName)) {
775             env.error(
776                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
777
userClassName(), methodName));
778             sawMethodJDOGetFlags = true;
779             return;
780         }
781         if (methodName.equals(jdoSetFlagsName)) {
782             env.error(
783                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
784
userClassName(), methodName));
785             sawMethodJDOSetFlags = true;
786             return;
787         }
788         if (methodName.equals(jdoMakeDirtyName)) {
789             env.error(
790                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
791
userClassName(), methodName));
792             sawMethodJDOMakeDirty = true;
793             return;
794         }
795         if (methodName.equals(jdoIsDirtyName)) {
796             env.error(
797                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
798
userClassName(), methodName));
799             sawMethodJDOIsDirty = true;
800             return;
801         }
802         if (methodName.equals(jdoIsTransactionalName)) {
803             env.error(
804                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
805
userClassName(), methodName));
806             sawMethodJDOIsTransactional = true;
807             return;
808         }
809         if (methodName.equals(jdoIsPersistentName)) {
810             env.error(
811                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
812
userClassName(), methodName));
813             sawMethodJDOIsPersistent = true;
814             return;
815         }
816         if (methodName.equals(jdoIsNewName)) {
817             env.error(
818                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
819
userClassName(), methodName));
820             sawMethodJDOIsNew = true;
821             return;
822         }
823         if (methodName.equals(jdoIsDeletedName)) {
824             env.error(
825                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
826
userClassName(), methodName));
827             sawMethodJDOIsDeleted = true;
828             return;
829         }
830         if (methodName.equals(jdoGetPersistenceManagerName)) {
831             env.error(
832                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
833
userClassName(), methodName));
834             sawMethodJDOGetPersistenceManager = true;
835             return;
836         }
837         if (methodName.equals(jdoGetObjectIdName)) {
838             env.error(
839                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
840
userClassName(), methodName));
841             sawMethodJDOGetObjectId = true;
842             return;
843         }
844         //^olsen: get signature from method builder
845
// for jdo constructor, check by name and signature
846
if (methodName.equals(jdoConstructorName)
847             && methodSig.equals("(" + JDOMetaData.JDOStateManagerSig + ")V")) {//NOI18N
848
env.error(
849                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
850
userClassName(), methodName));
851             sawMethodJDOConstructor = true;
852             return;
853         }
854         if (methodName.equals(jdoNewInstanceName)) {
855             env.error(
856                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
857
userClassName(), methodName));
858             sawMethodJDONewInstance = true;
859             return;
860         }
861         if (methodName.equals(jdoClearName)) {
862             env.error(
863                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
864
userClassName(), methodName));
865             sawMethodJDOClear = true;
866             return;
867         }
868         if (methodName.equals(jdoCopyName)) {
869             env.error(
870                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
871
userClassName(), methodName));
872             sawMethodJDOCopy = true;
873             return;
874         }
875         if (methodName.equals(jdoGetFieldName)) {
876             env.error(
877                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
878
userClassName(), methodName));
879             sawMethodJDOGetField = true;
880             return;
881         }
882         if (methodName.equals(jdoSetFieldName)) {
883             env.error(
884                 getI18N("enhancer.class_defines_jdo_method",//NOI18N
885
userClassName(), methodName));
886             sawMethodJDOSetField = true;
887             return;
888         }
889         //^olsen: get signature from method builder
890
// for method clone(), check by name and signature
891
if (methodName.equals(jdoCloneName)
892             && methodSig.equals("()Ljava/lang/Object;")) {//NOI18N
893
// it's OK to have a user-defined clone()
894
sawMethodJDOClone = true;
895             return;
896         }
897         //@olsen: check whether member starts with the reserved jdo prefix
898
if (methodName.startsWith("jdo")) {//NOI18N
899
//@olsen: issue a warning only
900
env.warning(
901                 getI18N("enhancer.class_has_jdo_like_member",//NOI18N
902
userClassName(), methodName));
903             return;
904         }
905
906 //@olsen: disabled feature
907
/*
908         boolean sawInitializeContents = false;
909         boolean sawFlushContents = false;
910         boolean sawClearContents = false;
911 */

912 //@olsen: disabled feature
913
/*
914         else if (methodName.equals("initializeContents") &&
915                  methodSig.equals("(com/sun/forte4j/persistence/internal/ObjectContents;)V"))
916             sawInitializeContents = true;
917         else if (methodName.equals("flushContents") &&
918                  methodSig.equals("(Lcom/sun/forte4j/persistence/internal/ObjectContents;)V"))
919             sawFlushContents = true;
920         else if (methodName.equals("clearContents") &&
921                  methodSig.equals("()V"))
922             sawClearContents = true;
923 */

924 //@olsen: disabled feature
925
/*
926         else if (methodName.equals("preDestroyPersistent") &&
927                  methodSig.equals("()V"))
928             needsPreDestroyPersistent = false;
929         else if (methodName.equals("postInitializeContents") &&
930                  methodSig.equals("()V"))
931             needsPostInitializeContents = false;
932         else if (methodName.equals("preFlushContents") &&
933                  methodSig.equals("()V"))
934             needsPreFlushContents = false;
935         else if (methodName.equals("preClearContents") &&
936                  methodSig.equals("()V"))
937             needsPreClearContents = false;
938 */

939 //@olsen: disabled feature
940
/*
941             if (!sawInitializeContents)
942                 generate |= GENInitContents;
943             if (!sawFlushContents)
944                 generate |= GENFlushContents;
945             if (!sawClearContents)
946                 generate |= GENClearContents;
947 */

948     }
949
950     /**
951      * Add a field as the index'th element of the field vector in the class.
952      */

953     private void insertPersistenceCapableFields(String JavaDoc fieldName,
954                                                 String JavaDoc fieldSig,
955                                                 String JavaDoc printableFieldSig,
956                                                 int accessFlags) {
957         affirm(implementsPersistence);
958
959         control.noteUpdate();
960
961         // create it
962
env.message("adding "//NOI18N
963
+ control.userClassName() +
964                     "." + fieldName + " " + printableFieldSig);//NOI18N
965

966         final ClassFile cfile = classFile();
967         final ConstantPool pool = cfile.pool();
968
969         //@olsen: fix 4467428, add synthetic attribute for generated fields
970
final AttributeVector fieldAttrs = new AttributeVector();
971         fieldAttrs.addElement(
972             new SyntheticAttribute(
973                 pool.addUtf8(SyntheticAttribute.expectedAttrName)));
974
975         final ClassField theField
976             = new ClassField(accessFlags,
977                              pool.addUtf8(fieldName),
978                              pool.addUtf8(fieldSig),
979                              fieldAttrs);
980
981         cfile.addField(theField);
982     }
983
984     /**
985      * Add all the methods required for com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapable interface.
986      */

987     private void insertPersistenceCapableMethods() {
988         affirm(implementsPersistence);
989
990         control.noteUpdate();
991
992         //@olsen: simplified generation of jdo[GS]etStateManager methods
993
affirm(!sawMethodJDOGetStateManager);
994         classFile().addMethod(
995             methodBuilder.makeJDOGetStateManager(
996                 this,
997                 jdoGetStateManagerName));
998
999         affirm(!sawMethodJDOSetStateManager);
1000        classFile().addMethod(
1001            methodBuilder.makeJDOSetStateManager(
1002                this,
1003                jdoSetStateManagerName));
1004
1005        //@olsen: simplified generation of jdo[GS]etFlags methods
1006
affirm(!sawMethodJDOGetFlags);
1007        classFile().addMethod(
1008            methodBuilder.makeJDOGetFlags(
1009                this,
1010                jdoGetFlagsName));
1011        affirm(!sawMethodJDOSetFlags);
1012        classFile().addMethod(
1013            methodBuilder.makeJDOSetFlags(
1014                this,
1015                jdoSetFlagsName));
1016
1017        //@olsen: add generation of jdoMakeDirty() method
1018
affirm(!sawMethodJDOMakeDirty);
1019        classFile().addMethod(
1020            methodBuilder.makeJDOMakeDirtyMethod(
1021                this,
1022                jdoMakeDirtyName));
1023
1024        //@olsen: add generation of JDO interrogative methods
1025
affirm(!sawMethodJDOIsDirty);
1026        classFile().addMethod(
1027            methodBuilder.makeJDOInterrogativeMethod(
1028                this,
1029                jdoIsDirtyName));
1030        affirm(!sawMethodJDOIsTransactional);
1031        classFile().addMethod(
1032            methodBuilder.makeJDOInterrogativeMethod(
1033                this,
1034                jdoIsTransactionalName));
1035        affirm(!sawMethodJDOIsPersistent);
1036        classFile().addMethod(
1037            methodBuilder.makeJDOInterrogativeMethod(
1038                this,
1039                jdoIsPersistentName));
1040        affirm(!sawMethodJDOIsNew);
1041        classFile().addMethod(
1042            methodBuilder.makeJDOInterrogativeMethod(
1043                this,
1044                jdoIsNewName));
1045        affirm(!sawMethodJDOIsDeleted);
1046        classFile().addMethod(
1047            methodBuilder.makeJDOInterrogativeMethod(
1048                this,
1049                jdoIsDeletedName));
1050
1051        //@olsen: add generation of jdoGetPersistenceManager method
1052
affirm(!sawMethodJDOGetPersistenceManager);
1053        classFile().addMethod(
1054            methodBuilder.makeJDOGetPersistenceManagerMethod(
1055                this,
1056                jdoGetPersistenceManagerName));
1057
1058        //@olsen: add generation of jdoGetObjectId method
1059
affirm(!sawMethodJDOGetObjectId);
1060        classFile().addMethod(
1061            methodBuilder.makeJDOGetObjectIdMethod(
1062                this,
1063                jdoGetObjectIdName));
1064
1065        //@olsen: add generation of the JDO constructor
1066
affirm(!sawMethodJDOConstructor);
1067        classFile().addMethod(
1068            methodBuilder.makeJDOConstructor(
1069                this,
1070                jdoConstructorName));
1071
1072        //@olsen: add generation of the jdoNewInstance method
1073
affirm(!sawMethodJDONewInstance);
1074        classFile().addMethod(
1075            methodBuilder.makeJDONewInstanceMethod(
1076                this,
1077                jdoNewInstanceName));
1078
1079        //@olsen: add generation of the jdoGetField method
1080
affirm(!sawMethodJDOGetField);
1081        classFile().addMethod(
1082            methodBuilder.makeJDOGetFieldMethod(
1083                this,
1084                jdoGetFieldName));
1085
1086        //@olsen: add generation of the jdoSetField method
1087
affirm(!sawMethodJDOSetField);
1088        classFile().addMethod(
1089            methodBuilder.makeJDOSetFieldMethod(
1090                this,
1091                jdoSetFieldName));
1092
1093        //@olsen: add generation of the jdoClear method
1094
affirm(!sawMethodJDOClear);
1095        classFile().addMethod(
1096            methodBuilder.makeJDOClearMethod(
1097                this,
1098                jdoClearName));
1099
1100        //@lars: removed jdoCopy-method creation
1101
//@olsen: add generation of the jdoCopy method
1102
/*
1103        affirm(!sawMethodJDOCopy);
1104        classFile().addMethod(
1105            methodBuilder.makeJDOCopyMethod(
1106                this,
1107                jdoCopyName));
1108        */

1109
1110        //@olsen: generate method clone() if not present
1111
if (!sawMethodJDOClone) {
1112            classFile().addMethod(
1113                methodBuilder.makeJDOClone(
1114                    this,
1115                    jdoCloneName));
1116        }
1117    }
1118
1119    /**
1120     * Add all the methods required for com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapableHooks interface.
1121     */

1122//@olsen: disabled feature
1123
/*
1124    private void insertPersistenceCapableHooksMethods() {
1125        if (needsPreDestroyPersistent)
1126            classFile().addMethod
1127                (methodBuilder.makeNullMethod(this, "preDestroyPersistent"));
1128
1129        if (needsPostInitializeContents)
1130            classFile().addMethod
1131                (methodBuilder.makeNullMethod(this, "postInitializeContents"));
1132
1133        if (needsPreFlushContents)
1134            classFile().addMethod
1135                (methodBuilder.makeNullMethod(this, "preFlushContents"));
1136
1137        if (needsPreClearContents)
1138            classFile().addMethod
1139                (methodBuilder.makeNullMethod(this, "preClearContents"));
1140
1141        if (needsPreDestroyPersistent || needsPostInitializeContents
1142            || needsPreFlushContents || needsPreClearContents)
1143            control.noteUpdate();
1144    }
1145*/

1146
1147    /**
1148     * Add the specified interface to list.
1149     */

1150    private void augmentClassInterface(String JavaDoc interfaceName) {
1151        control.noteUpdate();
1152        ClassFile cfile = classFile();
1153        ConstClass iface = cfile.pool().addClass(interfaceName);
1154        //@olsen: moved output to here
1155
env.message("adding implements "//NOI18N
1156
+ ClassControl.userClassFromVMClass(interfaceName));
1157        cfile.addInterface(iface);
1158    }
1159}
1160
Popular Tags