Skip to main content
summaryrefslogtreecommitdiffstats
blob: 88b39888d4ea10366da8e3a61c3181ad3a97c53b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.skynet.blam;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;

/**
 * @author Ryan D. Brooks
 */
public abstract class DynamicBlamOperation {
   private Method mainMethod;
   private BlamParameter[] parameters;

   public void init() throws IllegalArgumentException, NoSuchMethodException, OseeArgumentException {
      mainMethod = findMainMethod(getMainMethodName());

      String[] parameterNames = getParameterNames();
      Class<?>[] parameterTypes = mainMethod.getParameterTypes();
      if (parameterNames.length != parameterTypes.length) {
         throw new OseeArgumentException("The method [%s] has %d parameters, but %d parameter names.",
            getMainMethodName(), parameterTypes.length, parameterNames.length);
      }

      parameters = new BlamParameter[parameterTypes.length];
      for (int i = 0; i < parameterTypes.length; i++) {
         parameters[i] = new BlamParameter(parameterNames[i], parameterTypes[i], null);
      }

   }

   public void executeOperation(Object[] actualParameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
      mainMethod.invoke(this, actualParameters);
   }

   private Method findMainMethod(String mainMethodName) throws IllegalArgumentException, NoSuchMethodException {
      Class<? extends DynamicBlamOperation> classRep = getClass();
      Method[] methods = classRep.getDeclaredMethods(); // get only the methods declared directly in the given class

      for (Method method : methods) {
         if (method.getName().equals(mainMethodName)) {
            return method;
         }
      }
      throw new NoSuchMethodException(mainMethodName);
   }

   /**
    * should return user oriented names for input values in the same order that they should be passed to the operation's
    * configured execution method
    * 
    * @return array of parameter names
    */
   public abstract String[] getParameterNames();

   public abstract String getMainMethodName();

   public BlamParameter[] getParameters() {
      return parameters;
   }
}

Back to the top