|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
FieldBeanInitializer.java | 0% | 0% | 0% | 0% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.bean;
|
|
16 |
|
|
17 |
import java.lang.reflect.Field;
|
|
18 |
|
|
19 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
20 |
import org.apache.hivemind.ClassResolver;
|
|
21 |
import org.apache.tapestry.IBeanProvider;
|
|
22 |
import org.apache.tapestry.Tapestry;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* Initializes a bean with the value of a public static field.
|
|
26 |
*
|
|
27 |
* @author Howard Lewis Ship
|
|
28 |
* @since 1.0.8
|
|
29 |
*
|
|
30 |
**/
|
|
31 |
|
|
32 |
public class FieldBeanInitializer extends AbstractBeanInitializer |
|
33 |
{ |
|
34 |
protected String _fieldName;
|
|
35 |
protected Object _fieldValue;
|
|
36 |
private boolean _fieldResolved = false; |
|
37 |
|
|
38 | 0 |
public synchronized void setBeanProperty(IBeanProvider provider, Object bean) |
39 |
{ |
|
40 | 0 |
ClassResolver resolver = provider.getClassResolver(); |
41 |
|
|
42 | 0 |
if (!_fieldResolved)
|
43 | 0 |
resolveField(resolver); |
44 |
|
|
45 | 0 |
setBeanProperty(bean, _fieldValue); |
46 |
} |
|
47 |
|
|
48 | 0 |
private void resolveField(ClassResolver resolver) |
49 |
{ |
|
50 | 0 |
if (_fieldResolved)
|
51 | 0 |
return;
|
52 |
|
|
53 |
// This is all copied out of of FieldBinding!!
|
|
54 |
|
|
55 | 0 |
int dotx = _fieldName.lastIndexOf('.');
|
56 |
|
|
57 | 0 |
if (dotx < 0)
|
58 | 0 |
throw new ApplicationRuntimeException( |
59 |
Tapestry.format("invalid-field-name", _fieldName));
|
|
60 |
|
|
61 | 0 |
String className = _fieldName.substring(0, dotx); |
62 | 0 |
String simpleFieldName = _fieldName.substring(dotx + 1); |
63 |
|
|
64 |
// Simple class names are assumed to be in the java.lang package.
|
|
65 |
|
|
66 | 0 |
if (className.indexOf('.') < 0)
|
67 | 0 |
className = "java.lang." + className;
|
68 |
|
|
69 | 0 |
Class targetClass = null;
|
70 |
|
|
71 | 0 |
try
|
72 |
{ |
|
73 | 0 |
targetClass = resolver.findClass(className); |
74 |
} |
|
75 |
catch (Throwable t)
|
|
76 |
{ |
|
77 | 0 |
throw new ApplicationRuntimeException( |
78 |
Tapestry.format("unable-to-resolve-class", className),
|
|
79 |
t); |
|
80 |
} |
|
81 |
|
|
82 | 0 |
Field field = null;
|
83 |
|
|
84 | 0 |
try
|
85 |
{ |
|
86 | 0 |
field = targetClass.getField(simpleFieldName); |
87 |
} |
|
88 |
catch (NoSuchFieldException ex)
|
|
89 |
{ |
|
90 | 0 |
throw new ApplicationRuntimeException( |
91 |
Tapestry.format("field-not-defined", _fieldName),
|
|
92 |
ex); |
|
93 |
} |
|
94 |
|
|
95 |
// Get the value of the field. null means look for it as a static
|
|
96 |
// variable.
|
|
97 |
|
|
98 | 0 |
try
|
99 |
{ |
|
100 | 0 |
_fieldValue = field.get(null);
|
101 |
} |
|
102 |
catch (IllegalAccessException ex)
|
|
103 |
{ |
|
104 | 0 |
throw new ApplicationRuntimeException( |
105 |
Tapestry.format("illegal-field-access", _fieldName),
|
|
106 |
ex); |
|
107 |
} |
|
108 |
catch (NullPointerException ex)
|
|
109 |
{ |
|
110 | 0 |
throw new ApplicationRuntimeException( |
111 |
Tapestry.format("field-is-instance", _fieldName),
|
|
112 |
ex); |
|
113 |
} |
|
114 |
|
|
115 | 0 |
_fieldResolved = true;
|
116 |
} |
|
117 |
|
|
118 |
} |
|