View Javadoc

1   /*
2    *   Copyright (c) 2009 The JOMC Project
3    *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
4    *   All rights reserved.
5    *
6    *   Redistribution and use in source and binary forms, with or without
7    *   modification, are permitted provided that the following conditions
8    *   are met:
9    *
10   *     o Redistributions of source code must retain the above copyright
11   *       notice, this list of conditions and the following disclaimer.
12   *
13   *     o Redistributions in binary form must reproduce the above copyright
14   *       notice, this list of conditions and the following disclaimer in
15   *       the documentation and/or other materials provided with the
16   *       distribution.
17   *
18   *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
19   *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20   *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21   *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
22   *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23   *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24   *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25   *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26   *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27   *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28   *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   *
30   *   $Id: ReflectionHelper.java 2083 2010-06-24 00:53:49Z schulte2005 $
31   *
32   */
33  package org.jomc.sdk.model;
34  
35  import java.lang.reflect.InvocationTargetException;
36  import java.text.MessageFormat;
37  import java.util.Locale;
38  import java.util.ResourceBundle;
39  import org.jomc.model.PropertyException;
40  
41  /**
42   * Provides package private static helper methods for accessing objects using reflection.
43   *
44   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
45   * @version $Id: ReflectionHelper.java 2083 2010-06-24 00:53:49Z schulte2005 $
46   */
47  abstract class ReflectionHelper
48  {
49  
50      private static String GET_JAVA_VALUE = "getJavaValue";
51  
52      ReflectionHelper()
53      {
54          super();
55      }
56  
57      static java.lang.Object getJavaValue( final ClassLoader classLoader, final Object o ) throws PropertyException
58      {
59          if ( o != null )
60          {
61              try
62              {
63                  return o.getClass().getMethod( GET_JAVA_VALUE, ClassLoader.class ).invoke( o, classLoader );
64              }
65              catch ( final IllegalAccessException e )
66              {
67                  throw new PropertyException( getMessage(
68                      "methodAccessDenied", GET_JAVA_VALUE, o.getClass().getName() ), e );
69  
70              }
71              catch ( final IllegalArgumentException e )
72              {
73                  throw new AssertionError( e );
74              }
75              catch ( final InvocationTargetException e )
76              {
77                  throw new PropertyException( getMessage(
78                      "methodInvocationFailure", GET_JAVA_VALUE, o.getClass().getName() ), e );
79  
80              }
81              catch ( final SecurityException e )
82              {
83                  throw new PropertyException( getMessage(
84                      "methodAccessDenied", GET_JAVA_VALUE, o.getClass().getName() ), e );
85  
86              }
87              catch ( final NoSuchMethodException e )
88              {
89                  // Optional method not provided.
90                  return o;
91              }
92          }
93  
94          return o;
95      }
96  
97      static <T> T getJavaValue( final ClassLoader classLoader, final Object o, final Class<T> returnType )
98          throws PropertyException
99      {
100         final Object javaValue = getJavaValue( classLoader, o );
101 
102         if ( javaValue != null && !returnType.isAssignableFrom( javaValue.getClass() ) )
103         {
104             throw new PropertyException( getMessage(
105                 "illegalMethodInvocationResult", GET_JAVA_VALUE, o.getClass().getName(), javaValue.getClass().getName(),
106                 returnType.getName() ) );
107 
108         }
109 
110         return (T) javaValue;
111     }
112 
113     static <T> T getJavaValue( final Class<T> type, final String value ) throws PropertyException
114     {
115         if ( value != null )
116         {
117             try
118             {
119                 if ( type == Character.class )
120                 {
121                     if ( value.length() != 1 )
122                     {
123                         throw new PropertyException( getMessage( "illegalValue", value, Character.class.getName() ) );
124                     }
125 
126                     return type.getConstructor( new Class[]
127                         {
128                             char.class
129                         } ).newInstance( new java.lang.Object[]
130                         {
131                             value.charAt( 0 )
132                         } );
133 
134                 }
135                 else if ( type == String.class )
136                 {
137                     return (T) value;
138                 }
139                 else
140                 {
141                     return type.getConstructor( new Class[]
142                         {
143                             String.class
144                         } ).newInstance( value );
145 
146                 }
147             }
148             catch ( final InstantiationException e )
149             {
150                 throw new PropertyException( getMessage( "instantiationException", type.getName() ), e );
151             }
152             catch ( final IllegalAccessException e )
153             {
154                 throw new PropertyException( getMessage( "constructorAccessDenied", type.getName() ), e );
155             }
156             catch ( final IllegalArgumentException e )
157             {
158                 throw new AssertionError( e );
159             }
160             catch ( final InvocationTargetException e )
161             {
162                 throw new PropertyException( getMessage( "constructorInvocationFailure", type.getName() ), e );
163             }
164             catch ( final NoSuchMethodException e )
165             {
166                 throw new PropertyException( getMessage( "constructorNotFound", type.getName() ), e );
167             }
168         }
169 
170         return null;
171     }
172 
173     private static String getMessage( final String key, final java.lang.Object... arguments )
174     {
175         return MessageFormat.format( ResourceBundle.getBundle( ReflectionHelper.class.getName().replace( '.', '/' ),
176                                                                Locale.getDefault() ).getString( key ), arguments );
177 
178     }
179 
180 }