Source Code Cross Referenced for NewClassProcessor.java in  » Code-Analyzer » Spoon » spoon » jdiet » 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 » Code Analyzer » Spoon » spoon.jdiet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package spoon.jdiet;
002:
003:        import java.util.ArrayList;
004:        import java.util.Collection;
005:        import java.util.List;
006:        import java.util.Map;
007:        import java.util.Set;
008:        import java.util.Vector;
009:
010:        import spoon.jdiet.rt.Helper;
011:        import spoon.processing.AbstractProcessor;
012:        import spoon.processing.TraversalStrategy;
013:        import spoon.reflect.code.CtExpression;
014:        import spoon.reflect.code.CtInvocation;
015:        import spoon.reflect.code.CtNewClass;
016:        import spoon.reflect.declaration.CtClass;
017:        import spoon.reflect.factory.CodeFactory;
018:        import spoon.reflect.factory.ExecutableFactory;
019:        import spoon.reflect.factory.TypeFactory;
020:        import spoon.reflect.reference.CtExecutableReference;
021:        import spoon.reflect.reference.CtTypeReference;
022:
023:        /**
024:         * This processor replaces class instantiation with a J2ME compliant equivalent
025:         * instantiation.
026:         * 
027:         * @author Lionel Seinturier <Lionel.Seinturier@lifl.fr>
028:         */
029:        public class NewClassProcessor extends AbstractProcessor<CtNewClass> {
030:
031:            public TraversalStrategy getTraversalStrategy() {
032:                // Let arguments included in the construction be visited first
033:                return TraversalStrategy.POST_ORDER;
034:            }
035:
036:            public void process(CtNewClass nc) {
037:
038:                TypeFactory tf = getFactory().Type();
039:                ExecutableFactory ef = getFactory().Executable();
040:                CodeFactory cf = getFactory().Code();
041:
042:                CtTypeReference ctr = getType(nc);
043:
044:                if (!ctr.isPrimitif()) {
045:
046:                    // Transform new ArrayList(Collection) (or a sub-type) into Helper.newList
047:                    // Do not use List as Vector implements List
048:                    if (ctr.isSubtypeOf(tf.createReference(ArrayList.class))) {
049:                        List<CtExpression<?>> args = nc.getArguments();
050:                        if (args.size() == 1) {
051:                            CtExpression<?> arg = args.get(0);
052:                            if (arg.getType().isSubtypeOf(
053:                                    tf.createReference(Collection.class))) {
054:                                CtExecutableReference replmeth = ef
055:                                        .createReference(
056:                                                tf
057:                                                        .createReference(Helper.class),
058:                                                true,
059:                                                "newList",
060:                                                tf
061:                                                        .createReference(Vector.class));
062:                                CtInvocation repl = cf.createInvocation(null,
063:                                        replmeth, args);
064:                                nc.replace(repl);
065:                                return;
066:                            }
067:                        }
068:                    }
069:
070:                    // Transform new Set(Collection) (or a sub-type) into Helper.newSet
071:                    if (ctr.isSubtypeOf(tf.createReference(Set.class))) {
072:                        List<CtExpression<?>> args = nc.getArguments();
073:                        if (args.size() == 1) {
074:                            CtExpression<?> arg = args.get(0);
075:
076:                            if (arg.getType().isSubtypeOf(
077:                                    tf.createReference(Collection.class))) {
078:                                CtExecutableReference replmeth = ef
079:                                        .createReference(
080:                                                tf
081:                                                        .createReference(Helper.class),
082:                                                true,
083:                                                "newSet",
084:                                                tf
085:                                                        .createReference(Vector.class));
086:                                CtInvocation repl = cf.createInvocation(null,
087:                                        replmeth, args);
088:                                nc.replace(repl);
089:                                return;
090:                            }
091:                        }
092:                    }
093:
094:                    /*
095:                     * No else clause the previous branches may not match all
096:                     * conditions. In such a case, we may need to enter the next branch
097:                     * (especially for new Collection(...) where ... is different from
098:                     * Collection.
099:                     */
100:
101:                    // Transform new Collection (or a sub-type) into new Vector
102:                    if (ctr.isSubtypeOf(tf.createReference(Collection.class))) {
103:                        ctr.setSimpleName("Vector");
104:                    }
105:
106:                    // Transform new Map (or a sub-type) into new Hashtable
107:                    else if (ctr.isSubtypeOf(tf.createReference(Map.class))) {
108:                        ctr.setSimpleName("Hashtable");
109:                    }
110:
111:                    // Transform new Throwable(Throwable t) into new Throwable(t.getMessage())
112:                    else if (ctr.isSubtypeOf(tf
113:                            .createReference(Throwable.class))) {
114:                        List<CtExpression<?>> args = nc.getArguments();
115:                        if (args.size() == 1) {
116:                            CtExpression<?> arg = args.get(0);
117:                            if (arg.getType().isSubtypeOf(
118:                                    tf.createReference(Throwable.class))) {
119:                                CtExecutableReference getmessage = ef
120:                                        .createReference(
121:                                                tf
122:                                                        .createReference(Throwable.class),
123:                                                "getMessage");
124:                                CtInvocation repl = cf.createInvocation(arg,
125:                                        getmessage);
126:                                arg.replace(repl);
127:                            }
128:                        }
129:                    }
130:                }
131:            }
132:
133:            /**
134:             * Return the type of a CtNewClass element.
135:             * Deals with anonymous class instanciation
136:             * e.g. new Thread(){ public void run(){} }
137:             * or   new Runnable(){ public void run(){} }
138:             * 
139:             * We don't use the CtTypeReference of the anonymous class which is
140:             * under-defined with spoon (e.g. simpleName == "") but the CtTypeReference
141:             * of the superclass (Thread in the example) or of the superinterface.
142:             */
143:            static CtTypeReference getType(CtNewClass nc) {
144:
145:                CtClass c = nc.getAnonymousClass();
146:                if (c == null) {
147:                    // Not a anonymous class
148:                    return nc.getType();
149:                }
150:
151:                CtTypeReference ctr = c.getSuperclass();
152:                if (ctr != null) {
153:                    return ctr;
154:                }
155:
156:                // No superclass, return the superinterface (only one superinterface)
157:                Set<CtTypeReference> sups = c.getSuperinterfaces();
158:                ctr = sups.iterator().next();
159:                return ctr;
160:            }
161:
162:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.