Source Code Cross Referenced for BcelScopeHelper.java in  » Code-Analyzer » Clirr » net » sf » clirr » core » internal » bcel » 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 » Clirr » net.sf.clirr.core.internal.bcel 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.sf.clirr.core.internal.bcel;
002:
003:        import net.sf.clirr.core.CheckerException;
004:        import net.sf.clirr.core.spi.Scope;
005:
006:        import org.apache.bcel.Constants;
007:        import org.apache.bcel.classfile.Attribute;
008:        import org.apache.bcel.classfile.ConstantClass;
009:        import org.apache.bcel.classfile.ConstantPool;
010:        import org.apache.bcel.classfile.ConstantUtf8;
011:        import org.apache.bcel.classfile.InnerClass;
012:        import org.apache.bcel.classfile.InnerClasses;
013:        import org.apache.bcel.classfile.JavaClass;
014:        import org.apache.bcel.util.Repository;
015:
016:        final class BcelScopeHelper {
017:
018:            /**
019:             * 
020:             *
021:             */
022:            private BcelScopeHelper() {
023:            }
024:
025:            /**
026:             * Get a Scope object representing the accessibility of the specified
027:             * object.
028:             * <p>
029:             * Note that this method gives the wrong results for JavaClass objects
030:             * which are nested classes. Use getClassScope(jclass) instead.
031:             */
032:            public static Scope getScope(int accessFlags) {
033:                if ((accessFlags & Constants.ACC_PUBLIC) > 0) {
034:                    return Scope.PUBLIC;
035:                }
036:
037:                if ((accessFlags & Constants.ACC_PROTECTED) > 0) {
038:                    return Scope.PROTECTED;
039:                }
040:
041:                if ((accessFlags & Constants.ACC_PRIVATE) > 0) {
042:                    return Scope.PRIVATE;
043:                }
044:
045:                return Scope.PACKAGE;
046:            }
047:
048:            /**
049:             * Java class files only ever contain scope specifiers of "public" or
050:             * "package". For top-level classes, this is expected: it is not possible
051:             * to have a top-level protected or private class.
052:             * <p>
053:             * However nested classes <i>can</i> be declared as protected or private. The
054:             * way to tell the real scope of a nested class is to ignore the scope in
055:             * the actual class file itself, and instead look in the "InnerClasses"
056:             * attribute stored on the enclosing class. This is exactly what the java
057:             * compiler does when compiling, and what the jvm does when verifying class
058:             * linkage at runtime.
059:             * <p>
060:             * For a "top-level" class, this method just returns the access scope for
061:             * the class itself. For nested classes, the enclosing class of the
062:             * specified class is retrieved and its InnerClasses attribute checked to
063:             * find the true scope for the specified class.
064:             * <p>
065:             * @throws CheckerException if the specified class is a nested class and
066:             * the enclosing class could not be found, or if the supposedly enclosing
067:             * class has no reference to the nested class. This exception is not
068:             * expected to occur in practice, unless a truly screwed-up jar file is
069:             * passed to clirr for inspection.
070:             */
071:            public static Scope getClassScope(JavaClass jclass)
072:                    throws CheckerException {
073:                int dollarPos = jclass.getClassName().lastIndexOf('$');
074:                if (dollarPos == -1) {
075:                    // not a nested class
076:                    return getScope(jclass.getAccessFlags());
077:                }
078:
079:                // ok this is a nested class
080:                String jclassName = jclass.getClassName();
081:                String enclosingClassName = jclassName.substring(0, dollarPos);
082:                Repository repo = jclass.getRepository();
083:                JavaClass enclosingClass = repo.findClass(enclosingClassName);
084:
085:                if (enclosingClass == null) {
086:                    throw new CheckerException(
087:                            "Unable to locate enclosing class "
088:                                    + enclosingClassName + " for nested class "
089:                                    + jclassName);
090:                }
091:
092:                ConstantPool pool = enclosingClass.getConstantPool();
093:                Attribute[] attrs = enclosingClass.getAttributes();
094:                for (int i = 0; i < attrs.length; ++i) {
095:                    if (attrs[i] instanceof  InnerClasses) {
096:                        InnerClasses ics = (InnerClasses) attrs[i];
097:                        InnerClass[] icarray = ics.getInnerClasses();
098:                        for (int j = 0; j < icarray.length; ++j) {
099:                            // in the code below, instanceof checks should not be necessary
100:                            // before casting Constants because the classfile format ensures
101:                            // that instanceof would always be true
102:                            InnerClass ic = icarray[j];
103:                            int classIndex = ic.getInnerClassIndex();
104:                            ConstantClass constClass = (ConstantClass) pool
105:                                    .getConstant(classIndex);
106:                            int nameIndex = constClass.getNameIndex();
107:                            ConstantUtf8 nameconst = (ConstantUtf8) pool
108:                                    .getConstant(nameIndex);
109:                            String classname = nameconst.getBytes().replace(
110:                                    '/', '.');
111:                            if (jclassName.equals(classname)) {
112:                                return getScope(ic.getInnerAccessFlags());
113:                            }
114:                        }
115:                    }
116:                }
117:
118:                // weird; no nested class info found
119:                throw new CheckerException(
120:                        "Unable to find information in class "
121:                                + enclosingClass.getClassName()
122:                                + " referring back to nested class "
123:                                + jclassName);
124:
125:            }
126:
127:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.