//# 0 errors, 46 messages
//#
/*
    //#ExportedService.java:1:1: class: com.dmdirc.plugins.ExportedService
    //#ExportedService.java:1:1: method: com.dmdirc.plugins.ExportedService.com.dmdirc.plugins.ExportedService__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.dmdirc.plugins;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
 * Object to allow interaction with Exported methods
 */
public class ExportedService {
	/** Method we will be executing today! */
	final Method myMethod;
	
	/** Object we will be executing this method on. */
	final Object myObject;
	
	/**
	 * Create a new ExportedService object.
	 *
	 * @param myClass class method is in.
	 * @param methodName Name of method
	 */
	public ExportedService(final Class myClass, final String methodName) {
		this(myClass, methodName, null);
    //#ExportedService.java:44: method: void com.dmdirc.plugins.ExportedService.com.dmdirc.plugins.ExportedService(Class, String)
    //#input(void com.dmdirc.plugins.ExportedService(Class, String)): methodName
    //#input(void com.dmdirc.plugins.ExportedService(Class, String)): myClass
    //#input(void com.dmdirc.plugins.ExportedService(Class, String)): this
    //#output(void com.dmdirc.plugins.ExportedService(Class, String)): this.myMethod
    //#output(void com.dmdirc.plugins.ExportedService(Class, String)): this.myObject
    //#post(void com.dmdirc.plugins.ExportedService(Class, String)): init'ed(this.myMethod)
    //#post(void com.dmdirc.plugins.ExportedService(Class, String)): this.myObject == null
    //#unanalyzed(void com.dmdirc.plugins.ExportedService(Class, String)): Effects-of-calling:java.lang.Class:getDeclaredMethods
    //#unanalyzed(void com.dmdirc.plugins.ExportedService(Class, String)): Effects-of-calling:java.lang.reflect.Method:getName
    //#unanalyzed(void com.dmdirc.plugins.ExportedService(Class, String)): Effects-of-calling:java.lang.String:equals
	}
    //#ExportedService.java:45: end of method: void com.dmdirc.plugins.ExportedService.com.dmdirc.plugins.ExportedService(Class, String)
	
	/**
	 * Create a new ExportedService object.
	 *
	 * @param myClass class method is in.
	 * @param methodName Name of method
	 * @param object Object to execute this method on.
	 */
	public ExportedService(final Class<?> myClass, final String methodName, final Object object) {
    //#ExportedService.java:54: method: void com.dmdirc.plugins.ExportedService.com.dmdirc.plugins.ExportedService(Class, String, Object)
    //#input(void com.dmdirc.plugins.ExportedService(Class, String, Object)): methodName
    //#input(void com.dmdirc.plugins.ExportedService(Class, String, Object)): myClass
    //#input(void com.dmdirc.plugins.ExportedService(Class, String, Object)): object
    //#input(void com.dmdirc.plugins.ExportedService(Class, String, Object)): this
    //#output(void com.dmdirc.plugins.ExportedService(Class, String, Object)): this.myMethod
    //#output(void com.dmdirc.plugins.ExportedService(Class, String, Object)): this.myObject
    //#presumption(void com.dmdirc.plugins.ExportedService(Class, String, Object)): arr$.length@59 <= 4_294_967_295
    //#presumption(void com.dmdirc.plugins.ExportedService(Class, String, Object)): arr$[i$]@59 != null
    //#presumption(void com.dmdirc.plugins.ExportedService(Class, String, Object)): java.lang.Class:getDeclaredMethods(...)@59 != null
    //#presumption(void com.dmdirc.plugins.ExportedService(Class, String, Object)): java.lang.reflect.Method:getName(...)@61 != null
    //#post(void com.dmdirc.plugins.ExportedService(Class, String, Object)): init'ed(this.myMethod)
    //#post(void com.dmdirc.plugins.ExportedService(Class, String, Object)): this.myObject == object
    //#post(void com.dmdirc.plugins.ExportedService(Class, String, Object)): init'ed(this.myObject)
    //#test_vector(void com.dmdirc.plugins.ExportedService(Class, String, Object)): myClass: Inverse{null}, Addr_Set{null}
    //#test_vector(void com.dmdirc.plugins.ExportedService(Class, String, Object)): java.lang.String:equals(...)@61: {0}, {1}
		myObject = object;
		if (myClass == null) {
			myMethod = null;
		} else {
			final Method[] methods = myClass.getDeclaredMethods();
			for (Method m : methods) {
				if (m.getName().equals(methodName)) {
					myMethod = m;
					return;
				}
			}
			myMethod = null;
		}
	}
    //#ExportedService.java:68: end of method: void com.dmdirc.plugins.ExportedService.com.dmdirc.plugins.ExportedService(Class, String, Object)
	
	/**
	 * Execute the method.
	 *
	 * @param args Arguments to pass to method
	 * @return result of executing the method
	 */
	public Object execute(final Object... args) {
		if (myMethod == null) { return null; }
    //#ExportedService.java:77: method: Object com.dmdirc.plugins.ExportedService.execute(Object[])
    //#input(Object execute(Object[])): args
    //#input(Object execute(Object[])): this
    //#input(Object execute(Object[])): this.myMethod
    //#input(Object execute(Object[])): this.myObject
    //#output(Object execute(Object[])): return_value
    //#post(Object execute(Object[])): init'ed(return_value)
    //#test_vector(Object execute(Object[])): this.myMethod: Inverse{null}, Addr_Set{null}
		
		try {
			return myMethod.invoke(myObject, args);
		} catch (IllegalAccessException iae) {
			return null;
		} catch (IllegalArgumentException iae) {
			return null;
		} catch (InvocationTargetException ite) {
			return null;
    //#ExportedService.java:86: end of method: Object com.dmdirc.plugins.ExportedService.execute(Object[])
		}
	}
}
    //#output(com.dmdirc.plugins.ExportedService__static_init): __Descendant_Table[com/dmdirc/plugins/ExportedService]
    //#output(com.dmdirc.plugins.ExportedService__static_init): __Dispatch_Table.execute([Ljava/lang/Object;)Ljava/lang/Object;
    //#post(com.dmdirc.plugins.ExportedService__static_init): __Descendant_Table[com/dmdirc/plugins/ExportedService] == &__Dispatch_Table
    //#post(com.dmdirc.plugins.ExportedService__static_init): __Dispatch_Table.execute([Ljava/lang/Object;)Ljava/lang/Object; == &execute
    //#ExportedService.java:: end of method: com.dmdirc.plugins.ExportedService.com.dmdirc.plugins.ExportedService__static_init
    //#ExportedService.java:: end of class: com.dmdirc.plugins.ExportedService
