KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > FinalFieldSetterJdk14


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop;
19
20 import java.lang.reflect.Field JavaDoc;
21
22 /*
23  * FinalFields that are sent across the wire .. how to unmarshall and recreate the object on the
24  * receiving side? We don't want to invoke the constructor since it would establish values for
25  * final fields. We have to recreate the final field exactly like it was on the sender side.
26  *
27  * The sun.misc.Unsafe does this for us.
28  */

29
30 public class FinalFieldSetterJdk14 extends FinalFieldSetter
31 {
32     private final long fieldOffset;
33     private static final sun.misc.Unsafe unsafe; //Only available for Sun's JDK1.4+
34

35     static
36     {
37         sun.misc.Unsafe val = null;
38         try
39         {
40             Class JavaDoc unsafeClass = Class.forName("sun.misc.Unsafe");
41             Field JavaDoc unsafeField = unsafeClass.getDeclaredField("theUnsafe");
42             unsafeField.setAccessible(true);
43             val = (sun.misc.Unsafe)unsafeField.get((java.lang.Object JavaDoc)null);
44         }
45         catch(Throwable JavaDoc e)
46         {
47         }
48         unsafe = val;
49     }
50
51     public FinalFieldSetterJdk14(Field JavaDoc field)
52     {
53         if(unsafe != null)
54         {
55             fieldOffset = unsafe.objectFieldOffset(field);
56         }
57         else
58         {
59             fieldOffset = -1;
60         }
61     }
62
63     public void setBoolean(Object JavaDoc that, boolean value)
64     {
65         unsafe.putBoolean(that, fieldOffset, value);
66     }
67
68     public void setByte(Object JavaDoc that, byte value)
69     {
70         unsafe.putByte(that, fieldOffset, value);
71     }
72
73     public void setChar(Object JavaDoc that, char value)
74     {
75         unsafe.putChar(that, fieldOffset, value);
76     }
77
78     public void setDouble(Object JavaDoc that, double value)
79     {
80         unsafe.putDouble(that, fieldOffset, value);
81     }
82
83     public void setFloat(Object JavaDoc that, float value)
84     {
85         unsafe.putFloat(that, fieldOffset, value);
86     }
87
88     public void setInt(Object JavaDoc that, int value)
89     {
90         unsafe.putInt(that, fieldOffset, value);
91     }
92
93     public void setLong(Object JavaDoc that, long value)
94     {
95         unsafe.putLong(that, fieldOffset, value);
96     }
97
98     public void setShort(Object JavaDoc that, short value)
99     {
100         unsafe.putShort(that, fieldOffset, value);
101     }
102
103     public void set(Object JavaDoc that, Object JavaDoc value)
104     {
105         unsafe.putObject(that, fieldOffset, value);
106     }
107 }
108
Popular Tags