EMMA Coverage Report (generated Wed Aug 29 00:03:59 CDT 2007)
[all classes][org.fuwjax.jon.type]

COVERAGE SUMMARY FOR SOURCE FILE [FieldWrapper.java]

nameclass, %method, %block, %line, %
FieldWrapper.java100% (2/2)100% (10/10)89%  (100/112)87%  (28.8/33)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class FieldWrapper100% (1/1)100% (8/8)88%  (88/100)85%  (23.8/28)
setFieldValue (Object, Field, Object): void 100% (1/1)38%  (6/16)43%  (3/7)
isSynthetic (Field): boolean 100% (1/1)83%  (10/12)83%  (0.8/1)
FieldWrapper (Field, IndirectType): void 100% (1/1)100% (18/18)100% (6/6)
getName (): String 100% (1/1)100% (4/4)100% (1/1)
getType (): IndirectType 100% (1/1)100% (3/3)100% (1/1)
getValue (Object): Object 100% (1/1)100% (15/15)100% (5/5)
isTransient (Field): boolean 100% (1/1)100% (16/16)100% (2/2)
setValue (Object, Object): void 100% (1/1)100% (16/16)100% (5/5)
     
class FieldWrapper$AccessibleAction100% (1/1)100% (2/2)100% (12/12)100% (5/5)
FieldWrapper$AccessibleAction (Field): void 100% (1/1)100% (6/6)100% (3/3)
run (): Object 100% (1/1)100% (6/6)100% (2/2)

1/*
2 * This file is part of JON.
3 *
4 * JON is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * JON is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 * 
17 * Copyright 2007 Michael Doberenz
18 */
19package org.fuwjax.jon.type;
20 
21import java.lang.reflect.Field;
22import java.lang.reflect.Modifier;
23import java.security.AccessController;
24import java.security.PrivilegedAction;
25 
26import org.fuwjax.jon.IndirectType;
27import org.fuwjax.jon.ObjectAccessException;
28import org.fuwjax.jon.Reference;
29 
30/**
31 * An entry accessor for a field.
32 * @author michaeldoberenz
33 */
34public class FieldWrapper{
35        private static final String THIS = "this"; //$NON-NLS-1$
36        private Field field;
37        private IndirectType type;
38 
39        /**
40         * Constructs a new entry proxy for <code>field</code> of type
41         * <code>type</code>.
42         * @param field the field to wrap with this entry
43         * @param type the type of the field value
44         */
45        public FieldWrapper(final Field field, final IndirectType type){
46                this.field = field;
47                if(!field.isAccessible()){
48                        AccessController.doPrivileged(new AccessibleAction(field));
49                }
50                this.type = type;
51        }
52 
53        /**
54         * Returns true if <code>field</code> should not be serialized, false
55         * otherwise.
56         * @param field the field which may be transient
57         * @return true if <code>field</code> is transient, false otherwise.
58         */
59        public static boolean isTransient(final Field field){
60                final int mod = field.getModifiers();
61                return isSynthetic(field) || Modifier.isStatic(mod) || Modifier.isTransient(mod);
62        }
63 
64        private static boolean isSynthetic(final Field field){
65                return field.isSynthetic() && !field.getName().startsWith(THIS);
66        }
67 
68        /**
69         * Returns the type of this field.
70         * @return the type of this field
71         */
72        public IndirectType getType(){
73                return type;
74        }
75 
76        /**
77         * Retrieves the value of this field from <code>container</code>.
78         * @param container the object with a value for this field
79         * @return the value of the field from <code>container</code>
80         * @throws ObjectAccessException if an object relationship cannot be followed
81         */
82        public Object getValue(final Object container) throws ObjectAccessException{
83                try{
84                        return field.get(container);
85                }catch(IllegalArgumentException e){
86                        throw ObjectAccessException.Message.ObjectNotInstanceOfType.exception(e);
87                }catch(IllegalAccessException e){
88                        throw ObjectAccessException.Message.FieldInaccessible.exception(e);
89                }
90        }
91 
92        /**
93         * Returns the name of the field.
94         * @return the name of the field
95         */
96        public String getName(){
97                return field.getName();
98        }
99 
100        /**
101         * Creates a new {@link PrivilegedAction} that sets a supplied {@link Field}
102         * accessible.
103         * @author michaeldoberenz
104         */
105        private static final class AccessibleAction implements PrivilegedAction<Object>{
106                private Field field;
107 
108                /**
109                 * Creates a new privileged action that sets <code>field</code>
110                 * accessible.
111                 * @param field the field to make accessible
112                 */
113                AccessibleAction(final Field field){
114                        this.field = field;
115                }
116 
117                public Object run(){
118                        field.setAccessible(true);
119                        return null; // nothing to return
120                }
121        }
122 
123        /**
124         * Sets the value of this field on <code>container</code> to
125         * <code>value</code>.
126         * @param container the mutated object
127         * @param value the new value of this field on <code>container</code>
128         * @throws ObjectAccessException if an object relationship cannot be restored
129         */
130        public void setValue(final Object container, final Object value) throws ObjectAccessException{
131                if(value instanceof Reference){
132                        ((Reference)value).setField(field, container);
133                }else{
134                        setFieldValue(container, field, value);
135                }
136        }
137 
138        /**
139         * Sets the value of <code>field</code> on <code>container</code> to
140         * <code>value</code>.
141         * @param container the mutated object
142         * @param field the field to modify
143         * @param value the new value of this field on <code>container</code>
144         * @throws ObjectAccessException if an object relationship cannot be restored
145         */
146        public static void setFieldValue(final Object container, final Field field, final Object value)
147              throws ObjectAccessException{
148                try{
149                        field.set(container, value);
150                }catch(IllegalArgumentException e){
151                        throw ObjectAccessException.Message.ObjectNotInstanceOfType.exception(e);
152                }catch(IllegalAccessException e){
153                        throw ObjectAccessException.Message.FieldInaccessible.exception(e);
154                }
155        }
156}

[all classes][org.fuwjax.jon.type]
EMMA 2.0.5312 (C) Vladimir Roubtsov