Source Code Cross Referenced for ObjectAccessor.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » misc » accessors » 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 » Apache Harmony Java SE » org package » org.apache.harmony.misc.accessors 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.misc.accessors;
019:
020:        import java.lang.reflect.Constructor;
021:        import java.lang.reflect.Field;
022:        import java.lang.reflect.Member;
023:        import java.lang.reflect.Method;
024:        import java.util.Hashtable;
025:
026:        /**
027:         * Provides the direct access to classes and objects. This class allows to overcome
028:         * the certain limitations of the reflection API, such as setting the constant fields or
029:         * allocating objects without calling its constructor. The following groups of operations
030:         * are supported:
031:         * <ul>
032:         *      <li><b>getField/MethodID,getStaticField/MethodID</b> - used to get ID for methods and fields.
033:         *      <li><b>getXXX/setXXX</b> - used to read and write non-static fields in objects
034:         *          (XXX stands for a field type);
035:         *      <li><b>getStaticXXX/setStaticXXX</b> - used to read and write static fields in classes
036:         *          (XXX stands for a field type);
037:         *      <li><b>invokeStaticXXX</b> - used to call static methods in a class (XXX means return type);
038:         *      <li><b>invokeVirtualXXX</b> - used to call virtual methods for object (XXX means return type);
039:         *      <li><b>invokeNonVirtualXXX</b> - used to call non-virtual methods for
040:         *          the given class and object (XXX means return type);
041:         *      <li><b>allocateObject, newInstance</b> - provides a fine control over object
042:         *      construction;
043:         *      <li><b>hasStaticInitializer</b> - informational methods about class;
044:         *      <li><b>monitorEnter/Exit</b> - enter/exit monitor associated with the given object
045:         * </ul>
046:         * Fields and methods are identified in the class with help of ID's those actual meaning
047:         * is implementation dependent.
048:         * Depending on a platform, ID's may represent the real offets in the physical memory,
049:         * though it is not always guaranteed. Unlike the {@link ArrayAccessor} class, users should not rely on
050:         * any correspondence between ID's and memory address space. However, it is guaranteed that ID's, once
051:         * obtained, are valid during the whole lifetime of the given class and can equally be
052:         * applied for all its instances.
053:         * <p>
054:         * No security checks are made while reading and writing object's fields, as well as while calling object's methods. In addition to
055:         * variables, this class also allows to set the values for constant fields within an object.
056:         * <p>
057:         * For accessing Array objects, please use the {@link ArrayAccessor} class.
058:         *
059:         * @see ArrayAccessor
060:         */
061:        public class ObjectAccessor {
062:
063:            /**
064:             * This class complies to singleton pattern.
065:             */
066:            private static ObjectAccessor instance;
067:
068:            static ObjectAccessor getInstance() {
069:                if (instance == null) {
070:                    System.loadLibrary("accessors"); //$NON-NLS-1$
071:                    instance = new ObjectAccessor();
072:                }
073:                return instance;
074:            }
075:
076:            private ObjectAccessor() {
077:            }
078:
079:            private Hashtable methods = new Hashtable();
080:
081:            //Boolean field access
082:            /**
083:             * Reads a boolean field for the given object.
084:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
085:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
086:             * @param o object those field needs to be read
087:             * @param fieldID field ID.
088:             * @return boolean field value
089:             * @see #setBoolean(Object, long, boolean)
090:             */
091:            public final native boolean getBoolean(Object o, long fieldID);
092:
093:            /**
094:             * Reads a boolean field for the given class.
095:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
096:             * within a class.
097:             * @param c class those field needs to be read
098:             * @param fieldID field ID.
099:             * @return boolean field value
100:             * @see #setStaticBoolean(Class, long, boolean)
101:             */
102:            public final native boolean getStaticBoolean(Class c, long fieldID);
103:
104:            /**
105:             * Writes a boolean field for the given object.
106:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
107:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
108:             * @param o object those field needs to be set.
109:             * @param fieldID field ID
110:             * @param value field value to be set
111:             * @see #getBoolean(Object, long)
112:             */
113:            public final native void setBoolean(Object o, long fieldID,
114:                    boolean value);
115:
116:            /**
117:             * Writes a boolean field for the given class.
118:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
119:             * within a class.
120:             * @param c class those field needs to be read
121:             * @param fieldID field ID
122:             * @param value field value to be set
123:             * @see #getStaticBoolean(Class, long)
124:             */
125:            public final native void setStaticBoolean(Class c, long fieldID,
126:                    boolean value);
127:
128:            //Byte field access
129:            /**
130:             * Reads a byte field for the given object.
131:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
132:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
133:             * @param o object those field needs to be read
134:             * @param fieldID field ID.
135:             * @return byte field value
136:             * @see #setByte(Object, long, byte)
137:             */
138:            public final native byte getByte(Object o, long fieldID);
139:
140:            /**
141:             * Reads a byte field for the given class.
142:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
143:             * within a class.
144:             * @param c class those field needs to be read
145:             * @param fieldID field ID.
146:             * @return byte field value
147:             * @see #setStaticByte(Class, long, byte)
148:             */
149:            public final native byte getStaticByte(Class c, long fieldID);
150:
151:            /**
152:             * Writes a byte field for the given object.
153:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
154:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
155:             * @param o object those field needs to be set.
156:             * @param fieldID field ID
157:             * @param value field value to be set
158:             * @see #getByte(Object, long)
159:             */
160:            public final native void setByte(Object o, long fieldID, byte value);
161:
162:            /**
163:             * Writes a byte field for the given class.
164:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
165:             * within a class.
166:             * @param c class those field needs to be read
167:             * @param fieldID field ID
168:             * @param value field value to be set
169:             * @see #getStaticByte(Class, long)
170:             */
171:            public final native void setStaticByte(Class c, long fieldID,
172:                    byte value);
173:
174:            //Char field access
175:            /**
176:             * Reads a char field for the given object.
177:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
178:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
179:             * @param o object those field needs to be read
180:             * @param fieldID field ID.
181:             * @return char field value
182:             * @see #setChar(Object, long, char)
183:             */
184:            public final native char getChar(Object o, long fieldID);
185:
186:            /**
187:             * Reads a char field for the given class.
188:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
189:             * within a class.
190:             * @param c class those field needs to be read
191:             * @param fieldID field ID.
192:             * @return char field value
193:             * @see #setStaticChar(Class, long, char)
194:             */
195:            public final native char getStaticChar(Class c, long fieldID);
196:
197:            /**
198:             * Writes a char field for the given object.
199:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
200:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
201:             * @param o object those field needs to be set.
202:             * @param fieldID field ID
203:             * @param value field value to be set
204:             * @see #getChar(Object, long)
205:             */
206:            public final native void setChar(Object o, long fieldID, char value);
207:
208:            /**
209:             * Writes a char field for the given class.
210:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
211:             * within a class.
212:             * @param c class those field needs to be read
213:             * @param fieldID field ID
214:             * @param value field value to be set
215:             * @see #getStaticChar(Class, long)
216:             */
217:            public final native void setStaticChar(Class c, long fieldID,
218:                    char value);
219:
220:            //Short field access
221:            /**
222:             * Reads a short field for the given object.
223:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
224:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
225:             * @param o object those field needs to be read
226:             * @param fieldID field ID.
227:             * @return short field value
228:             * @see #setShort(Object, long, short)
229:             */
230:            public final native short getShort(Object o, long fieldID);
231:
232:            /**
233:             * Reads a short field for the given class.
234:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
235:             * within a class.
236:             * @param c class those field needs to be read
237:             * @param fieldID field ID.
238:             * @return short field value
239:             * @see #setStaticShort(Class, long, short)
240:             */
241:            public final native short getStaticShort(Class c, long fieldID);
242:
243:            /**
244:             * Writes a short field for the given object.
245:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
246:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
247:             * @param o object those field needs to be set.
248:             * @param fieldID field ID
249:             * @param value field value to be set
250:             * @see #getShort(Object, long)
251:             */
252:            public final native void setShort(Object o, long fieldID,
253:                    short value);
254:
255:            /**
256:             * Writes a short field for the given class.
257:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
258:             * within a class.
259:             * @param c class those field needs to be read
260:             * @param fieldID field ID
261:             * @param value field value to be set
262:             * @see #getStaticShort(Class, long)
263:             */
264:            public final native void setStaticShort(Class c, long fieldID,
265:                    short value);
266:
267:            //Int field access
268:            /**
269:             * Reads a int field for the given object.
270:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
271:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
272:             * @param o object those field needs to be read
273:             * @param fieldID field ID.
274:             * @return int field value
275:             * @see #setInt(Object, long, int)
276:             */
277:            public final native int getInt(Object o, long fieldID);
278:
279:            /**
280:             * Reads a int field for the given class.
281:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
282:             * within a class.
283:             * @param c class those field needs to be read
284:             * @param fieldID field ID.
285:             * @return int field value
286:             * @see #setStaticInt(Class, long, int)
287:             */
288:            public final native int getStaticInt(Class c, long fieldID);
289:
290:            /**
291:             * Writes a int field for the given object.
292:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
293:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
294:             * @param o object those field needs to be set.
295:             * @param fieldID field ID
296:             * @param value field value to be set
297:             * @see #getInt(Object, long)
298:             */
299:            public final native void setInt(Object o, long fieldID, int value);
300:
301:            /**
302:             * Writes a int field for the given class.
303:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
304:             * within a class.
305:             * @param c class those field needs to be read
306:             * @param fieldID field ID
307:             * @param value field value to be set
308:             * @see #getStaticInt(Class, long)
309:             */
310:            public final native void setStaticInt(Class c, long fieldID,
311:                    int value);
312:
313:            //Long field access
314:            /**
315:             * Reads a long field for the given object.
316:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
317:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
318:             * @param o object those field needs to be read
319:             * @param fieldID field ID.
320:             * @return long field value
321:             * @see #setLong(Object, long, long)
322:             */
323:            public final native long getLong(Object o, long fieldID);
324:
325:            /**
326:             * Reads a long field for the given class.
327:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
328:             * within a class.
329:             * @param c class those field needs to be read
330:             * @param fieldID field ID.
331:             * @return long field value
332:             * @see #setStaticLong(Class, long, long)
333:             */
334:            public final native long getStaticLong(Class c, long fieldID);
335:
336:            /**
337:             * Writes a long field for the given object.
338:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
339:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
340:             * @param o object those field needs to be set.
341:             * @param fieldID field ID
342:             * @param value field value to be set
343:             * @see #getLong(Object, long)
344:             */
345:            public final native void setLong(Object o, long fieldID, long value);
346:
347:            /**
348:             * Writes a long field for the given class.
349:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
350:             * within a class.
351:             * @param c class those field needs to be read
352:             * @param fieldID field ID
353:             * @param value field value to be set
354:             * @see #getStaticLong(Class, long)
355:             */
356:            public final native void setStaticLong(Class c, long fieldID,
357:                    long value);
358:
359:            //Float field access
360:            /**
361:             * Reads a float field for the given object.
362:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
363:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
364:             * @param o object those field needs to be read
365:             * @param fieldID field ID.
366:             * @return float field value
367:             * @see #setFloat(Object, long, float)
368:             */
369:            public final native float getFloat(Object o, long fieldID);
370:
371:            /**
372:             * Reads a float field for the given class.
373:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
374:             * within a class.
375:             * @param c class those field needs to be read
376:             * @param fieldID field ID.
377:             * @return float field value
378:             * @see #setStaticFloat(Class, long, float)
379:             */
380:            public final native float getStaticFloat(Class c, long fieldID);
381:
382:            /**
383:             * Writes a float field for the given object.
384:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
385:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
386:             * @param o object those field needs to be set.
387:             * @param fieldID field ID
388:             * @param value field value to be set
389:             * @see #getFloat(Object, long)
390:             */
391:            public final native void setFloat(Object o, long fieldID,
392:                    float value);
393:
394:            /**
395:             * Writes a float field for the given class.
396:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
397:             * within a class.
398:             * @param c class those field needs to be read
399:             * @param fieldID field ID
400:             * @param value field value to be set
401:             * @see #getStaticFloat(Class, long)
402:             */
403:            public final native void setStaticFloat(Class c, long fieldID,
404:                    float value);
405:
406:            //Double field access
407:            /**
408:             * Reads a double field for the given object.
409:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
410:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
411:             * @param o object those field needs to be read
412:             * @param fieldID field ID.
413:             * @return double field value
414:             * @see #setDouble(Object, long, double)
415:             */
416:            public final native double getDouble(Object o, long fieldID);
417:
418:            /**
419:             * Reads a double field for the given class.
420:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
421:             * within a class.
422:             * @param c class those field needs to be read
423:             * @param fieldID field ID.
424:             * @return double field value
425:             * @see #setStaticDouble(Class, long, double)
426:             */
427:            public final native double getStaticDouble(Class c, long fieldID);
428:
429:            /**
430:             * Writes a double field for the given object.
431:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
432:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
433:             * @param o object those field needs to be set.
434:             * @param fieldID field ID
435:             * @param value field value to be set
436:             * @see #getDouble(Object, long)
437:             */
438:            public final native void setDouble(Object o, long fieldID,
439:                    double value);
440:
441:            /**
442:             * Writes a double field for the given class.
443:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
444:             * within a class.
445:             * @param c class those field needs to be read
446:             * @param fieldID field ID
447:             * @param value field value to be set
448:             * @see #getStaticDouble(Class, long)
449:             */
450:            public final native void setStaticDouble(Class c, long fieldID,
451:                    double value);
452:
453:            //Object field access
454:            /**
455:             * Reads an Object field for the given object.
456:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
457:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
458:             * @param o object those field needs to be read
459:             * @param fieldID field ID.
460:             * @return Object field value
461:             * @see #setObject(Object, long, Object)
462:             */
463:            public final native Object getObject(Object o, long fieldID);
464:
465:            /**
466:             * Reads an Object field for the given class.
467:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
468:             * within a class.
469:             * @param c class those field needs to be read
470:             * @param fieldID field ID.
471:             * @return Object field value
472:             * @see #setStaticObject(Class, long, Object)
473:             */
474:            public final native Object getStaticObject(Class c, long fieldID);
475:
476:            /**
477:             * Writes an Object field for the given object.
478:             * Use the {@link #getFieldID(Field)} method to obtain the ID for the specific field
479:             * within a class. For static fields, use the {@link #getStaticFieldID(Class, String)} method.
480:             * @param o object those field needs to be set.
481:             * @param fieldID field ID
482:             * @param value field value to be set
483:             * @see #getObject(Object, long)
484:             */
485:            public final native void setObject(Object o, long fieldID,
486:                    Object value);
487:
488:            /**
489:             * Writes an Object field for the given class.
490:             * Use the {@link #getStaticFieldID(Class, String)} method to obtain the ID for the specific field
491:             * within a class.
492:             * @param c class those field needs to be read
493:             * @param fieldID field ID
494:             * @param value field value to be set
495:             * @see #getStaticObject(Class, long)
496:             */
497:            public final native void setStaticObject(Class c, long fieldID,
498:                    Object value);
499:
500:            /**
501:             * Returns the ID for a field with the given name.
502:             * For static fields, use the {@link #getStaticFieldID(Class, String)} method.
503:             *
504:             * @param c class containing field
505:             * @param name field name
506:             * @return field ID
507:             * @throws NoSuchFieldError if the field could not be found
508:             */
509:            public final long getFieldID(Class c, String name) {
510:                try {
511:                    return getFieldID(c.getDeclaredField(name));
512:                } catch (SecurityException e) {
513:                    throw new RuntimeException(e);
514:                } catch (NoSuchFieldException e) {
515:                    // Try to find the field in the superclass
516:                    Class sc = c.getSuperclass();
517:                    if (sc == null) {
518:                        throw new NoSuchFieldError(e.getMessage());
519:                    } else {
520:                        return getFieldID(sc, name);
521:                    }
522:                }
523:            }
524:
525:            /**
526:             * Returns the ID for a field with the given name and type.
527:             * This may be faster than getting the field using it's name only.
528:             * For static fields, use the {@link #getStaticFieldID(Class, String)} method.
529:             *
530:             * @param c class containing field
531:             * @param name field name
532:             * @param type field type
533:             * @return field ID
534:             * @throws NoSuchFieldError if the field could not be found
535:             */
536:            public final long getFieldID(Class c, String name, Class type) {
537:                return getFieldID(c, name, getSystemName(type));
538:            }
539:
540:            /**
541:             * Returns the ID for the static field with the given name.
542:             *
543:             * @param c class containing static field
544:             * @param name field name
545:             * @return field ID
546:             * @throws NoSuchFieldError if the field could not be found
547:             */
548:            public final long getStaticFieldID(Class c, String name) {
549:                return getFieldID(c, name);
550:            }
551:
552:            /**
553:             * Returns the ID for the static field with the given name and type.
554:             * This may be faster than getting the field using it's name only.
555:             * @param c class containing static field
556:             * @param name field name
557:             * @param type field type
558:             * @return field ID
559:             * @throws NoSuchFieldError if the field could not be found
560:             */
561:            public final long getStaticFieldID(Class c, String name, Class type) {
562:                return getStaticFieldID(c, name, getSystemName(type));
563:            }
564:
565:            /**
566:             * Returns the ID for the reflected field.
567:             *
568:             * @param f reflected field
569:             * @return field ID
570:             */
571:            public final native long getFieldID(Field f);
572:
573:            /**
574:             * Returns the ID for the reflected static field.
575:             * Default implementation delegates to the
576:             * {@link #getFieldID(Field)} call.
577:             *
578:             * @param f reflected field
579:             * @return field ID
580:             */
581:            public final long getStaticFieldID(Field f) {
582:                return getFieldID(f);
583:            }
584:
585:            /**
586:             * Returns the ID for the specified method or constructor.
587:             * Use class constants for primitive parameter types. For example,
588:             * for <code>byte</code> type use the {@link java.lang.Byte#TYPE} class.
589:             * @param c a class the method belongs to
590:             * @param name method name or <code>null</code> in case of constructor
591:             * @param parameterTypes array of parameter types.
592:             * @return method ID
593:             * @throws NoSuchMethodError if the method could not be found
594:             * @see #invokeVirtualVoid(Object, long, Object[])
595:             * @see #invokeNonVirtualVoid(Class, Object, long, Object[])
596:             * @see #newInstance(Class, long, Object[])
597:             */
598:            public final long getMethodID(Class c, String name,
599:                    Class[] parameterTypes) {
600:                return getMethodID(c, name, parameterTypes, false);
601:            }
602:
603:            private static long getMethodID(Class c, String name,
604:                    Class[] parameterTypes, boolean isStatic) {
605:                try {
606:                    String sig = "("; //$NON-NLS-1$
607:                    for (int i = 0; i < parameterTypes.length; i++) {
608:                        sig += getSystemName(parameterTypes[i]);
609:                    }
610:                    sig += ")"; //$NON-NLS-1$
611:                    if (name == null) {
612:                        name = "<init>"; //$NON-NLS-1$
613:                        sig += "V"; //$NON-NLS-1$
614:                    } else {
615:                        Method m = c.getDeclaredMethod(name, parameterTypes);
616:                        sig += getSystemName(m.getReturnType());
617:                    }
618:                    return isStatic ? getStaticMethodID0(c, name, sig)
619:                            : getMethodID0(c, name, sig);
620:                } catch (SecurityException e) {
621:                    throw new RuntimeException(e);
622:                } catch (NoSuchMethodException e) {
623:                    throw new NoSuchMethodError(e.getMessage());
624:                }
625:            }
626:
627:            /**
628:             * Returns the ID for the specified static method.
629:             * Use class constants for primitive parameter types. For example,
630:             * for <code>byte</code> type use the {@link java.lang.Byte#TYPE} class.
631:             * @param c a class the method belongs to
632:             * @param name method name or <code>null</code> in case of constructor
633:             * @param parameterTypes array of parameter types.
634:             * @return static method ID
635:             * @throws NoSuchMethodError if the method could not be found
636:             * @see #invokeVirtualVoid(Object, long, Object[])
637:             */
638:            public final long getStaticMethodID(Class c, String name,
639:                    Class[] parameterTypes) {
640:                return getMethodID(c, name, parameterTypes, true);
641:            }
642:
643:            /**
644:             * Returns the ID for the reflected constructor.
645:             * @param c reflected constructor
646:             * @return method ID
647:             * @see #invokeVirtualVoid(Object, long, Object[])
648:             * @see #newInstance(Class, long, Object[])
649:             */
650:            public final long getMethodID(Constructor c) {
651:                return getMethodID0(c);
652:            }
653:
654:            /**
655:             * Returns the ID for the reflected method.
656:             * @param m reflected method
657:             * @return method ID
658:             * @see #invokeVirtualVoid(Object, long, Object[])
659:             * @see #invokeNonVirtualVoid(Class, Object, long, Object[])
660:             * @see #invokeStaticVoid(Class, long, Object[])
661:             * @see #newInstance(Class, long, Object[])
662:             */
663:            public final long getMethodID(Method m) {
664:                return getMethodID0(m);
665:            }
666:
667:            private static native long getMethodID0(Class c, String name,
668:                    String sig);
669:
670:            private static native long getStaticMethodID0(Class c, String name,
671:                    String sig);
672:
673:            private static native long getMethodID0(Member m);
674:
675:            private Class[] objectArrayTypes(Object[] args) {
676:                Class[] res = new Class[args.length];
677:                for (int i = 0; i < args.length; i++) {
678:                    res[i] = args[i] == null ? null : args[i].getClass();
679:                }
680:                return res;
681:            }
682:
683:            /**
684:             * Invokes static void method with the given ID without security check.
685:             * Primitive type arguments should be wrapped with appropriate objects.
686:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
687:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the method ID.
688:             * @param c a class where method is defined
689:             * @param methodID method ID
690:             * @param args array of arguments
691:             * @see #getMethodID(Class, String, Class[])
692:             */
693:            public final native void invokeStaticVoid(Class c, long methodID,
694:                    Object[] args);
695:
696:            /**
697:             * Invokes void method or constructor with the given ID without security check.
698:             * Primitive type arguments should be wrapped with appropriate objects.
699:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
700:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the method ID.
701:             * @param methodID method ID
702:             * @param obj an object those method or constructor needs to be called
703:             * @param args array of arguments
704:             * @see #getMethodID(Class, String, Class[])
705:             */
706:            public final native void invokeVirtualVoid(Object obj,
707:                    long methodID, Object[] args);
708:
709:            /**
710:             * Invokes a non-virtual void method or constructor with the given ID without security check.
711:             * Primitive type arguments should be wrapped with appropriate objects.
712:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
713:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the method ID.
714:             * @param c a class where method or constructor is defined
715:             * @param obj an object those method or constructor needs to be called
716:             * @param methodID method ID
717:             * @param args array of arguments
718:             * @see #getMethodID(Class, String, Class[])
719:             */
720:            public final native void invokeNonVirtualVoid(Class c, Object obj,
721:                    long methodID, Object[] args);
722:
723:            /**
724:             * Invokes a static reference-type method with the given ID without security check.
725:             * Primitive type arguments should be wrapped with appropriate objects.
726:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
727:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the method ID.
728:             * @param c a class where method is defined
729:             * @param methodID method ID
730:             * @param args array of arguments
731:             * @see #getMethodID(Class, String, Class[])
732:             */
733:            public final native Object invokeStaticObject(Class c,
734:                    long methodID, Object[] args);
735:
736:            /**
737:             * Invokes reference-type method with the given ID without security check.
738:             * Primitive type arguments should be wrapped with appropriate objects.
739:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
740:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the method ID.
741:             * @param methodID method ID
742:             * @param obj an object those method or constructor needs to be called
743:             * @param args array of arguments
744:             * @see #getMethodID(Class, String, Class[])
745:             */
746:            public final native Object invokeVirtualObject(Object obj,
747:                    long methodID, Object[] args);
748:
749:            /**
750:             * Invokes a non-virtual reference-type method with the given ID without security check.
751:             * Primitive type arguments should be wrapped with appropriate objects.
752:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
753:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the method ID.
754:             * @param c a class where method or constructor is defined
755:             * @param obj an object those method or constructor needs to be called
756:             * @param methodID method ID
757:             * @param args array of arguments
758:             * @see #getMethodID(Class, String, Class[])
759:             */
760:            public final native Object invokeNonVirtualObject(Class c,
761:                    Object obj, long methodID, Object[] args);
762:
763:            /**
764:             * Invokes a static long method with the given ID without security check.
765:             * Primitive type arguments should be wrapped with appropriate objects.
766:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
767:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the method ID.
768:             * @param c a class where method is defined
769:             * @param methodID method ID
770:             * @param args array of arguments
771:             * @see #getMethodID(Class, String, Class[])
772:             */
773:            public final native long invokeStaticLong(Class c, long methodID,
774:                    Object[] args);
775:
776:            /**
777:             * Invokes long method with the given ID without security check.
778:             * Primitive type arguments should be wrapped with appropriate objects.
779:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
780:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the method ID.
781:             * @param methodID method ID
782:             * @param obj an object those method or constructor needs to be called
783:             * @param args array of arguments
784:             * @see #getMethodID(Class, String, Class[])
785:             */
786:            public final native long invokeVirtualLong(Object obj,
787:                    long methodID, Object[] args);
788:
789:            /**
790:             * Invokes a non-virtual long method with the given ID without security check.
791:             * Primitive type arguments should be wrapped with appropriate objects.
792:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
793:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the method ID.
794:             * @param c a class where method or constructor is defined
795:             * @param obj an object those method or constructor needs to be called
796:             * @param methodID method ID
797:             * @param args array of arguments
798:             * @see #getMethodID(Class, String, Class[])
799:             */
800:            public final native long invokeNonVirtualLong(Class c, Object obj,
801:                    long methodID, Object[] args);
802:
803:            /**
804:             * Allocates new object of the given class without calling its constructor.
805:             * Constructor can be called independently with help of {@link #invokeNonVirtualVoid(Class, Object, long,  Object[])} method.
806:             * @param c A class those object needs to be allocated.
807:             * @return allocated object
808:             */
809:            public final native Object allocateObject(Class c);
810:
811:            /**
812:             * Allocates new object of class c and invokes its constructor with the given ID
813:             * and args without security checks.
814:             * Primitive type arguments should be wrapped with appropriate objects.
815:             * For example, byte value should be wrapped with {@link java.lang.Byte#TYPE}.
816:             * Use the {@link #getMethodID(Class, String, Class[])} call to obtain the constructor ID.
817:             * @param methodID method ID
818:             * @param c class those instance needs to be created
819:             * @param args array of arguments
820:             * @return allocated object
821:             */
822:            public final native Object newInstance(Class c, long methodID,
823:                    Object[] args);
824:
825:            /**
826:             * Allocates new object of a class c and invokes noarg constructor without security check.
827:             *
828:             * @param c class those object needs to be created
829:             * @return allocated object
830:             */
831:            public final native Object newInstance(Class c);
832:
833:            /**
834:             * Determines whether the class c has static initializer.
835:             * @return true if class c has static initializer, false otherwise
836:             */
837:            public final native boolean hasStaticInitializer(Class c);
838:
839:            /**
840:             * calls monitorEnter java bytecode command. Acquire object <code>o</code> monitor
841:             * @param o object to lock
842:             */
843:            public native void monitorEnter(Object o);
844:
845:            /**
846:             * calls monitorExit java bytecode command. To free object <code>o</code> monitor
847:             * @param o object to unlock
848:             */
849:            public native void monitorExit(Object o);
850:
851:            private static final String getSystemName(Class cls) {
852:                if (cls == boolean.class) {
853:                    return "Z"; //$NON-NLS-1$
854:                } else if (cls == char.class) {
855:                    return "C"; //$NON-NLS-1$
856:                } else if (cls == byte.class) {
857:                    return "B"; //$NON-NLS-1$
858:                } else if (cls == short.class) {
859:                    return "S"; //$NON-NLS-1$
860:                } else if (cls == int.class) {
861:                    return "I"; //$NON-NLS-1$
862:                } else if (cls == long.class) {
863:                    return "J"; //$NON-NLS-1$
864:                } else if (cls == float.class) {
865:                    return "F"; //$NON-NLS-1$
866:                } else if (cls == double.class) {
867:                    return "D"; //$NON-NLS-1$
868:                } else if (cls == void.class) {
869:                    return "V"; //$NON-NLS-1$
870:                } else { // Object type.
871:                    String className = cls.getName().replace('.', '/');
872:                    // Add reference to non-array reference types.
873:                    return (cls.isArray() ? className : ('L' + className + ';'));
874:                }
875:            }
876:
877:            private final native long getFieldID(Class c, String name,
878:                    String sig);
879:
880:            private final native long getStaticFieldID(Class c, String name,
881:                    String sig);
882:
883:            public final native long getGlobalReference(Object o);
884:
885:            public final native void releaseGlobalReference(long ref);
886:
887:            public final native Object getObjectFromReference(long ref);
888:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.