Source Code Cross Referenced for ClassScope.java in  » Code-Analyzer » pmd-4.2rc1 » net » sourceforge » pmd » symboltable » 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 » pmd 4.2rc1 » net.sourceforge.pmd.symboltable 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003:         */package net.sourceforge.pmd.symboltable;
004:
005:        import net.sourceforge.pmd.ast.ASTName;
006:        import net.sourceforge.pmd.ast.SimpleNode;
007:        import net.sourceforge.pmd.util.Applier;
008:
009:        import java.util.ArrayList;
010:        import java.util.HashMap;
011:        import java.util.List;
012:        import java.util.Map;
013:
014:        public class ClassScope extends AbstractScope {
015:
016:            protected Map<ClassNameDeclaration, List<NameOccurrence>> classNames = new HashMap<ClassNameDeclaration, List<NameOccurrence>>();
017:            protected Map<MethodNameDeclaration, List<NameOccurrence>> methodNames = new HashMap<MethodNameDeclaration, List<NameOccurrence>>();
018:            protected Map<VariableNameDeclaration, List<NameOccurrence>> variableNames = new HashMap<VariableNameDeclaration, List<NameOccurrence>>();
019:
020:            // FIXME - this breaks given sufficiently nested code
021:            private static ThreadLocal<Integer> anonymousInnerClassCounter = new ThreadLocal<Integer>() {
022:                protected Integer initialValue() {
023:                    return Integer.valueOf(1);
024:                }
025:            };
026:
027:            private String className;
028:
029:            public ClassScope(String className) {
030:                this .className = className;
031:                anonymousInnerClassCounter.set(Integer.valueOf(1));
032:            }
033:
034:            /**
035:             * This is only for anonymous inner classes
036:             * <p/>
037:             * FIXME - should have name like Foo$1, not Anonymous$1
038:             * to get this working right, the parent scope needs
039:             * to be passed in when instantiating a ClassScope
040:             */
041:            public ClassScope() {
042:                //this.className = getParent().getEnclosingClassScope().getClassName() + "$" + String.valueOf(anonymousInnerClassCounter);
043:                int v = anonymousInnerClassCounter.get().intValue();
044:                this .className = "Anonymous$" + v;
045:                anonymousInnerClassCounter.set(v + 1);
046:            }
047:
048:            public void addDeclaration(VariableNameDeclaration variableDecl) {
049:                if (variableNames.containsKey(variableDecl)) {
050:                    throw new RuntimeException(variableDecl
051:                            + " is already in the symbol table");
052:                }
053:                variableNames
054:                        .put(variableDecl, new ArrayList<NameOccurrence>());
055:            }
056:
057:            public NameDeclaration addVariableNameOccurrence(
058:                    NameOccurrence occurrence) {
059:                NameDeclaration decl = findVariableHere(occurrence);
060:                if (decl != null
061:                        && occurrence.isMethodOrConstructorInvocation()) {
062:                    List<NameOccurrence> nameOccurrences = methodNames
063:                            .get(decl);
064:                    if (nameOccurrences == null) {
065:                        // TODO may be a class name: Foo.this.super();
066:                    } else {
067:                        nameOccurrences.add(occurrence);
068:                        SimpleNode n = occurrence.getLocation();
069:                        if (n instanceof  ASTName) {
070:                            ((ASTName) n).setNameDeclaration(decl);
071:                        } // TODO what to do with PrimarySuffix case?
072:                    }
073:
074:                } else if (decl != null && !occurrence.isThisOrSuper()) {
075:                    List<NameOccurrence> nameOccurrences = variableNames
076:                            .get(decl);
077:                    if (nameOccurrences == null) {
078:                        // TODO may be a class name
079:                    } else {
080:                        nameOccurrences.add(occurrence);
081:                        SimpleNode n = occurrence.getLocation();
082:                        if (n instanceof  ASTName) {
083:                            ((ASTName) n).setNameDeclaration(decl);
084:                        } // TODO what to do with PrimarySuffix case?
085:                    }
086:                }
087:                return decl;
088:            }
089:
090:            public Map<VariableNameDeclaration, List<NameOccurrence>> getVariableDeclarations() {
091:                VariableUsageFinderFunction f = new VariableUsageFinderFunction(
092:                        variableNames);
093:                Applier.apply(f, variableNames.keySet().iterator());
094:                return f.getUsed();
095:            }
096:
097:            public Map<MethodNameDeclaration, List<NameOccurrence>> getMethodDeclarations() {
098:                return methodNames;
099:            }
100:
101:            public Map<ClassNameDeclaration, List<NameOccurrence>> getClassDeclarations() {
102:                return classNames;
103:            }
104:
105:            public ClassScope getEnclosingClassScope() {
106:                return this ;
107:            }
108:
109:            public String getClassName() {
110:                return this .className;
111:            }
112:
113:            public void addDeclaration(MethodNameDeclaration decl) {
114:                methodNames.put(decl, new ArrayList<NameOccurrence>());
115:            }
116:
117:            public void addDeclaration(ClassNameDeclaration decl) {
118:                classNames.put(decl, new ArrayList<NameOccurrence>());
119:            }
120:
121:            protected NameDeclaration findVariableHere(NameOccurrence occurrence) {
122:                if (occurrence.isThisOrSuper()
123:                        || occurrence.getImage().equals(className)) {
124:                    if (variableNames.isEmpty() && methodNames.isEmpty()) {
125:                        // this could happen if you do this:
126:                        // public class Foo {
127:                        //  private String x = super.toString();
128:                        // }
129:                        return null;
130:                    }
131:                    // return any name declaration, since all we really want is to get the scope
132:                    // for example, if there's a
133:                    // public class Foo {
134:                    //  private static final int X = 2;
135:                    //  private int y = Foo.X;
136:                    // }
137:                    // we'll look up Foo just to get a handle to the class scope
138:                    // and then we'll look up X.
139:                    if (!variableNames.isEmpty()) {
140:                        return variableNames.keySet().iterator().next();
141:                    }
142:                    return methodNames.keySet().iterator().next();
143:                }
144:
145:                if (occurrence.isMethodOrConstructorInvocation()) {
146:                    for (MethodNameDeclaration mnd : methodNames.keySet()) {
147:                        if (mnd.getImage().equals(occurrence.getImage())) {
148:                            int args = occurrence.getArgumentCount();
149:                            if (args == mnd.getParameterCount()) {
150:                                // FIXME if several methods have the same name
151:                                // and parameter count, only one will get caught here
152:                                // we need to make some attempt at type lookup and discrimination
153:                                // or, failing that, mark this as a usage of all those methods
154:                                return mnd;
155:                            }
156:                        }
157:                    }
158:                    return null;
159:                }
160:
161:                List<String> images = new ArrayList<String>();
162:                images.add(occurrence.getImage());
163:                if (occurrence.getImage().startsWith(className)) {
164:                    images.add(clipClassName(occurrence.getImage()));
165:                }
166:                ImageFinderFunction finder = new ImageFinderFunction(images);
167:                Applier.apply(finder, variableNames.keySet().iterator());
168:                return finder.getDecl();
169:            }
170:
171:            public String toString() {
172:                String res = "ClassScope (" + className + "): ";
173:                if (!classNames.isEmpty())
174:                    res += "(" + glomNames(classNames.keySet()) + ")";
175:                if (!methodNames.isEmpty()) {
176:                    for (MethodNameDeclaration mnd : methodNames.keySet()) {
177:                        res += mnd.toString();
178:                        int usages = methodNames.get(mnd).size();
179:                        res += "(begins at line "
180:                                + mnd.getNode().getBeginLine() + ", " + usages
181:                                + " usages)";
182:                        res += ",";
183:                    }
184:                }
185:                if (!variableNames.isEmpty())
186:                    res += "(" + glomNames(variableNames.keySet()) + ")";
187:                return res;
188:            }
189:
190:            private String clipClassName(String in) {
191:                return in.substring(in.indexOf('.') + 1);
192:            }
193:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.