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.kernel.vm;
019:
020: import java.lang.reflect.Field;
021: import java.security.AccessController;
022: import java.security.PrivilegedAction;
023:
024: /**
025: * <p>
026: * This class must be implemented by the VM to support Object manipulation.
027: * </p>
028: */
029: public class Objects {
030: private static final Objects INSTANCE = new Objects();
031:
032: /**
033: * <p>
034: * Retrieves an instance of the Objects service.
035: * </p>
036: *
037: * @return An instance of Objects.
038: */
039: public static Objects getInstance() {
040: // TODO add class loader check
041: return AccessController
042: .doPrivileged(new PrivilegedAction<Objects>() {
043: public Objects run() {
044: return INSTANCE;
045: }
046: });
047: }
048:
049: private Objects() {
050: super ();
051: }
052:
053: /**
054: * <p>
055: * Retrieves the offset value of the {@link Field} for use by other methods
056: * in this class.
057: * </p>
058: *
059: * @param field The {@link Field} to retrieve the offset for.
060: * @return The offset value.
061: */
062: public long getFieldOffset(Field field) {
063: return 0L;
064: }
065:
066: /**
067: * <p>
068: * Retrieves the base offset for the array Class given. The Class passed
069: * MUST me an array type, such that the method {@link Class#isArray()}
070: * returns <code>true</code>. For example, <code>int[].class</code>.
071: * </p>
072: *
073: * @param clazz The array Class object.
074: * @return The base offset value.
075: * @throws NullPointerException if <code>clazz</code> is <code>null</code>.
076: * @throws IllegalArgumentException if <code>clazz</code> is not an array type.
077: */
078: public int getArrayBaseOffset(Class<?> clazz) {
079: if (!clazz.isArray()) {
080: throw new IllegalArgumentException();
081: }
082: return 0;
083: }
084:
085: /**
086: * <p>
087: * Retrieves the array index scale for the array Class given. The index
088: * scale is the value used to determine the offset of a particular element
089: * in the array given the array's base offset and an index. The following
090: * code snippet illustrates the usage.
091: * </p>
092: *
093: * <pre>
094: * int base = Objects.getArrayBaseOffset(int[].class);
095: *
096: * int scale = Objects.getArrayIndexScale(int[].class);
097: *
098: * int elementIdx = 1;
099: *
100: * int[] array = { 0, 1, 2 };
101: *
102: * long offsetForIdx = base + (elementIdx * scale);
103: * </pre>
104: *
105: * <p>
106: * The Class passed MUST me an array type, such that the method
107: * {@link Class#isArray()} returns <code>true</code>. For example,
108: * <code>int[].class</code>.
109: * </p>
110: *
111: * @param clazz The array Class object.
112: * @return The index scale value.
113: * @throws NullPointerException if <code>clazz</code> is <code>null</code>.
114: * @throws IllegalArgumentException if <code>clazz</code> is not an array type.
115: */
116: public int getArrayIndexScale(Class<?> clazz) {
117: if (!clazz.isArray()) {
118: throw new IllegalArgumentException();
119: }
120: return 0;
121: }
122:
123: /**
124: * <p>
125: * Compares and swaps the value of an int-typed field on an Object instance.
126: * </p>
127: *
128: * @param object The instance containing the field.
129: * @param fieldOffset The offset value of the field.
130: * @param expected The expected value of the field.
131: * @param update The new value to write to the field.
132: * @return <code>true</code> if the field was updated, <code>false</code>
133: * otherwise.
134: */
135: public boolean compareAndSwapInt(Object object, long fieldOffset,
136: int expected, int update) {
137: return false;
138: }
139:
140: /**
141: * <p>
142: * Compares and swaps the value of a long-typed field on an Object instance.
143: * </p>
144: *
145: * @param object The instance containing the field.
146: * @param fieldOffset The offset value of the field.
147: * @param expected The expected value of the field.
148: * @param update The new value to write to the field.
149: * @return <code>true</code> if the field was updated, <code>false</code>
150: * otherwise.
151: */
152: public boolean compareAndSwapLong(Object object, long fieldOffset,
153: long expected, long update) {
154: return false;
155: }
156:
157: /**
158: * <p>
159: * Compares and swaps the value of an Object-typed field on an Object
160: * instance.
161: * </p>
162: *
163: * @param object The instance containing the field.
164: * @param fieldOffset The offset value of the field.
165: * @param expected The expected value of the field.
166: * @param update The new value to write to the field.
167: * @return <code>true</code> if the field was updated, <code>false</code>
168: * otherwise.
169: */
170: public boolean compareAndSwapObject(Object object,
171: long fieldOffset, Object expected, Object update) {
172: return false;
173: }
174:
175: /**
176: * <p>
177: * Writes an int value to an Object's field as though it were declared
178: * <code>volatile</code>.
179: * </p>
180: *
181: * @param object The instance containing the field to write to.
182: * @param fieldOffset The offset of the field to write to.
183: * @param newValue The value to write.
184: */
185: public void putIntVolatile(Object object, long fieldOffset,
186: int newValue) {
187: return;
188: }
189:
190: /**
191: * <p>
192: * Reads an int value from an Object's field as though it were declared
193: * <code>volatile</code>.
194: * </p>
195: *
196: * @param object The instance containing the field to read from.
197: * @param fieldOffset The offset of the field to read from.
198: * @return The value that was read.
199: */
200: public int getIntVolatile(Object object, long fieldOffset) {
201: return 0;
202: }
203:
204: /**
205: * <p>
206: * Writes a long value to an Object's field as though it were declared
207: * <code>volatile</code>.
208: * </p>
209: *
210: * @param object The instance containing the field to write to.
211: * @param fieldOffset The offset of the field to write to.
212: * @param newValue The value to write.
213: */
214: public void putLongVolatile(Object object, long fieldOffset,
215: long newValue) {
216: return;
217: }
218:
219: /**
220: * <p>
221: * Reads a long value from an Object's field as though it were declared
222: * <code>volatile</code>.
223: * </p>
224: *
225: * @param object The instance containing the field to read from.
226: * @param fieldOffset The offset of the field to read from.
227: * @return The value that was read.
228: */
229: public long getLongVolatile(Object object, long fieldOffset) {
230: return 0;
231: }
232:
233: /**
234: * <p>
235: * Writes an Object reference value to an Object's field as though it were
236: * declared <code>volatile</code>.
237: * </p>
238: *
239: * @param object The instance containing the field to write to.
240: * @param fieldOffset The offset of the field to write to.
241: * @param newValue The value to write.
242: */
243: public void putObjectVolatile(Object object, long fieldOffset,
244: Object newValue) {
245: return;
246: }
247:
248: /**
249: * <p>
250: * Writes an int value to an Object's field as though it were declared
251: * <code>volatile</code>.
252: * </p>
253: *
254: * @param object The instance containing the field to write to.
255: * @param fieldOffset The offset of the field to write to.
256: * @param newValue The value to write.
257: */
258: public Object getObjectVolatile(Object object, long fieldOffset) {
259: return null;
260: }
261:
262: /**
263: * <p>
264: * Writes a long value to an Object's field.
265: * </p>
266: *
267: * @param object The instance containing the field to write to.
268: * @param fieldOffset The offset of the field to write to.
269: * @param newValue The value to write.
270: */
271: public void putLong(Object object, long fieldOffset, long newValue) {
272: return;
273: }
274:
275: /**
276: * <p>
277: * Reads a long value from an Object's field.
278: * </p>
279: *
280: * @param object The instance containing the field to read from.
281: * @param fieldOffset The offset of the field to read from.
282: * @return The value that was read.
283: */
284: public long getLong(Object object, long fieldOffset) {
285: return 0L;
286: }
287: }
|