Source Code Cross Referenced for PatternAnalyser.java in  » IDE-Netbeans » beans » org » netbeans » modules » beans » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » beans » org.netbeans.modules.beans 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.beans.IntrospectionException;
045:        import java.util.ArrayList;
046:        import java.util.Collection;
047:        import java.util.Collections;
048:        import java.util.HashMap;
049:        import java.util.List;
050:        import java.util.Map;
051:        import java.util.concurrent.atomic.AtomicBoolean;
052:        import javax.lang.model.element.Element;
053:        import javax.lang.model.element.ElementKind;
054:        import javax.lang.model.element.ExecutableElement;
055:        import javax.lang.model.element.TypeElement;
056:        import javax.lang.model.element.VariableElement;
057:        import javax.lang.model.type.TypeKind;
058:        import javax.lang.model.type.TypeMirror;
059:        import javax.lang.model.util.ElementFilter;
060:        import static javax.lang.model.element.Modifier.*;
061:
062:        import org.netbeans.api.java.source.CompilationInfo;
063:        import org.netbeans.api.java.source.ElementHandle;
064:        import org.openide.util.Exceptions;
065:
066:        import static org.netbeans.modules.beans.BeanUtils.*;
067:        import org.netbeans.modules.beans.TmpPattern.Property;
068:        import org.netbeans.modules.beans.TmpPattern.IdxProperty;
069:        import org.netbeans.modules.beans.TmpPattern.EventSet;
070:        import org.openide.filesystems.FileObject;
071:
072:        /** Analyses the ClassElement trying to find source code patterns i.e.
073:         * properties or event sets;
074:         * 
075:         * @author Petr Hrebejk
076:         */
077:
078:        public final class PatternAnalyser {
079:
080:            private BeanPanelUI ui;
081:
082:            private FileObject fileObject;
083:
084:            // Enclosing class
085:            private ElementHandle<TypeElement> classElementHandle;
086:
087:            // Classes
088:            private ArrayList<ClassPattern> currentClassesPatterns = new ArrayList<ClassPattern>();
089:            // Properties
090:            private ArrayList<PropertyPattern> currentPropertyPatterns = new ArrayList<PropertyPattern>();
091:            // Indexed properties
092:            private ArrayList<IdxPropertyPattern> currentIdxPropertyPatterns = new ArrayList<IdxPropertyPattern>();
093:            // Event sets
094:            private ArrayList<EventSetPattern> currentEventSetPatterns = new ArrayList<EventSetPattern>();
095:
096:            AtomicBoolean canceled = new AtomicBoolean();
097:
098:            public PatternAnalyser(FileObject fileObject, BeanPanelUI ui) {
099:                this .fileObject = fileObject;
100:                this .ui = ui;
101:            }
102:
103:            public PatternAnalyser(FileObject fileObject, BeanPanelUI ui,
104:                    Collection<ClassPattern> classes) {
105:                this (fileObject, ui);
106:                this .currentClassesPatterns.addAll(classes);
107:            }
108:
109:            public List<PropertyPattern> getPropertyPatterns() {
110:                return currentPropertyPatterns;
111:            }
112:
113:            public List<IdxPropertyPattern> getIdxPropertyPatterns() {
114:                return currentIdxPropertyPatterns;
115:            }
116:
117:            public List<EventSetPattern> getEventSetPatterns() {
118:                return currentEventSetPatterns;
119:            }
120:
121:            // XXX Sorting
122:            public List<Pattern> getPatterns() {
123:                List<Pattern> patterns = new ArrayList<Pattern>(
124:                        currentClassesPatterns.size()
125:                                + currentEventSetPatterns.size()
126:                                + currentIdxPropertyPatterns.size()
127:                                + currentIdxPropertyPatterns.size());
128:
129:                patterns.addAll(currentPropertyPatterns);
130:                patterns.addAll(currentIdxPropertyPatterns);
131:                patterns.addAll(currentEventSetPatterns);
132:                patterns.addAll(currentClassesPatterns);
133:
134:                return patterns;
135:            }
136:
137:            /** Gets the classelemnt of this pattern analyser */
138:            public ElementHandle<TypeElement> getClassElementHandle() {
139:                return classElementHandle;
140:            }
141:
142:            public BeanPanelUI getUI() {
143:                return ui;
144:            }
145:
146:            public FileObject getFileObject() {
147:                return fileObject;
148:            }
149:
150:            /** Analyzes given element and returns the PatterAnalyzer containing the
151:             * result.
152:             * 
153:             * XXX Make it really cancelable.
154:             * 
155:             */
156:            public void analyzeAll(CompilationInfo ci, TypeElement element) {
157:
158:                if (isCanceled()) {
159:                    return;
160:                }
161:
162:                Parameters p = new Parameters(ci, element);
163:                this .classElementHandle = ElementHandle.create(element);
164:
165:                // Analyse patterns
166:                resolveMethods(p);
167:                resolveFields(p);
168:
169:                // Now analyze innerclasses
170:                resolveTypes(p);
171:
172:                // Create the real patterns // Compare old and new patterns to resolve changes
173:
174:                try {
175:                    resolveChangesOfProperties(p);
176:                    resolveChangesOfIdxProperties(p);
177:                    resolveChangesOfEventSets(p);
178:                } catch (IntrospectionException e) {
179:                    Exceptions.printStackTrace(e);
180:                }
181:
182:            }
183:
184:            private boolean isCanceled() {
185:                synchronized (canceled) {
186:                    return canceled.get();
187:                }
188:            }
189:
190:            public void cancel() {
191:                synchronized (canceled) {
192:                    canceled.set(true);
193:                }
194:            }
195:
196:            // Private methods ---------------------------------------------------------
197:
198:            private void resolveTypes(Parameters p) {
199:
200:                List<TypeElement> types = ElementFilter.typesIn(p.element
201:                        .getEnclosedElements());
202:
203:                for (TypeElement typeElement : types) {
204:                    if (typeElement.getKind() == ElementKind.CLASS
205:                            || typeElement.getKind() == ElementKind.INTERFACE) {
206:                        PatternAnalyser pa = new PatternAnalyser(p.ci
207:                                .getFileObject(), ui);
208:                        pa.analyzeAll(p.ci, typeElement);
209:                        ClassPattern cp = new ClassPattern(pa, typeElement
210:                                .asType(), BeanUtils.nameAsString(typeElement));
211:                        currentClassesPatterns.add(cp);
212:                    }
213:                }
214:
215:            }
216:
217:            /** This method analyses the ClassElement for "property patterns".
218:             * The method is analogous to JavaBean Introspector methods for classes
219:             * without a BeanInfo.
220:             */
221:            private void resolveMethods(Parameters p) {
222:
223:                // First get all methods in classElement
224:                List<ExecutableElement> methods = ElementFilter
225:                        .methodsIn(p.element.getEnclosedElements());
226:
227:                // Temporary structures for analysing EventSets
228:                Map<String, ExecutableElement> adds = new HashMap<String, ExecutableElement>();
229:                Map<String, ExecutableElement> removes = new HashMap<String, ExecutableElement>();
230:
231:                // Analyze each method
232:                for (ExecutableElement method : methods) {
233:                    String name = nameAsString(method);
234:                    int len = name.length();
235:
236:                    if ((name.startsWith(GET_PREFIX) && len > GET_PREFIX
237:                            .length())
238:                            || (name.startsWith(SET_PREFIX) && len > SET_PREFIX
239:                                    .length())
240:                            || (name.startsWith(IS_PREFIX) && len > IS_PREFIX
241:                                    .length())) {
242:                        Property pp = analyseMethodForProperties(p, method);
243:                        if (pp != null)
244:                            addProperty(p, pp);
245:                    }
246:                    if ((name.startsWith(ADD_PREFIX) && len > ADD_PREFIX
247:                            .length())
248:                            || (name.startsWith(REMOVE_PREFIX) && len > REMOVE_PREFIX
249:                                    .length())) {
250:                        analyseMethodForEventSets(p, method, adds, removes);
251:                    }
252:                }
253:                // Resolve the temporay structures of event sets
254:
255:                // Now look for matching addFooListener+removeFooListener pairs.
256:                for (String compound : adds.keySet()) {
257:                    // Skip any "add" which doesn't have a matching remove // NOI18N
258:                    if (removes.get(compound) == null) {
259:                        continue;
260:                    }
261:                    // Method name has to end in Listener
262:                    if (compound.indexOf("Listener:") <= 0) { // NOI18N
263:                        continue;
264:                    }
265:
266:                    ExecutableElement addMethod = adds.get(compound);
267:                    ExecutableElement removeMethod = removes.get(compound);
268:
269:                    List<? extends VariableElement> params = addMethod
270:                            .getParameters();
271:                    TypeMirror argType = params.get(0).asType();
272:
273:                    // Check if the argument is a subtype of EventListener
274:                    if (argType.getKind() != TypeKind.DECLARED || // filter out primitive types + arrays
275:                            !p.ci.getTypes()
276:                                    .isSubtype(
277:                                            argType,
278:                                            p.ci.getElements().getTypeElement(
279:                                                    "java.util.EventListener")
280:                                                    .asType())) {
281:                        continue;
282:                    }
283:                    EventSet esp = new EventSet(p.ci, addMethod, removeMethod);
284:                    addEventSet(p, esp);
285:                }
286:            }
287:
288:            private void resolveFields(Parameters p) {
289:
290:                // Analyze fields
291:                List<VariableElement> fields = ElementFilter.fieldsIn(p.element
292:                        .getEnclosedElements());
293:                ;
294:
295:                String propertyStyle = PropertyActionSettings.getDefault()
296:                        .getPropStyle();
297:
298:                for (VariableElement field : fields) {
299:
300:                    if (field.getModifiers().contains(STATIC)) {
301:                        continue;
302:                    }
303:
304:                    //System.out.println("Property style " + propertyStyle);   
305:                    String fieldName = nameAsString(field);
306:                    //System.out.println("Field name1 " + fieldName);
307:                    if (fieldName.startsWith(propertyStyle)) {
308:                        fieldName = fieldName.substring(1);
309:                        //System.out.println("Field name2 " + fieldName);
310:                    }
311:
312:                    Property pp = p.propertyPatterns.get(fieldName);
313:                    if (pp == null)
314:                        pp = p.idxPropertyPatterns.get(fieldName);
315:                    if (pp == null)
316:                        continue;
317:                    TypeMirror ppType = pp.type;
318:                    if (ppType != null && ppType.equals(field.asType()))
319:                        pp.estimatedField = field;
320:                }
321:            }
322:
323:            /** Analyses one method for property charcteristics */
324:            Property analyseMethodForProperties(Parameters p,
325:                    ExecutableElement method) {
326:                // Skip static methods as Introspector does.
327:
328:                if (method.getModifiers().contains(STATIC)) {
329:                    return null;
330:                }
331:
332:                String name = nameAsString(method);
333:                VariableElement[] params = method.getParameters().toArray(
334:                        new VariableElement[0]);
335:                TypeMirror returnType = method.getReturnType();
336:
337:                Property pp = null;
338:
339:                try {
340:                    if (params.length == 0) {
341:                        if (name.startsWith(GET_PREFIX)) {
342:                            // SimpleGetter
343:                            pp = new Property(method, null);
344:                        } else if (returnType.getKind() == TypeKind.BOOLEAN
345:                                && name.startsWith(IS_PREFIX)) {
346:                            // Boolean getter
347:                            pp = new Property(method, null);
348:                        }
349:                    } else if (params.length == 1) {
350:                        if (params[0].asType().getKind() == TypeKind.INT
351:                                && name.startsWith(GET_PREFIX)) {
352:                            pp = new IdxProperty(p.ci, null, null, method, null);
353:                        } else if (returnType.getKind() == TypeKind.VOID
354:                                && name.startsWith(SET_PREFIX)) {
355:                            pp = new Property(null, method);
356:                            // PENDING vetoable => constrained
357:                        }
358:                    } else if (params.length == 2) {
359:                        if (params[0].asType().getKind() == TypeKind.INT
360:                                && name.startsWith(SET_PREFIX)) {
361:                            pp = new IdxProperty(p.ci, null, null, null, method);
362:                            // PENDING vetoable => constrained
363:                        }
364:                    }
365:                } catch (IntrospectionException ex) {
366:                    // PropertyPattern constructor found some differencies from design patterns.
367:                    Exceptions.printStackTrace(ex);
368:                    pp = null;
369:                }
370:
371:                return pp;
372:            }
373:
374:            /** Method analyses class methods for EventSetPatterns
375:             */
376:            void analyseMethodForEventSets(Parameters p,
377:                    ExecutableElement method,
378:                    Map<String, ExecutableElement> adds,
379:                    Map<String, ExecutableElement> removes) {
380:                // Skip static methods
381:                if (method.getModifiers().contains(STATIC)) {
382:                    return;
383:                }
384:
385:                String name = nameAsString(method);
386:                VariableElement params[] = method.getParameters().toArray(
387:                        new VariableElement[0]);
388:                TypeMirror returnType = method.getReturnType();
389:
390:                if (params.length == 1 && returnType.getKind() == TypeKind.VOID) {
391:                    TypeMirror paramType = params[0].asType();
392:                    if (paramType.getKind() == TypeKind.DECLARED) {
393:                        Element lsnrType = p.ci.getTypes().asElement(paramType);
394:                        String lsnrTypeName = nameAsString(lsnrType);
395:                        if (name.startsWith(ADD_PREFIX)
396:                                && name.substring(3).equals(lsnrTypeName)) {
397:                            String compound = name.substring(3) + ":"
398:                                    + nameAsString(lsnrType); // NOI18N
399:                            adds.put(compound, method);
400:                        } else if (name.startsWith(REMOVE_PREFIX)
401:                                && name.substring(6).equals(lsnrTypeName)) {
402:                            String compound = name.substring(6) + ":"
403:                                    + nameAsString(lsnrType); // NOI18N
404:                            removes.put(compound, method);
405:                        }
406:                    }
407:                }
408:
409:            }
410:
411:            // Utility methods --------------------------------------------------------------------
412:
413:            /** Adds the new property. Or generates composite property if property
414:             *  of that name already exists. It puts the property in the right HashMep
415:             * according to type of property idx || not idx
416:             */
417:            @SuppressWarnings("unchecked")
418:            private void addProperty(Parameters p, Property property) {
419:                boolean isIndexed = property instanceof  IdxProperty;
420:                HashMap hm = isIndexed ? p.idxPropertyPatterns
421:                        : p.propertyPatterns;
422:                String name = property.name;
423:
424:                Property old = p.propertyPatterns.get(name);
425:                if (old == null)
426:                    old = p.idxPropertyPatterns.get(name);
427:
428:                if (old == null) { // There is no other property of that name
429:                    hm.put(name, property);
430:                    return;
431:                }
432:
433:                // If the property type has changed, use new property pattern
434:                TypeMirror opt = old.type;
435:                TypeMirror npt = property.type;
436:                if (opt != null && npt != null && !opt.equals(npt)) {
437:                    hm.put(name, property);
438:                    return;
439:                }
440:
441:                boolean isOldIndexed = old instanceof  IdxProperty;
442:
443:                if (isIndexed || isOldIndexed) {
444:                    if (isIndexed && !isOldIndexed) {
445:                        p.propertyPatterns.remove(old.name); // Remove old from not indexed
446:                    } else if (!isIndexed && isOldIndexed) {
447:                        p.idxPropertyPatterns.remove(old.name); // Remove old from indexed
448:                    }
449:                    IdxProperty composite = new IdxProperty(old, property);
450:                    p.idxPropertyPatterns.put(name, composite);
451:                } else {
452:                    Property composite = new Property(old, property);
453:                    p.propertyPatterns.put(name, composite);
454:                }
455:
456:                // PENDING : Resolve types of getters and setters to pair correctly
457:                // methods with equalNames.
458:                /*
459:                MethodElement getter = pp.getGetterMethod() == null ?
460:                  old.getGetterMethod() : pp.getGetterMethod();
461:                MethodElement setter = pp.getSetterMethod() == null ?
462:                  old.getSetterMethod() : pp.getSetterMethod();
463:
464:                PropertyPattern composite = isIndexed ?
465:                  new IdxPropertyPattern ( getter, setter ) :
466:                  new PropertyPattern( getter, setter );
467:                hm.put( pp.getName(), composite );
468:                 */
469:
470:            }
471:
472:            /** adds an eventSetPattern */
473:
474:            private void addEventSet(Parameters p, EventSet eventSet) {
475:                String key = eventSet.name
476:                        + p.ci.getTypes().asElement(eventSet.type)
477:                                .getSimpleName();
478:                EventSet old = p.eventSetPatterns.get(key);
479:
480:                if (old == null) {
481:                    p.eventSetPatterns.put(key, eventSet);
482:                    return;
483:                }
484:
485:                EventSet composite = new EventSet(old, eventSet);
486:                p.eventSetPatterns.put(key, composite);
487:            }
488:
489:            private static class Parameters {
490:
491:                private CompilationInfo ci;
492:                private TypeElement element;
493:                private HashMap<String, Property> propertyPatterns;
494:                private HashMap<String, IdxProperty> idxPropertyPatterns;
495:                private HashMap<String, EventSet> eventSetPatterns;
496:
497:                Parameters(CompilationInfo ci, TypeElement element) {
498:                    this .ci = ci;
499:                    this .element = element;
500:                    propertyPatterns = new HashMap<String, Property>();
501:                    idxPropertyPatterns = new HashMap<String, IdxProperty>();
502:                    eventSetPatterns = new HashMap<String, EventSet>();
503:                }
504:
505:            }
506:
507:            //    // XXX can be replaced with ClassDefinition.isSubTypeOf()
508:            //    static boolean isSubclass(ClassDefinition a, ClassDefinition b) {
509:            //
510:            //        if (a == null || b == null) {
511:            //            return false;
512:            //        }
513:            //        assert JMIUtils.isInsideTrans();
514:            //        
515:            //        return a.isSubTypeOf(b);
516:            //
517:            //    }
518:
519:            private void resolveChangesOfProperties(Parameters p)
520:                    throws IntrospectionException {
521:                currentPropertyPatterns = new ArrayList<PropertyPattern>(
522:                        p.propertyPatterns.size());
523:                for (Property property : p.propertyPatterns.values()) {
524:                    currentPropertyPatterns.add(property.createPattern(this ));
525:                }
526:                Collections.sort(currentPropertyPatterns,
527:                        Pattern.NAME_COMPARATOR);
528:                // currentPropertyPatterns = resolveChanges( currentPropertyPatterns, propertyPatterns, LevelComparator.PROPERTY );
529:            }
530:
531:            private void resolveChangesOfIdxProperties(Parameters p)
532:                    throws IntrospectionException {
533:                currentIdxPropertyPatterns = new ArrayList<IdxPropertyPattern>(
534:                        p.idxPropertyPatterns.size());
535:                for (IdxProperty property : p.idxPropertyPatterns.values()) {
536:                    currentIdxPropertyPatterns
537:                            .add(property.createPattern(this ));
538:                }
539:                Collections.sort(currentIdxPropertyPatterns,
540:                        Pattern.NAME_COMPARATOR);
541:                // currentIdxPropertyPatterns = resolveChanges( currentIdxPropertyPatterns, idxPropertyPatterns, LevelComparator.IDX_PROPERTY );
542:            }
543:
544:            private void resolveChangesOfEventSets(Parameters p) {
545:                currentEventSetPatterns = new ArrayList<EventSetPattern>(
546:                        p.eventSetPatterns.size());
547:                for (EventSet property : p.eventSetPatterns.values()) {
548:                    currentEventSetPatterns.add(property.createPattern(this ));
549:                }
550:                Collections.sort(currentEventSetPatterns,
551:                        Pattern.NAME_COMPARATOR);
552:                // currentEventSetPatterns = resolveChanges( currentEventSetPatterns, eventSetPatterns, LevelComparator.EVENT_SET );
553:            }
554:            //
555:            //
556:            //    static ArrayList resolveChanges( Collection current, Map created, LevelComparator comparator ) throws JmiException {
557:            //        JMIUtils.isInsideTrans();
558:            //        ArrayList old = new ArrayList( current );
559:            //        ArrayList cre = new ArrayList( created.size() );
560:            //        cre.addAll( created.values() );
561:            //        ArrayList result = new ArrayList( created.size() + 5 );
562:            //
563:            //
564:            //        for ( int level = 0; level <= comparator.getLevels(); level ++ ) {
565:            //            Iterator itCre = cre.iterator();
566:            //            while ( itCre.hasNext() ) {
567:            //                Pattern crePattern = (Pattern) itCre.next();
568:            //                Iterator itOld = old.iterator();
569:            //                while ( itOld.hasNext() ) {
570:            //                    Pattern oldPattern = (Pattern) itOld.next();
571:            //                    if ( comparator.compare( level, oldPattern, crePattern ) ) {
572:            //                        itOld.remove( );
573:            //                        itCre.remove( );
574:            //                        comparator.copyProperties(oldPattern, crePattern );
575:            //                        result.add( oldPattern );
576:            //                        break;
577:            //                    }
578:            //                }
579:            //            }
580:            //        }
581:            //        result.addAll( cre );
582:            //        return result;
583:            //    }
584:
585:            // Inner Classes --- comparators for patterns -------------------------------------------------
586:
587:            //    abstract static class LevelComparator {
588:            //
589:            //        abstract boolean compare( int level, Pattern p1, Pattern p2 );
590:            //        abstract int getLevels();
591:            //        abstract void copyProperties( Pattern p1, Pattern p2 );
592:            //
593:            //        static LevelComparator PROPERTY = new LevelComparator.Property();
594:            //        static LevelComparator IDX_PROPERTY = new LevelComparator.IdxProperty();
595:            //        static LevelComparator EVENT_SET = new LevelComparator.EventSet();
596:            //
597:            //        static class Property extends LevelComparator {
598:            //
599:            //            boolean compare( int level, Pattern p1, Pattern p2 ) {
600:            //
601:            //                switch ( level ) {
602:            //                case 0:
603:            //                    return ((PropertyPattern)p1).getGetterMethod() == ((PropertyPattern)p2).getGetterMethod() &&
604:            //                           ((PropertyPattern)p1).getSetterMethod() == ((PropertyPattern)p2).getSetterMethod() ;
605:            //                case 1:
606:            //                    return ((PropertyPattern)p1).getGetterMethod() == ((PropertyPattern)p2).getGetterMethod();
607:            //                case 2:
608:            //                    return ((PropertyPattern)p1).getSetterMethod() == ((PropertyPattern)p2).getSetterMethod();
609:            //                default:
610:            //                    return false;
611:            //                }
612:            //            }
613:            //
614:            //            int getLevels() {
615:            //                return 2;
616:            //            }
617:            //
618:            //            void copyProperties( Pattern p1, Pattern p2 ) {
619:            //                ((PropertyPattern) p1).copyProperties( (PropertyPattern)p2 );
620:            //            }
621:            //        }
622:            //
623:            //        static class IdxProperty extends LevelComparator {
624:            //
625:            //            boolean compare( int level, Pattern p1, Pattern p2 ) {
626:            //
627:            //                switch ( level ) {
628:            //                case 0:
629:            //                    return ((IdxPropertyPattern)p1).getIndexedGetterMethod() == ((IdxPropertyPattern)p2).getIndexedGetterMethod() &&
630:            //                           ((IdxPropertyPattern)p1).getIndexedSetterMethod() == ((IdxPropertyPattern)p2).getIndexedSetterMethod() ;
631:            //                case 1:
632:            //                    return ((IdxPropertyPattern)p1).getIndexedGetterMethod() == ((IdxPropertyPattern)p2).getIndexedGetterMethod();
633:            //                case 2:
634:            //                    return ((IdxPropertyPattern)p1).getIndexedSetterMethod() == ((IdxPropertyPattern)p2).getIndexedSetterMethod();
635:            //                default:
636:            //                    return false;
637:            //                }
638:            //            }
639:            //
640:            //            int getLevels() {
641:            //                return 2;
642:            //            }
643:            //
644:            //            void copyProperties( Pattern p1, Pattern p2 ) {
645:            //                ((IdxPropertyPattern) p1).copyProperties( (IdxPropertyPattern)p2 );
646:            //            }
647:            //        }
648:            //
649:            //        static class EventSet extends LevelComparator {
650:            //
651:            //            boolean compare( int level, Pattern p1, Pattern p2 ) {
652:            //
653:            //                switch ( level ) {
654:            //                case 0:
655:            //                    return ((EventSetPattern)p1).getAddListenerMethod() == ((EventSetPattern)p2).getAddListenerMethod() ||
656:            //                           ((EventSetPattern)p1).getRemoveListenerMethod() == ((EventSetPattern)p2).getRemoveListenerMethod() ;
657:            //                    /*
658:            //                    case 1:  
659:            //                      return ((EventSetPattern)p1).getAddListenerMethod() == ((EventSetPattern)p2).getAddListenerMethod();
660:            //                    case 2: 
661:            //                      return ((EventSetPattern)p1).getRemoveListenerMethod() == ((EventSetPattern)p2).getRemoveListenerMethod();
662:            //                    */
663:            //                default:
664:            //                    return false;
665:            //                }
666:            //            }
667:            //
668:            //            int getLevels() {
669:            //                return 0;
670:            //            }
671:            //
672:            //            void copyProperties( Pattern p1, Pattern p2 ) {
673:            //                ((EventSetPattern) p1).copyProperties( (EventSetPattern)p2 );
674:            //            }
675:            //        }
676:            //    }
677:
678:            //    public static FileObject fileObjectForElement( Element element ) {
679:            //        return JavaMetamodel.getManager().getFileObject(element.getResource());
680:            //    }
681:            //    
682:            //    public static JavaClass findClassElement( String name, Pattern pattern ) {
683:            //        return pattern.patternAnalyser.findClassElement( name );
684:            //    }
685:            //
686:            //
687:            //    public FileObject findFileObject () {
688:            //        return fileObjectForElement( referenceClassElement != null ? referenceClassElement : classElementHandle );
689:            //    }
690:            //
691:            //    JavaClass findClassElement( String name ) {
692:            //        Type t = findType(name);
693:            //        if (t instanceof JavaClass) {
694:            //            return (JavaClass) t;
695:            //        } else {
696:            //            return null;
697:            //        }
698:            //    }
699:            //    
700:            //    Type findType(String name) throws JmiException {
701:            //        assert JMIUtils.isInsideTrans();
702:            //        JavaClass jc = referenceClassElement != null? referenceClassElement: classElementHandle;
703:            //        Type t = null;
704:            //        if (jc.isValid()) {
705:            //            t = JavaMetamodel.getManager().getJavaExtent(jc).getType().resolve(name);
706:            //        }
707:            //
708:            //        return t;
709:            //    }
710:            //
711:
712:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.