##### Coverage Report - org.webmacro.engine.IntrospectionUtils

Classes in this File | Line Coverage | Branch Coverage | Complexity
IntrospectionUtils |
35% | 13/37
36% | 21/58
| 10.25;10.25

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
21 |  |
      */
22 |  |
     package org.webmacro.engine;
23 |  |
24 |  |
     /**
25 |  |
      *
26 |  |
      * @author  Keats
27 |  |
      * @since May 25, 2002
28 |  |
      */
29 |  |
     public final class IntrospectionUtils
30 |  |
     {
31 |  |
32 |  |
         /** Deny instantiation */
33 |  |
         private IntrospectionUtils ()
34 | 0 |
         {
35 | 0 |
         }
36 |  |
37 |  |
         static boolean matches (Class[] sig, Class[] args)
38 |  |
         {
39 | 93112 |
             if (args.length != sig.length)
40 | 9870 |
                 return false;
41 |  |
42 |  |
             try
43 |  |
             {
44 | 145388 |
                 for (int i = 0; i < sig.length; i++)
45 |  |
                 {
46 | 64620 |
                     Class s = sig[i];
47 | 64620 |
                     Class a = args[i];
48 | 64620 |
                     if (s.isPrimitive())
49 |  |
                     {
50 | 64 |
                         if ((s == Integer.TYPE && a == Integer.class) ||
51 |  |
                                 (s == Boolean.TYPE && a == Boolean.class) ||
52 |  |
                                 (s == Character.TYPE && a == Character.class) ||
53 |  |
                                 (s == Long.TYPE && a == Long.class) ||
54 |  |
                                 (s == Short.TYPE && a == Short.class) ||
55 |  |
                                 (s == Double.TYPE && a == Double.class) ||
56 |  |
                                 (s == Float.TYPE && a == Float.class) ||
57 |  |
                                 (s == Void.TYPE && a == Void.class) ||
58 |  |
                                 (s == Byte.TYPE && a == Byte.class))
59 | 0 |
                             continue;
60 |  |
                         else
61 | 48 |
                             return false;
62 |  |
                     }
63 | 64556 |
                     else if (a == null || s.isAssignableFrom(a))
64 | 62078 |
                         continue;
65 |  |
                     else
66 | 2426 |
                         return false;
67 |  |
                 }
68 |  |
             }
69 | 0 |
             catch (NullPointerException e)
70 |  |
             {
71 | 0 |
                 return false; // XXX: block nulls, isAssignableFrom throws this
72 | 80768 |
             }
73 | 80768 |
             return true;
74 |  |
         }
75 |  |
76 |  |
         static public Class[] createTypesFromArgs (Object[] args)
77 |  |
         {
78 | 0 |
             Class[] types = new Class[args.length];
79 | 0 |
             for (int i = 0; i < args.length; i++)
80 |  |
             {
81 |  |
                 try
82 |  |
                 {
83 | 0 |
                     types[i] = args[i].getClass();
84 |  |
                 }
85 | 0 |
                 catch (NullPointerException e)
86 |  |
                 {
87 | 0 |
                     types[i] = null;
88 | 0 |
                 }
89 |  |
             }
90 | 0 |
             return types;
91 |  |
         }
92 |  |
93 |  |
         /** Attempt to instantiate a class with the supplied args. */
94 |  |
         static public Object instantiate (Class c, Object[] args)
95 |  |
                 throws Exception
96 |  |
         {
97 | 0 |
             Object o = null;
98 | 0 |
             if (args == null || args.length == 0)
99 |  |
             {
100 | 0 |
                 o = c.newInstance(); // no arg constructor
101 |  |
             }
102 |  |
             else
103 |  |
             {
104 |  |
                 // try each constructor with the right number of args,
105 |  |
                 // until one works or all have failed
106 | 0 |
                 java.lang.reflect.Constructor[] cons = c.getConstructors();
107 | 0 |
                 for (int i = 0; i < cons.length; i++)
108 |  |
                 {
109 | 0 |
                     if (cons[i].getParameterTypes().length == args.length)
110 |  |
                     {
111 |  |
                         // try to instantiate using this constructor
112 |  |
                         try
113 |  |
                         {
114 | 0 |
                             o = cons[i].newInstance(args);
115 | 0 |
                             break; // if successful, we're done!
116 |  |
                         }
117 | 0 |
                         catch (Exception e)
118 |  |
                         {
119 |  |
                           // ignore this failure, try the next one
120 |  |
                         }
121 |  |
                     }
122 |  |
                 }
123 | 0 |
                 if (o == null)
124 |  |
                 {
125 | 0 |
                     throw new InstantiationException(
126 |  |
                             "Unable to construct object of type " + c.getName()
127 |  |
                             + " using the supplied arguments: "
128 |  |
                             + java.util.Arrays.asList(args).toString());
129 |  |
                 }
130 |  |
             }
131 | 0 |
             return o;
132 |  |
         }
133 |  |
     }