001 package org.maltparser.core.feature.system; 002 003 import java.lang.reflect.Constructor; 004 import java.lang.reflect.InvocationTargetException; 005 006 import org.maltparser.core.config.ConfigurationRegistry; 007 import org.maltparser.core.exception.MaltChainedException; 008 import org.maltparser.core.feature.FeatureException; 009 import org.maltparser.core.feature.function.Function; 010 /** 011 * 012 * 013 * @author Johan Hall 014 * @since 1.0 015 **/ 016 public class FunctionDescription { 017 private String name; 018 private Class<?> functionClass; 019 private boolean hasSubfunctions; 020 021 public FunctionDescription(String name, Class<?> functionClass, boolean hasSubfunctions) { 022 setName(name); 023 setFunctionClass(functionClass); 024 setHasSubfunctions(hasSubfunctions); 025 } 026 027 public Function newFunction(ConfigurationRegistry registry) throws MaltChainedException { 028 Constructor<?>[] constructors = functionClass.getConstructors(); 029 if (constructors.length == 0) { 030 try { 031 return (Function)functionClass.newInstance(); 032 } catch (InstantiationException e) { 033 throw new FeatureException("The function '"+functionClass.getName()+"' cannot be initialized. ", e); 034 } catch (IllegalAccessException e) { 035 throw new FeatureException("The function '"+functionClass.getName()+"' cannot be initialized. ", e); 036 } 037 } 038 Class<?>[] params = constructors[0].getParameterTypes(); 039 if (params.length == 0) { 040 try { 041 return (Function)functionClass.newInstance(); 042 } catch (InstantiationException e) { 043 throw new FeatureException("The function '"+functionClass.getName()+"' cannot be initialized. ", e); 044 } catch (IllegalAccessException e) { 045 throw new FeatureException("The function '"+functionClass.getName()+"' cannot be initialized. ", e); 046 } 047 } 048 Object[] arguments = new Object[params.length]; 049 for (int i = 0; i < params.length; i++) { 050 if (hasSubfunctions && params[i] == java.lang.String.class) { 051 arguments[i] = name; 052 } else { 053 arguments[i] = registry.get(params[i]); 054 } 055 } 056 try { 057 return (Function)constructors[0].newInstance(arguments); 058 } catch (InstantiationException e) { 059 throw new FeatureException("The function '"+functionClass.getName()+"' cannot be initialized. ", e); 060 } catch (IllegalAccessException e) { 061 throw new FeatureException("The function '"+functionClass.getName()+"' cannot be initialized. ", e); 062 } catch (InvocationTargetException e) { 063 throw new FeatureException("The function '"+functionClass.getName()+"' cannot be initialized. ", e); 064 } 065 } 066 067 public String getName() { 068 return name; 069 } 070 071 public void setName(String name) { 072 this.name = name; 073 } 074 075 public Class<?> getFunctionClass() { 076 return functionClass; 077 } 078 079 public void setFunctionClass(Class<?> functionClass) { 080 this.functionClass = functionClass; 081 } 082 083 public boolean isHasSubfunctions() { 084 return hasSubfunctions; 085 } 086 087 public void setHasSubfunctions(boolean hasSubfunctions) { 088 this.hasSubfunctions = hasSubfunctions; 089 } 090 091 public boolean equals(Object obj) { 092 if (this == obj) 093 return true; 094 if (obj == null) 095 return false; 096 if (getClass() != obj.getClass()) 097 return false; 098 if (!(name.equalsIgnoreCase(((FunctionDescription)obj).getName()))) { 099 return false; 100 } else if (!(functionClass.equals(((FunctionDescription)obj).getFunctionClass()))) { 101 return false; 102 } 103 return true; 104 } 105 106 public String toString() { 107 StringBuilder sb = new StringBuilder(); 108 sb.append(name); 109 sb.append("->"); 110 sb.append(functionClass.getName()); 111 return sb.toString(); 112 } 113 }