KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beans > ReflectiveBeanProp


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.schema2beans;
21
22 import java.lang.reflect.*;
23
24 /**
25  * A BeanProp that works using reflection.
26  */

27 public class ReflectiveBeanProp extends BeanProp {
28     private Method writer;
29     private Method arrayWriter;
30     private Method reader;
31     private Method arrayReader;
32     private Method adder;
33     private Method remover;
34
35     public ReflectiveBeanProp(BaseBean bean, String JavaDoc dtdName, String JavaDoc beanName,
36                               int type, Class JavaDoc propClass,
37                               Method writer, Method arrayWriter, Method reader, Method arrayReader, Method adder, Method remover) {
38         super(bean, dtdName, beanName, type, propClass, -1);
39         bindings = null;
40         attributes = null;
41         this.writer = writer;
42         this.arrayWriter = arrayWriter;
43         this.reader = reader;
44         this.arrayReader = arrayReader;
45         this.adder = adder;
46         this.remover = remover;
47     }
48
49     public ReflectiveBeanProp(BaseBean bean, String JavaDoc dtdName, String JavaDoc beanName,
50                               int type, Class JavaDoc propClass, boolean isRoot,
51                               Method writer, Method arrayWriter, Method reader, Method arrayReader, Method adder, Method remover) {
52         super(bean, dtdName, beanName, type, propClass, isRoot, -1);
53         bindings = null;
54         attributes = null;
55         this.writer = writer;
56         this.arrayWriter = arrayWriter;
57         this.reader = reader;
58         this.arrayReader = arrayReader;
59         this.adder = adder;
60         this.remover = remover;
61     }
62
63     DOMBinding getBinding(int index) {
64         throw new IllegalStateException JavaDoc();
65     }
66     
67     protected int bindingsSize() {
68         Object JavaDoc[] arr = getObjectArray(0);
69         if (arr == null)
70             return 0;
71         return arr.length;
72     }
73
74     public Object JavaDoc getValue(int index) {
75         try {
76             if (isIndexed())
77                 return reader.invoke(bean, new Object JavaDoc[] {new Integer JavaDoc(index)});
78             else
79                 return reader.invoke(bean, new Object JavaDoc[] {});
80         } catch (java.lang.IllegalAccessException JavaDoc e) {
81             throw new RuntimeException JavaDoc(e);
82         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
83             throw new RuntimeException JavaDoc(e);
84         }
85     }
86
87     public Object JavaDoc getValueById(int id) {
88         return getObjectArray(0)[idToIndex(id)];
89     }
90
91     public int indexToId(int index) {
92         return index;
93     }
94
95     public int idToIndex(int id) {
96         return id;
97     }
98
99     protected Object JavaDoc[] getObjectArray(int extraElements) {
100         if (arrayReader == null)
101             return new Object JavaDoc[] {};
102         try {
103             return (Object JavaDoc[]) arrayReader.invoke(bean, new Object JavaDoc[] {});
104         } catch (java.lang.IllegalAccessException JavaDoc e) {
105             throw new RuntimeException JavaDoc(e);
106         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
107             throw new RuntimeException JavaDoc(e);
108         }
109     }
110     
111     public void setValue(Object JavaDoc[] value) {
112         try {
113             arrayWriter.invoke(bean, new Object JavaDoc[] {value});
114         } catch (java.lang.IllegalAccessException JavaDoc e) {
115             throw new RuntimeException JavaDoc(e);
116         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
117             throw new RuntimeException JavaDoc(e);
118         }
119     }
120
121     public int removeValue(Object JavaDoc value) {
122         try {
123             return ((Integer JavaDoc) remover.invoke(bean, new Object JavaDoc[] {value})).intValue();
124         } catch (java.lang.IllegalAccessException JavaDoc e) {
125             throw new RuntimeException JavaDoc(e);
126         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
127             throw new RuntimeException JavaDoc(e);
128         }
129     }
130
131     public void removeValue(int index) {
132         Object JavaDoc[] arr = getObjectArray(0);
133         if (arr == null)
134             return;
135         Object JavaDoc[] newArr = (Object JavaDoc[]) Array.newInstance(arr.getClass().getComponentType(),
136                                             arr.length-1);
137         System.arraycopy(arr, 0, newArr, 0, index);
138         System.arraycopy(arr, index+1, newArr, index, arr.length - index - 1);
139         setValue(newArr);
140     }
141
142     protected int setElement(int index, Object JavaDoc value, boolean add) {
143         try {
144             if (add)
145                 index = ((Integer JavaDoc) adder.invoke(bean, new Object JavaDoc[] {value})).intValue();
146             else if (isIndexed())
147                 writer.invoke(bean, new Object JavaDoc[] {new Integer JavaDoc(index), value});
148             else
149                 writer.invoke(bean, new Object JavaDoc[] {value});
150             return index;
151         } catch (java.lang.IllegalAccessException JavaDoc e) {
152             throw new RuntimeException JavaDoc("setElement: "+getBeanName(), e);
153         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
154             throw new RuntimeException JavaDoc("setElement: "+getBeanName(), e);
155         }
156     }
157
158     public void setAttributeValue(int index, String JavaDoc name, String JavaDoc value) {
159         throw new IllegalStateException JavaDoc();
160     }
161
162     public String JavaDoc getAttributeValue(int index, String JavaDoc name) {
163         throw new IllegalStateException JavaDoc();
164     }
165
166     public DOMBinding registerDomNode(org.w3c.dom.Node JavaDoc node, DOMBinding binding,
167                       BaseBean bean) throws Schema2BeansException {
168         throw new IllegalStateException JavaDoc();
169     }
170     
171     public String JavaDoc getFullName() {
172         return getFullName(0);
173     }
174
175     public String JavaDoc getFullName(int index) {
176         if (reader == null)
177             return bean.nameSelf();
178         else
179             return bean.nameSelf()+"/"+bean.nameChild(getValue(index), false, false);
180     }
181
182     void buildPathName(DOMBinding binding, StringBuffer JavaDoc str) {
183         throw new IllegalStateException JavaDoc();
184     }
185     
186     String JavaDoc buildFullName(int index, String JavaDoc attr) {
187         throw new IllegalStateException JavaDoc();
188     }
189 }
190
Popular Tags