View Javadoc
1   /*
2   This project provide access to non public fields
3   and methods as help for writing tests.
4   Copyright (C) 2007  Christof Lehmann
5   
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License
8   as published by the Free Software Foundation; either version 2
9   of the License, or (at your option) any later version.
10  
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   */
20  package net.sf.testextensions;
21  
22  /**
23   * The <code>AccessibleObjectContainer</code> declares a usefull
24   * method, which would be used by subclasses.
25   * @author forge-cl
26   *
27   */
28  class AccessibleObjectContainer {
29  
30  	/**
31  	 * The field <code>parameterClasses</code> remember the types
32  	 * of the callable method or constructor.
33  	 */
34  	private Class < ? > [] parameterClasses;
35  
36  	/**
37  	 * The constructor initialize the field argumentClasses.
38  	 * @param theParameterClasses the types of the method invocation
39  	 */
40  	protected AccessibleObjectContainer(
41  			final Class < ? > [] theParameterClasses) {
42  		parameterClasses = theParameterClasses;
43  	}
44  
45  	/**
46  	 * The method <code>suitesToArgs</code> check, if the types
47  	 * of the arguments for a method call suites to the accepted
48  	 * types.
49  	 * @param args the arguments for the method call
50  	 * @return true if the arguments suites to the method declaration
51  	 */
52  	public boolean suitesToArgs(final Object[] args) {
53  		boolean flag = parameterClasses != null
54  			&&
55  			parameterClasses .length == args .length;
56  		for (int i = 0; i < parameterClasses.length && flag; i++) {
57  			Object curParameter = args[i];
58  			Class curParamClass = parameterClasses[i];
59  			flag = isInstanceOf(curParameter, curParamClass);
60  		}
61  		return flag;
62     }
63  
64  	/**
65  	 * The method <code>isInstanceOf</code> checks, if a object is an instance
66  	 * of a class. Its also works with primitive.
67  	 *
68  	 * @param aObject
69  	 *            the object, which should be an instanceOf
70  	 * @param aClass
71  	 *            the object should be an instance of this class.
72  	 * @return true wenn aClass.isInstance(aObject) or its aquivalent
73  	 *
74  	 */
75  	private boolean isInstanceOf(final Object aObject, final Class aClass) {
76  		return aClass.isInstance(aObject)
77  				|| (getAequivalent(aClass).isInstance(aObject));
78  	}
79  
80  	/**
81  	 * The method <code>getQequivalent</code> returns the non primitive
82  	 * representation of a primitive.
83  	 *
84  	 * @param aClass
85  	 *            a primitive Class
86  	 * @return the non primitive or the same class
87  	 */
88  	private Class getAequivalent(final Class aClass) {
89  		if (aClass.isPrimitive()) {
90  			String theName = aClass.getName();
91  			if (theName.equals("int")) {
92  				return Integer.class;
93  			} else if (theName.equals("double")) {
94  				return Double.class;
95  			} else if (theName.equals("short")) {
96  				return Short.class;
97  			} else if (theName.equals("char")) {
98  				return Character.class;
99  			} else if (theName.equals("long")) {
100 				return Long.class;
101 			} else if (theName.equals("boolean")) {
102 				return Boolean.class;
103 			} else if (theName.equals("byte")) {
104 				return Byte.class;
105 			} else {
106 				return aClass;
107 			}
108 		} else {
109 			return aClass;
110 		}
111 	}
112 
113 
114 }