| 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 | */ |
| 19 | package org.fuwjax.jon.type; |
| 20 | |
| 21 | import java.lang.reflect.AccessibleObject; |
| 22 | import java.lang.reflect.Field; |
| 23 | import java.security.AccessController; |
| 24 | import java.security.PrivilegedAction; |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | |
| 28 | import org.fuwjax.jon.ClassCastStrategy; |
| 29 | import org.fuwjax.jon.IndirectType; |
| 30 | import org.fuwjax.jon.ObjectAccessException; |
| 31 | import org.fuwjax.jon.ReferenceStrategy; |
| 32 | import org.fuwjax.jon.SerialFormatException; |
| 33 | import org.fuwjax.jon.accessor.EntriesAccessor; |
| 34 | import org.fuwjax.jon.accessor.KeyValueReader; |
| 35 | import org.fuwjax.jon.accessor.KeyValueWriter; |
| 36 | import org.fuwjax.jon.accessor.ListReader; |
| 37 | import org.fuwjax.jon.accessor.ListWriter; |
| 38 | import org.fuwjax.jon.accessor.ObjectAccessor; |
| 39 | import org.fuwjax.jon.accessor.Symbol; |
| 40 | |
| 41 | /** |
| 42 | * An abstract type for objects. |
| 43 | * @author michaeldoberenz |
| 44 | */ |
| 45 | public class ObjectType extends AbstractIndirectType implements ObjectAccessor, EntriesAccessor{ |
| 46 | private FieldWrapper[] fields; |
| 47 | private ObjectType parent; |
| 48 | |
| 49 | /** |
| 50 | * Creates an instance. |
| 51 | * @param cls the class backing this type |
| 52 | */ |
| 53 | public ObjectType(final Class<?> cls){ |
| 54 | super(cls); |
| 55 | AccessController.doPrivileged(new AccessibleAction(cls)); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected void init(){ |
| 60 | buildFieldList(); |
| 61 | try{ |
| 62 | parent = (ObjectType)getIndirectType(getType().getSuperclass()); |
| 63 | }catch(ClassCastException e){ |
| 64 | parent = null; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private void buildFieldList(){ |
| 69 | final Field[] refFields = getType().getDeclaredFields(); |
| 70 | final List<FieldWrapper> entryList = new ArrayList<FieldWrapper>(); |
| 71 | for(int i = 0; i < refFields.length; ++i){ |
| 72 | if(FieldWrapper.isTransient(refFields[i])){ |
| 73 | continue; |
| 74 | } |
| 75 | final IndirectType type = getIndirectType(refFields[i].getType()); |
| 76 | final FieldWrapper entry = new FieldWrapper(refFields[i], type); |
| 77 | entryList.add(entry); |
| 78 | } |
| 79 | fields = entryList.toArray(new FieldWrapper[entryList.size()]); |
| 80 | } |
| 81 | |
| 82 | public ReferenceStrategy getReferenceStrategy(){ |
| 83 | return ReferenceStrategy.AssignedReference; |
| 84 | } |
| 85 | |
| 86 | public ClassCastStrategy getClassCastStrategy(){ |
| 87 | return ClassCastStrategy.AlwaysCast; |
| 88 | } |
| 89 | |
| 90 | public Symbol getSymbolSpec(){ |
| 91 | return Symbol.Object; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Creates a new {@link PrivilegedAction} that sets all the fields on a |
| 96 | * supplied {@link Class} accessible. |
| 97 | * @author michaeldoberenz |
| 98 | */ |
| 99 | private static final class AccessibleAction implements PrivilegedAction<Object>{ |
| 100 | private Class<?> cls; |
| 101 | |
| 102 | /** |
| 103 | * Creates a new privileged action that sets all the fields on |
| 104 | * <code>cls</code> accessible. |
| 105 | * @param cls the class whose fields should be made accessible |
| 106 | */ |
| 107 | AccessibleAction(final Class<?> cls){ |
| 108 | this.cls = cls; |
| 109 | } |
| 110 | |
| 111 | public Object run(){ |
| 112 | AccessibleObject.setAccessible(cls.getDeclaredFields(), true); |
| 113 | return null; // nothing to return |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public void writePartitions(final ListWriter writer, final Object object) throws ObjectAccessException, |
| 118 | SerialFormatException{ |
| 119 | ObjectType partition = this; |
| 120 | while(partition != null){ |
| 121 | if(partition.fields.length > 0){ |
| 122 | writer.write(object, partition); |
| 123 | } |
| 124 | partition = partition.parent; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | public void writeEntries(final KeyValueWriter writer, final Object object) throws ObjectAccessException, |
| 129 | SerialFormatException{ |
| 130 | for(FieldWrapper field: fields){ |
| 131 | writer.writeKey(field.getName(), IdentifierType.DEFAULT); |
| 132 | writer.writeValue(field.getValue(object), field.getType()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | public Object createContainer(final Object object) throws ObjectAccessException{ |
| 137 | if(object != null){ |
| 138 | return object; |
| 139 | } |
| 140 | try{ |
| 141 | return NaiveInstantiator.create(getType()); |
| 142 | }catch(ObjectAccessException e){ |
| 143 | return UnsafeInstantiator.create(getType()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | public Object createInstance(final Object container, final Object object){ |
| 148 | return container; |
| 149 | } |
| 150 | |
| 151 | public void readPartitions(final ListReader listReader, final Object container) throws ObjectAccessException, |
| 152 | SerialFormatException{ |
| 153 | ObjectType partition = this; |
| 154 | while(partition != null){ |
| 155 | if(partition.fields.length > 0){ |
| 156 | listReader.read(container, partition); |
| 157 | } |
| 158 | partition = partition.parent; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | private FieldWrapper getField(final String key) throws ObjectAccessException{ |
| 163 | for(FieldWrapper field: fields){ |
| 164 | if(field.getName().equals(key)){ |
| 165 | return field; |
| 166 | } |
| 167 | } |
| 168 | throw ObjectAccessException.Message.FieldNotFound.exception(); |
| 169 | } |
| 170 | |
| 171 | public void readEntries(final KeyValueReader reader, final Object container) throws ObjectAccessException, |
| 172 | SerialFormatException{ |
| 173 | boolean foundEntry; |
| 174 | do{ |
| 175 | foundEntry = readEntry(reader, container); |
| 176 | }while(foundEntry); |
| 177 | } |
| 178 | |
| 179 | private boolean readEntry(final KeyValueReader reader, final Object container) throws ObjectAccessException, |
| 180 | SerialFormatException{ |
| 181 | final int pos = reader.getPosition(); |
| 182 | try{ |
| 183 | final String key = reader.readKey(null, IdentifierType.DEFAULT).toString(); |
| 184 | final FieldWrapper field = getField(key); |
| 185 | final Object value = reader.readValue(field.getValue(container), field.getType()); |
| 186 | field.setValue(container, value); |
| 187 | return true; |
| 188 | }catch(SerialFormatException e){ |
| 189 | e.assertPosition(pos); |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | } |