View Javadoc

1   /**
2    * Copyright (C) 2009 Erik Putrycz <erik.putrycz@gmail.com>
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.ep.db4o.javassist;
18  
19  import java.lang.reflect.Field;
20  import java.lang.reflect.Modifier;
21  
22  import com.db4o.internal.Platform4;
23  import com.db4o.reflect.ReflectClass;
24  import com.db4o.reflect.ReflectField;
25  import com.db4o.reflect.Reflector;
26  
27  public class JVSTField implements ReflectField {
28  
29  	private final Field field;
30  
31  	private final Reflector reflector;
32  
33  	public JVSTField(Reflector reflector_, Field field_) {
34  		reflector = reflector_;
35  		field = field_;
36  		setAccessible();
37  	}
38  
39  	public Object get(Object onObject) {
40  		try {
41  			return field.get(onObject);
42  		} catch (Exception e) {
43  			return null;
44  		}
45  	}
46  
47  	public ReflectClass getFieldType() {
48  		return reflector.forClass(field.getType());
49  	}
50  
51  	public String getName() {
52  		return field.getName();
53  	}
54  
55  	public Object indexEntry(Object orig) {
56  		return orig;
57  	}
58  
59  	public ReflectClass indexType() {
60  		return getFieldType();
61  	}
62  
63  	public boolean isPublic() {
64  		return Modifier.isPublic(field.getModifiers());
65  	}
66  
67  	public boolean isStatic() {
68  		return Modifier.isStatic(field.getModifiers());
69  	}
70  
71  	public boolean isTransient() {
72  		return Modifier.isTransient(field.getModifiers());
73  	}
74  
75  	public void set(Object onObject, Object attribute) {
76  		try {
77  			field.set(onObject, attribute);
78  		} catch (Exception e) {
79  			e.printStackTrace();
80  			// FIXME: This doesn't work when in its own package...
81  			// if(Debug.atHome){
82  			//e.printStackTrace();
83  			// }
84  		}
85  	}		
86  
87  	public void setAccessible() {
88  		Platform4.setAccessible(field);
89  	}
90  
91  }