1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.script.framework.provider.java;
25 
26 import java.lang.reflect.*;
27 import com.sun.star.script.framework.log.LogUtils;
28 
29 /**
30  * A StrictResolver can be used to get a ScriptProxy object for a given
31  * ScriptDescriptor and Class. The StrictResolver is an implementation of
32  * the Resolver strategy. It will only return a ScriptProxy object if a
33  * method accepting all of the arguments specified in the ScriptDescriptor
34  * can be found in the Class.
35  *
36  * @author     Tomas O'Connor
37  * @created    August 2, 2002
38  */
39 public class StrictResolver implements Resolver
40 {
41     /**
42      *Constructor for the StrictResolver object
43      */
StrictResolver()44     public StrictResolver()
45     {
46         LogUtils.DEBUG( this.getClass().getName() + " created" );
47     }
48 
49 
50     /**
51      * Returns a ScriptProxy object for the given ScriptDescriptor and Class.
52      * Only a strict match will be returned ie. where all of the arguments in
53      * the given ScriptDescriptor match the types of the
54      *
55      * @param  sd  the ScriptDescriptor for which a ScriptProxy is required
56      * @param  c   the Class file in which to search for the method
57      * @return     the ScriptProxy matching the criteria, or null if no match is found
58      */
getProxy( ScriptDescriptor sd, Class c )59     public ScriptProxy getProxy( ScriptDescriptor sd, Class c )
60     throws NoSuchMethodException
61     {
62         Method m = null;
63         ScriptProxy sp = null;
64 
65         LogUtils.DEBUG( "StrictResolver.getProxy() for: " + sd.toString() );
66 
67         try
68         {
69             m = resolveArguments( sd, c );
70         }
71         catch ( ClassNotFoundException e )
72         {
73             throw new NoSuchMethodException( "StrictResolver.getProxy: Can't find method: "
74             + sd.getMethodName() + ":" + e.getMessage() );
75         }
76         catch ( NoSuchMethodException e )
77         {
78             throw new NoSuchMethodException( "StrictResolver.getProxy: Can't find method: "
79                 + sd.getMethodName() + ":" + e.getMessage() );
80         }
81 
82         sp = new ScriptProxy( m );
83 
84         int modifiers = m.getModifiers();
85         if ( !Modifier.isStatic( modifiers ) )
86         {
87             Object o;
88             try
89             {
90                 o = c.newInstance();
91             }
92             catch ( InstantiationException ie )
93             {
94                 throw new NoSuchMethodException( "getScriptProxy: Can't instantiate: " +
95                     c.getName() );
96             }
97             catch ( IllegalAccessException iae )
98             {
99                 throw new NoSuchMethodException( "getScriptProxy: Can't access: "
100                     + c.getName() );
101             }
102             sp.setTargetObject( o );
103         }
104 
105         return sp;
106     }
107 
108 
109     /**
110      *  Description of the Method
111      *
112      * @param  sd                          Description of the Parameter
113      * @param  c                           Description of the Parameter
114      * @return                             Description of the Return Value
115      * @exception  ClassNotFoundException
116      * @exception  NoSuchMethodException
117      */
resolveArguments( ScriptDescriptor sd, Class c )118     private Method resolveArguments( ScriptDescriptor sd, Class c )
119     throws ClassNotFoundException, NoSuchMethodException
120     {
121         return c.getMethod( sd.getMethodName(), sd.getArgumentTypes() );
122     }
123 }
124 
125