001    /**
002     * Copyright (C) 2009 Erik Putrycz <erik.putrycz@gmail.com>
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     */
016    
017    package net.ep.db4o.javassist;
018    
019    import java.lang.reflect.Field;
020    import java.lang.reflect.Modifier;
021    
022    import com.db4o.internal.Platform4;
023    import com.db4o.reflect.ReflectClass;
024    import com.db4o.reflect.ReflectField;
025    import com.db4o.reflect.Reflector;
026    
027    public class JVSTField implements ReflectField {
028    
029            private final Field field;
030    
031            private final Reflector reflector;
032    
033            public JVSTField(Reflector reflector_, Field field_) {
034                    reflector = reflector_;
035                    field = field_;
036                    setAccessible();
037            }
038    
039            public Object get(Object onObject) {
040                    try {
041                            return field.get(onObject);
042                    } catch (Exception e) {
043                            return null;
044                    }
045            }
046    
047            public ReflectClass getFieldType() {
048                    return reflector.forClass(field.getType());
049            }
050    
051            public String getName() {
052                    return field.getName();
053            }
054    
055            public Object indexEntry(Object orig) {
056                    return orig;
057            }
058    
059            public ReflectClass indexType() {
060                    return getFieldType();
061            }
062    
063            public boolean isPublic() {
064                    return Modifier.isPublic(field.getModifiers());
065            }
066    
067            public boolean isStatic() {
068                    return Modifier.isStatic(field.getModifiers());
069            }
070    
071            public boolean isTransient() {
072                    return Modifier.isTransient(field.getModifiers());
073            }
074    
075            public void set(Object onObject, Object attribute) {
076                    try {
077                            field.set(onObject, attribute);
078                    } catch (Exception e) {
079                            e.printStackTrace();
080                            // FIXME: This doesn't work when in its own package...
081                            // if(Debug.atHome){
082                            //e.printStackTrace();
083                            // }
084                    }
085            }               
086    
087            public void setAccessible() {
088                    Platform4.setAccessible(field);
089            }
090    
091    }