Urbi SDK Remote for Java  2.7.5
UObject.java
00001 /*
00002  * Copyright (C) 2010-2011, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00011 package urbi;
00012 
00013 /**
00014 \mainpage
00015 This document is the autogenerated documentation for Urbi SDK Remote Java.
00016 
00017 Classes of interest:
00018 - Urbi connections: urbi::UAbstractClient, urbi::UClient, urbi::USyncClient.
00019 - Data types: urbi::UValue, urbi::UBinary.
00020 - UObject API: urbi::UObject, urbi::UVar.
00021 */
00022 
00023 import gnu.bytecode.*;
00024 import java.lang.Class;
00025 import java.lang.reflect.Method;
00026 import java.lang.reflect.Constructor;
00027 import java.lang.RuntimeException;
00028 import java.util.LinkedList;
00029 import urbi.urbi;
00030 import urbi.UObjectCPP;
00031 import urbi.UVar;
00032 import urbi.URBIStarterJAVA;
00033 import urbi.UContextImpl;
00034 import urbi.UValue;
00035 import urbi.UrbiRoot;
00036 import urbi.UAutoValue;
00037 import urbi.UEvent;
00038 
00039 /// This class is the base UObject class for Java UObject.
00040 /// Please have all your Java UObject extends UObject
00041 public class UObject extends UObjectCPP
00042 {
00043 
00044     /// Constructor
00045     public UObject (String s) {
00046         super (s);
00047     }
00048 
00049     private boolean superdofinalize = true;
00050 
00051     protected void finalize() {
00052         // Call super finalize only when destruction originate from
00053         // JAVA.
00054         if (superdofinalize)
00055             super.finalize();
00056     }
00057 
00058     // Hack to have java subclasses finalize() called when destruction
00059     // originare from C++.
00060     protected void swigDirectorDisconnect() {
00061         // Destruction originate from C++
00062         superdofinalize = false;
00063         // call subclass finalize if there are any
00064         finalize();
00065         // then let swig do its work
00066         super.swigDirectorDisconnect ();
00067     }
00068 
00069     /// ------------------ ///
00070     ///                    ///
00071     ///  UNotifyOnRequest  ///
00072     ///                    ///
00073     /// ------------------ ///
00074 
00075     protected native void registerNotifyOnRequest(long var,
00076                                                   String var_name,
00077                                                   boolean is_owned,
00078                                                   String obj_name,
00079                                                   String method,
00080                                                   String signature,
00081                                                   String return_type,
00082                                                   int    arg_number,
00083                                                   String[] types);
00084 
00085 
00086     private String[] classToStringArray(Class[] p) {
00087         String[] types = new String[p.length];
00088         for (int i = 0; i < p.length; ++i) {
00089             types[i] = p[i].toString();
00090         }
00091         return types;
00092     }
00093 
00094 
00095     protected void UNotifyOnRequest (String var_name, Method m)
00096     {
00097         checkNotifyRegisteredMethod (m, "UNotifyOnRequest");
00098         String bytecode_sig = getMethodBytecodeSignature (m);
00099         Class[] p = m.getParameterTypes();
00100         String[] types = classToStringArray(p);
00101         registerNotifyOnRequest (0,
00102                                  var_name,
00103                                  false,
00104                                  get__name (),
00105                                  m.getName (),
00106                                  bytecode_sig,
00107                                  m.getReturnType().getName (),
00108                                  p.length,
00109                                  types);
00110     }
00111 
00112     protected void UNotifyOnRequest (UVar v, Method m)
00113     {
00114         checkNotifyRegisteredMethod (m, "UNotifyOnRequest");
00115         String bytecode_sig = getMethodBytecodeSignature (m);
00116         Class[] p = m.getParameterTypes();
00117         String[] types = classToStringArray(p);
00118         registerNotifyOnRequest (UVar.getCPtr (v),
00119                                  v.getName(),
00120                                  v.getOwned (),
00121                                  get__name (),
00122                                  m.getName (),
00123                                  bytecode_sig,
00124                                  m.getReturnType().getName (),
00125                                  p.length,
00126                                  types);
00127     }
00128 
00129     protected void UNotifyOnRequest (String var_name, String method_name)
00130     {
00131         Method m = findMethodFromName (this, method_name);
00132         UNotifyOnRequest (var_name, m);
00133     }
00134 
00135     protected void UNotifyOnRequest (String var_name,
00136                                      String method_name,
00137                                      String[] parameters_name)
00138     {
00139         Method m;
00140         try {
00141             Class[] params = stringTypeToClassType (parameters_name);
00142             Class obj_class = getClass ();
00143             m = obj_class.getMethod (method_name, params);
00144         }
00145         catch (java.lang.ClassNotFoundException e) {
00146             throw new RuntimeException (e);
00147         }
00148         catch (java.lang.NoSuchMethodException e) {
00149             throw new RuntimeException (e);
00150         }
00151         UNotifyOnRequest (var_name, m);
00152     }
00153 
00154 
00155     protected void UNotifyOnRequest (UVar v, String method_name)
00156     {
00157         Method m = findMethodFromName (this, method_name);
00158         UNotifyOnRequest (v, m);
00159     }
00160 
00161     protected void UNotifyOnRequest (UVar v,
00162                                      String method_name,
00163                                      String[] parameters_name)
00164     {
00165         Method m;
00166         try {
00167             Class[] params = stringTypeToClassType (parameters_name);
00168             Class obj_class = getClass ();
00169             m = obj_class.getMethod (method_name, params);
00170         }
00171         catch (java.lang.ClassNotFoundException e) {
00172             throw new RuntimeException (e);
00173         }
00174         catch (java.lang.NoSuchMethodException e) {
00175             throw new RuntimeException (e);
00176         }
00177         UNotifyOnRequest (v, m);
00178     }
00179 
00180 
00181     /// --------------- ///
00182     ///                 ///
00183     ///  UNotifyChange  ///
00184     ///                 ///
00185     /// --------------- ///
00186 
00187     protected native void registerNotifyChange(long var,
00188                                                String var_name,
00189                                                boolean is_owned,
00190                                                String obj_name,
00191                                                String method,
00192                                                String signature,
00193                                                String return_type,
00194                                                int    arg_number,
00195                                                String[] types);
00196 
00197     protected void UNotifyChange (String var_name, Method m)
00198     {
00199         checkNotifyRegisteredMethod (m, "UNotifyChange");
00200         String bytecode_sig = getMethodBytecodeSignature (m);
00201         Class[] p = m.getParameterTypes();
00202         String[] types = classToStringArray(p);
00203         registerNotifyChange (0,
00204                               var_name,
00205                               false,
00206                               get__name (),
00207                               m.getName (),
00208                               bytecode_sig,
00209                               m.getReturnType().getName (),
00210                               p.length,
00211                               types);
00212     }
00213 
00214     protected void UNotifyChange (UVar v, Method m)
00215     {
00216         checkNotifyRegisteredMethod (m, "UNotifyChange");
00217         String bytecode_sig = getMethodBytecodeSignature (m);
00218         Class[] p = m.getParameterTypes();
00219         String[] types = classToStringArray(p);
00220         registerNotifyChange (UVar.getCPtr (v),
00221                               v.getName(),
00222                               v.getOwned (),
00223                               get__name (),
00224                               m.getName (),
00225                               bytecode_sig,
00226                               m.getReturnType().getName (),
00227                               p.length,
00228                               types);
00229     }
00230 
00231     protected void UNotifyChange (String var_name, String method_name)
00232     {
00233         Method m = findMethodFromName (this, method_name);
00234         UNotifyChange (var_name, m);
00235     }
00236 
00237     protected void UNotifyChange (String var_name,
00238                                   String method_name,
00239                                   String[] parameters_name)
00240     {
00241         Method m;
00242         try {
00243             Class[] params = stringTypeToClassType (parameters_name);
00244             Class obj_class = getClass ();
00245             m = obj_class.getMethod (method_name, params);
00246         }
00247         catch (java.lang.ClassNotFoundException e) {
00248             throw new RuntimeException (e);
00249         }
00250         catch (java.lang.NoSuchMethodException e) {
00251             throw new RuntimeException (e);
00252         }
00253         UNotifyChange (var_name, m);
00254     }
00255 
00256 
00257     protected void UNotifyChange (UVar v, String method_name)
00258     {
00259         Method m = findMethodFromName (this, method_name);
00260         UNotifyChange (v, m);
00261     }
00262 
00263     protected void UNotifyChange (UVar v,
00264                                   String method_name,
00265                                   String[] parameters_name)
00266     {
00267         Method m;
00268         try {
00269             Class[] params = stringTypeToClassType (parameters_name);
00270             Class obj_class = getClass ();
00271             m = obj_class.getMethod (method_name, params);
00272         }
00273         catch (java.lang.ClassNotFoundException e) {
00274             throw new RuntimeException (e);
00275         }
00276         catch (java.lang.NoSuchMethodException e) {
00277             throw new RuntimeException (e);
00278         }
00279         UNotifyChange (v, m);
00280     }
00281 
00282 
00283 
00284     /// --------------- ///
00285     ///                 ///
00286     ///  UBindFunction  ///
00287     ///                 ///
00288     /// --------------- ///
00289 
00290     protected native void registerFunction (Object obj,
00291                                             String obj_name,
00292                                             String method,
00293                                             String signature,
00294                                             String return_type,
00295                                             int    arg_number,
00296                                             String[] types);
00297 
00298     protected void UBindFunction (Object obj, Method m)
00299     {
00300         Class[] p = m.getParameterTypes();
00301 
00302         if (p.length > 16) {
00303 
00304             String msg = "Function \""+ m.getName () + "\" has " + p.length
00305                 + " arguments. You can't bind a function with more than "
00306                 + "16 arguments with UBindFunction.";
00307             throw new RuntimeException (msg);
00308         }
00309 
00310         String bytecode_sig = getMethodBytecodeSignature (m);
00311         String[] types = classToStringArray(p);
00312         registerFunction (obj,
00313                           get__name (),
00314                           m.getName (),
00315                           bytecode_sig,
00316                           m.getReturnType().getName (),
00317                           p.length,
00318                           types);
00319     }
00320 
00321     protected void UBindFunction (Object obj,
00322                                   String method_name)
00323     {
00324         Method m = findMethodFromName (obj, method_name);
00325         UBindFunction (obj, m);
00326     }
00327 
00328     protected void UBindFunction (String method_name)
00329     {
00330         Method m = findMethodFromName (this, method_name);
00331         UBindFunction (this, m);
00332     }
00333 
00334     protected void UBindFunctions(Object obj,
00335                                   String ... method_names)
00336     {
00337         for (String method_name : method_names)
00338             UBindFunction(obj, method_name);
00339     }
00340 
00341     protected void UBindFunctions(String ... method_names)
00342     {
00343         for (String method_name : method_names)
00344             UBindFunction(this, method_name);
00345     }
00346 
00347 
00348     protected void UBindFunction (Object obj,
00349                                   String method_name,
00350                                   String[] parameters_name)
00351     {
00352         Method m;
00353         try {
00354             Class[] params = stringTypeToClassType (parameters_name);
00355             Class obj_class = obj.getClass ();
00356             m = obj_class.getMethod (method_name, params);
00357         }
00358         catch (java.lang.ClassNotFoundException e) {
00359             throw new RuntimeException (e);
00360         }
00361         catch (java.lang.NoSuchMethodException e) {
00362             throw new RuntimeException (e);
00363         }
00364         UBindFunction (obj, m);
00365     }
00366 
00367     /// --------------- ///
00368     ///                 ///
00369     ///    USetTimer    ///
00370     ///                 ///
00371     /// --------------- ///
00372 
00373     protected native String registerTimerFunction (Object obj,
00374                                                  String obj_name,
00375                                                  double period,
00376                                                  String method,
00377                                                  String signature,
00378                                                  String return_type,
00379                                                  int    arg_number);
00380 
00381     protected String USetTimer (double period, Object obj, Method m)
00382         throws RuntimeException
00383     {
00384         Class[] p = m.getParameterTypes();
00385         if (p.length > 0) {
00386 
00387             String msg = "Function \""+ m.getName () + "\" has " + p.length
00388                 + " arguments. You can only register functions with zero "
00389                 + " arguments with USetTimer.";
00390             throw new RuntimeException (msg);
00391         }
00392         if (m.getReturnType() != int.class) {
00393             String msg = "Return type of the function \""
00394                 + m.getName () + "\" is "+ m.getReturnType().getName ()
00395                 + ". You can only register functions with a return type of"
00396                 + " type int with USetTimer.";
00397             throw new RuntimeException (msg);
00398         }
00399 
00400         String bytecode_sig = getMethodBytecodeSignature (m);
00401         return registerTimerFunction (obj,
00402                                       get__name (),
00403                                       period,
00404                                       m.getName (),
00405                                       bytecode_sig,
00406                                       m.getReturnType().getName (),
00407                                       m.getParameterTypes().length);
00408     }
00409 
00410 
00411     protected String USetTimer (double period,
00412                                 Object obj,
00413                                 String method_name,
00414                                 String[] parameters_name)
00415     {
00416         Method m;
00417         try {
00418             Class[] params = stringTypeToClassType (parameters_name);
00419             Class obj_class = obj.getClass ();
00420             m = obj_class.getMethod (method_name, params);
00421         }
00422         catch (java.lang.ClassNotFoundException e) {
00423             throw new RuntimeException (e);
00424         }
00425         catch (java.lang.NoSuchMethodException e) {
00426             throw new RuntimeException (e);
00427         }
00428         return USetTimer (period, obj, m);
00429     }
00430 
00431     protected String USetTimer (double period,
00432                                 Object obj,
00433                                 String method_name)
00434     {
00435         Method m = findMethodFromName (obj, method_name);
00436         return USetTimer (period, obj, m);
00437     }
00438 
00439     /// --------------- ///
00440     ///                 ///
00441     ///     UBindVar    ///
00442     ///                 ///
00443     /// --------------- ///
00444 
00445     protected void UBindVar (UVar v, String name) {
00446         v.init (get__name (), name, getCtx_());
00447     }
00448 
00449     /// --------------- ///
00450     ///                 ///
00451     ///   UBindEvent    ///
00452     ///                 ///
00453     /// --------------- ///
00454 
00455     protected void UBindEvent (UEvent e, String name) {
00456         e.init (get__name(), name, getCtx_());
00457     }
00458 
00459     /// --------------- ///
00460     ///                 ///
00461     ///      UStart     ///
00462     ///                 ///
00463     /// --------------- ///
00464 
00465     public static void UStartRename (Class uobject_cls, String urbi_name)
00466     {
00467         /// Get the constructor that take a string
00468         Class string_cls;
00469         Constructor uobject_ctor;
00470         try {
00471             string_cls = Class.forName ("java.lang.String");
00472             Class[] params = {string_cls};
00473             uobject_ctor = uobject_cls.getConstructor (params);
00474         }
00475         catch (java.lang.ClassNotFoundException e) {
00476             throw new RuntimeException (e);
00477         }
00478         catch (java.lang.NoSuchMethodException e) {
00479             throw new RuntimeException (e);
00480         }
00481         starterList.add (new UObjectStarter (urbi_name, uobject_ctor));
00482     }
00483 
00484     public static void UStart (Class uobject_cls)
00485     {
00486         String name = uobject_cls.getName ();
00487         String[] urbi_name = name.split ("\\.");
00488         if (urbi_name.length > 0) {
00489             UStartRename(uobject_cls, urbi_name[urbi_name.length - 1]);
00490         }
00491     }
00492 
00493     public static void main (String argv[]) {
00494         /// Add program name at the begining of the argv array
00495         String[] new_argv = new String [argv.length + 1];
00496         for (int i = 0; i < argv.length; ++i) {
00497             new_argv[i + 1] = argv[i];
00498         }
00499         new_argv[0] = "UObject";
00500         UrbiRoot root = new UrbiRoot("urbi-launch", false);
00501         urbi.main (new_argv.length, new_argv, root);
00502     }
00503 
00504     /// internal
00505     private void checkNotifyRegisteredMethod (Method m, String notifyName)
00506         throws RuntimeException
00507     {
00508         Class[] p = m.getParameterTypes();
00509         if (p.length > 1) {
00510 
00511             String msg = "Function \""+ m.getName () + "\" has " + p.length
00512                 + " arguments. You can't register a function with more than "
00513                 + "1 arguments with " + notifyName + ".";
00514             throw new RuntimeException(msg);
00515         }
00516         if ((m.getReturnType() != int.class) && (m.getReturnType() != void.class)) {
00517             String msg = "Return type of the function \""
00518                 + m.getName () + "\" is "+ m.getReturnType().getName ()
00519                 + ". You can only register functions with a return type of"
00520                 + " type int or void with " + notifyName + ".";
00521             throw new RuntimeException(msg);
00522         }
00523     }
00524 
00525 
00526     /// internal
00527     private static String getMethodBytecodeSignature(Method m)
00528     {
00529         return getMethodBytecodeSignature (m.getParameterTypes(),
00530                                            m.getReturnType());
00531     }
00532 
00533     /// internal
00534     private static String getMethodBytecodeSignature(Class[] parameterTypes,
00535                                                      Class returnType)
00536     {
00537         StringBuffer sb = new StringBuffer();
00538         sb.append("(");
00539         for (int i=0; i<parameterTypes.length; i++)
00540             sb.append(Type.make(parameterTypes[i]).getSignature());
00541         sb.append(")");
00542         sb.append(Type.make(returnType).getSignature());
00543         return sb.toString();
00544     }
00545 
00546     /// internal
00547     protected Method findMethodFromName (Object obj, String method_name)
00548         throws RuntimeException
00549     {
00550         Class obj_cls = obj.getClass ();
00551         Method[] methods = obj_cls.getMethods();
00552         Method res = null;
00553         for (int i = 0; i < methods.length; ++i) {
00554             if (methods[i].getName() == method_name) {
00555                 if (res == null)
00556                     res = methods[i];
00557                 else
00558                 {
00559                     String msg = "There are several methods with name "
00560                         + method_name + " in UObject "
00561                         + obj_cls.getName ()
00562                         + ", please specify the arguments to avoid ambiguities";
00563                     throw new RuntimeException(msg);
00564                 }
00565             }
00566         }
00567         if (res == null) {
00568             String msg = "Can't find method with name "
00569                 + method_name + " in object "
00570                 + obj_cls.getName ();
00571             throw new RuntimeException(msg);
00572         }
00573         return res;
00574     }
00575 
00576     /// internal
00577     protected Class[] stringTypeToClassType (String[] typeArray)
00578         throws java.lang.ClassNotFoundException
00579     {
00580         Class[] params = new Class[typeArray.length];
00581         for (int i = 0; i < typeArray.length; ++i) {
00582             params[i] = Class.forName (typeArray[i]);
00583         }
00584         return params;
00585     }
00586 
00587     public void call(String object, String method, UValue v1, UValue v2, UValue v3, UValue v4, UValue v5, UValue v6) {
00588         super.call(object, method, new UAutoValue(v1), new UAutoValue(v2),
00589                    new UAutoValue(v3), new UAutoValue(v4), new UAutoValue(v5),
00590                    new UAutoValue(v6));
00591     }
00592 
00593     public void call(String object, String method, UValue v1, UValue v2, UValue v3, UValue v4, UValue v5) {
00594         super.call(object, method, new UAutoValue(v1), new UAutoValue(v2),
00595                    new UAutoValue(v3), new UAutoValue(v4), new UAutoValue(v5));
00596     }
00597 
00598     public void call(String object, String method, UValue v1, UValue v2, UValue v3, UValue v4) {
00599         super.call(object, method, new UAutoValue(v1), new UAutoValue(v2),
00600                    new UAutoValue(v3), new UAutoValue(v4));
00601     }
00602 
00603     public void call(String object, String method, UValue v1, UValue v2, UValue v3) {
00604         super.call(object, method, new UAutoValue(v1), new UAutoValue(v2),
00605                    new UAutoValue(v3));
00606     }
00607 
00608     public void call(String object, String method, UValue v1, UValue v2) {
00609         super.call(object, method, new UAutoValue(v1), new UAutoValue(v2));
00610     }
00611 
00612     public void call(String object, String method, UValue v1) {
00613         super.call(object, method, new UAutoValue(v1));
00614     }
00615 
00616     public void call(String object, String method) {
00617         super.call(object, method);
00618     }
00619 
00620     private static LinkedList<UObjectStarter> starterList = new LinkedList<UObjectStarter>();
00621 }