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: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Alexei Y. Zakharov
021: * @version $Revision: 1.1.2.4 $
022: */package org.apache.harmony.jndi.provider.dns;
023:
024: import java.lang.reflect.Field;
025:
026: /**
027: * Contains several utility methods. Is used from unit tests.
028: * @author Alexei Zakharov
029: * @version $Revision: 1.1.2.4 $
030: */
031: public class TestMgr {
032:
033: /**
034: * Retrieves the field information and set accessible to
035: * <code>true</code>.
036: *
037: * @return retrieved <code>Field</code> object
038: * @throws SecurityException is such operation is prohibited by system
039: * security manager
040: * @throws NoSuchFieldException if the filed with such name has not been
041: * found
042: */
043: static Field getField(Object obj, String fieldName)
044: throws SecurityException, NoSuchFieldException {
045: Field field = obj.getClass().getDeclaredField(fieldName);
046:
047: field.setAccessible(true);
048: return field;
049: }
050:
051: /**
052: * Retrieves the value of an integer field regardless to it's access
053: * modifier.
054: * @param obj object to retrieve the field value from
055: * @param fieldName name of the field
056: * @return retrieved value of the field
057: * @throws SecurityException is such operation is prohibited by system
058: * security manager
059: * @throws NoSuchFieldException if the filed with such name has not been
060: * found
061: */
062: static int getIntField(Object obj, String fieldName)
063: throws SecurityException, NoSuchFieldException,
064: IllegalArgumentException {
065: Field field = getField(obj, fieldName);
066: int n = -1;
067:
068: try {
069: n = field.getInt(obj);
070: } catch (IllegalAccessException e) {
071: // FIXME
072: e.printStackTrace();
073: }
074: return n;
075: }
076:
077: /**
078: * Retrieves the value of a boolean field regardless to it's access
079: * modifier.
080: * @param obj object to retrieve the field value from
081: * @param fieldName name of the field
082: * @return retrieved value of the field
083: * @throws SecurityException is such operation is prohibited by system
084: * security manager
085: * @throws NoSuchFieldException if the filed with such name has not been
086: * found
087: */
088: static boolean getBoolField(Object obj, String fieldName)
089: throws SecurityException, NoSuchFieldException,
090: IllegalArgumentException {
091: Field field = getField(obj, fieldName);
092: boolean b = false;
093:
094: try {
095: b = field.getBoolean(obj);
096: } catch (IllegalAccessException e) {
097: // FIXME
098: e.printStackTrace();
099: }
100: return b;
101: }
102: }
|