001/*
002 *  Copyright 2013 Christopher Pheby
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.jadira.reflection.access.asm;
017
018import java.lang.reflect.Field;
019
020import org.jadira.reflection.access.api.FieldAccess;
021
022/**
023 * FieldAccess implementation using an ASM based access strategy
024 * @param <C> The Class containing the Field to be accessed
025 */
026public class AsmFieldAccess<C> implements FieldAccess<C> {
027
028        private String fieldName;
029        private AsmClassAccess<C> classAccess;
030        private Class<C> declaringClass;
031        private Class<?> fieldClass;
032        private Field field;    
033
034        @SuppressWarnings("unchecked")
035        private AsmFieldAccess(AsmClassAccess<C> classAccess, Field f) {
036                
037                this.declaringClass = (Class<C>) f.getDeclaringClass();
038                
039                this.fieldClass = f.getType();
040                
041                this.classAccess = classAccess;         
042                this.fieldName = f.getName();
043                
044                this.field = f;
045        }
046        
047        @Override
048        public Class<C> declaringClass() {
049                return declaringClass;
050        }
051
052        @Override
053        public Class<?> fieldClass() {
054                return fieldClass;
055        }
056
057        @Override
058        public Field field() {
059                return field;
060        }
061        
062        /**
063         * Get a new instance that can access the given Field
064         * @param classAccess The AsmClassAccess instance to be delegated to
065         * @param f Field to be accessed
066         * @param <C> The type of class
067         * @return New AsmFieldAccess instance
068         */
069        public static final <C> AsmFieldAccess<C> get(AsmClassAccess<C> classAccess, Field f) {
070                return new AsmFieldAccess<C>(classAccess, f);
071        }
072
073        @Override
074        public Object getValue(C parent) {
075                return classAccess.getValue(parent, fieldName);
076        }
077
078        @Override
079        public void putValue(C parent, Object newFieldValue) {
080                classAccess.putValue(parent, fieldName, newFieldValue);
081        }
082
083        @Override
084        public boolean getBooleanValue(C parent) {
085                return classAccess.getBooleanValue(parent, fieldName);
086        }
087
088        @Override
089        public byte getByteValue(C parent) {
090                return classAccess.getByteValue(parent, fieldName);
091        }
092
093        @Override
094        public char getCharValue(C parent) {
095                return classAccess.getCharValue(parent, fieldName);
096        }
097
098        @Override
099        public short getShortValue(C parent) {
100                return classAccess.getShortValue(parent, fieldName);
101        }
102
103        @Override
104        public int getIntValue(C parent) {
105                return classAccess.getIntValue(parent, fieldName);
106        }
107
108        @Override
109        public long getLongValue(C parent) {
110                return classAccess.getLongValue(parent, fieldName);
111        }
112
113        @Override
114        public float getFloatValue(C parent) {
115                return classAccess.getFloatValue(parent, fieldName);
116        }
117
118        @Override
119        public double getDoubleValue(C parent) {
120                return classAccess.getDoubleValue(parent, fieldName);
121        }
122
123        @Override
124        public void putBooleanValue(C parent, boolean newFieldValue) {
125                classAccess.putBooleanValue(parent, fieldName, newFieldValue);
126        }
127
128        @Override
129        public void putByteValue(C parent, byte newFieldValue) {
130                classAccess.putByteValue(parent, fieldName, newFieldValue);
131        }
132
133        @Override
134        public void putCharValue(C parent, char newFieldValue) {
135                classAccess.putCharValue(parent, fieldName, newFieldValue);
136        }
137
138        @Override
139        public void putShortValue(C parent, short newFieldValue) {
140                classAccess.putShortValue(parent, fieldName, newFieldValue);
141        }
142
143        @Override
144        public void putIntValue(C parent, int newFieldValue) {
145                classAccess.putIntValue(parent, fieldName, newFieldValue);
146        }
147
148        @Override
149        public void putLongValue(C parent, long newFieldValue) {
150                classAccess.putLongValue(parent, fieldName, newFieldValue);
151        }
152
153        @Override
154        public void putFloatValue(C parent, float newFieldValue) {
155                classAccess.putFloatValue(parent, fieldName, newFieldValue);
156        }
157
158        @Override
159        public void putDoubleValue(C parent, double newFieldValue) {
160                classAccess.putDoubleValue(parent, fieldName, newFieldValue);
161        }
162}