Source Code Cross Referenced for NonPublicMemberAccessor.java in  » Scripting » Pnuts » pnuts » ext » 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 » Scripting » Pnuts » pnuts.ext 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)NonPublicMemberAccessor.java 1.2 04/12/06
003:         *
004:         * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005:         *
006:         * See the file "LICENSE.txt" for information on usage and redistribution
007:         * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008:         */
009:        package pnuts.ext;
010:
011:        import java.lang.reflect.Constructor;
012:        import java.lang.reflect.Field;
013:        import java.lang.reflect.Method;
014:        import java.util.Vector;
015:
016:        import pnuts.lang.Configuration;
017:        import pnuts.lang.Context;
018:        import pnuts.lang.Runtime;
019:        import pnuts.lang.PnutsException;
020:
021:        /**
022:         * when -a option is given to the pnuts command, this class is used so that
023:         * non-public members can be accessed.
024:         */
025:        public class NonPublicMemberAccessor extends PublicMemberAccessor {
026:
027:            public NonPublicMemberAccessor() {
028:            }
029:
030:            public NonPublicMemberAccessor(Configuration conf) {
031:                super (conf);
032:            }
033:
034:            public Method[] getMethods(Class cls) {
035:                Vector vec = new Vector();
036:                Class c = cls;
037:                while (c != null) {
038:                    Method[] declared = c.getDeclaredMethods();
039:                    for (int i = 0; i < declared.length; i++) {
040:                        Method m = declared[i];
041:                        m.setAccessible(true);
042:                        vec.addElement(m);
043:                    }
044:                    c = c.getSuperclass();
045:                }
046:                Method[] ret = new Method[vec.size()];
047:                vec.copyInto(ret);
048:                return ret;
049:            }
050:
051:            public Constructor[] getConstructors(Class cls) {
052:                Vector vec = new Vector();
053:                Class c = cls;
054:                while (c != null) {
055:                    Constructor[] declared = c.getDeclaredConstructors();
056:                    for (int i = 0; i < declared.length; i++) {
057:                        Constructor cons = declared[i];
058:                        cons.setAccessible(true);
059:                        vec.addElement(cons);
060:                    }
061:                    c = c.getSuperclass();
062:                }
063:                Constructor[] ret = new Constructor[vec.size()];
064:                vec.copyInto(ret);
065:                return ret;
066:            }
067:
068:            protected Field getField(final Class cls, final String name)
069:                    throws NoSuchFieldException {
070:                Class c = cls;
071:                while (c != null) {
072:                    Field fields[] = c.getDeclaredFields();
073:                    for (int i = 0; i < fields.length; i++) {
074:                        Field f = fields[i];
075:                        if (f.getName().equals(name)) {
076:                            f.setAccessible(true);
077:                            return f;
078:                        }
079:                    }
080:                    c = c.getSuperclass();
081:                }
082:                throw new NoSuchFieldException(name);
083:            }
084:
085:            /**
086:             * Gets a field value of the target object.
087:             * 
088:             * @param context
089:             *            the context in which the field is read
090:             * @param target
091:             *            the target object
092:             * @param name
093:             *            the field name
094:             * @return the field value
095:             */
096:            public Object getField(Context context, Object target, String name) {
097:                return getObjectField(context, target, name);
098:            }
099:
100:            /**
101:             * Sets a field value of the specified object.
102:             * 
103:             * @param context
104:             *            the context in which the field is written.
105:             * @param target
106:             *            the target object
107:             * @param name
108:             *            the field name
109:             * @param value
110:             *            the field value
111:             */
112:            public void putField(Context context, Object target, String name,
113:                    Object value) {
114:                putObjectField(context, target, name, value);
115:            }
116:
117:            protected Object getObjectField(Context context, Object target,
118:                    String name) {
119:                try {
120:                    return getField(target.getClass(), name).get(target);
121:                } catch (NoSuchFieldException e1) {
122:                    if (target instanceof  Class) {
123:                        return getStaticField(context, (Class) target, name);
124:                    }
125:                    throw new PnutsException("field.notFound", new Object[] {
126:                            name, target.getClass() }, context);
127:                } catch (PnutsException pe) {
128:                    throw pe;
129:                } catch (Exception e) {
130:                    throw new PnutsException(e, context);
131:                }
132:            }
133:
134:            protected void putObjectField(Context context, Object target,
135:                    String name, Object value) {
136:                try {
137:                    Field field = getField(target.getClass(), name);
138:                    Class type = field.getType();
139:                    if (Runtime.isArray(value) && type.isArray()) {
140:                        if (!type.isInstance(value)) {
141:                            value = Runtime.transform(type, value, null);
142:                        }
143:                    }
144:                    field.set(target, value);
145:                } catch (NoSuchFieldException e1) {
146:                    throw new PnutsException("field.notFound", new Object[] {
147:                            name, target.getClass() }, context);
148:                } catch (PnutsException pe) {
149:                    throw pe;
150:                } catch (Exception e) {
151:                    throw new PnutsException(e, context);
152:                }
153:            }
154:
155:            public Object callMethod(Context context, Class c, String name,
156:                    Object args[], Class types[], Object target) {
157:                return invokeMethod(context, c, name, args, types, target);
158:            }
159:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.