Exemplo n.º 1
0
 /**
  * Returns object with specific ID.
  *
  * @param ID Id of the object that is to be returned.
  * @return the object with specified ID
  */
 public Object getObject(int ID) {
   ObjectEntry obj = objects.getById(ID);
   Object result = null;
   if (obj != null) {
     result = obj.getObject();
   }
   return result;
 }
Exemplo n.º 2
0
  /**
   * Returns a collection containing all instances controlled by this inspector.
   *
   * @return a collection containing all instances controlled by this inspector
   */
  public Collection<Object> getObjects() {
    ArrayList<Object> result = new ArrayList<Object>();

    for (ObjectEntry o : objects) {
      result.add(o.getObject());
    }

    return result;
  }
Exemplo n.º 3
0
  /**
   * Returns the ID of an object.
   *
   * @param o the object thats ID is to be returned
   * @return the ID of the object if the object could be found; <code>null</code> otherwise
   */
  public Integer getObjectID(Object o) {
    // search for an already existing reference to object o
    for (ObjectEntry oE : objects) {
      if (oE.getObject() == o) {
        //                System.out.println(">> Object found!");
        return oE.getID();
      }
    }

    return null;
  }
Exemplo n.º 4
0
  /**
   * Returns a collection containing all instances of a given class object.
   *
   * @param name the name of the class object
   * @return a collection containing all instances of a given class object
   */
  public Collection<Object> getObjectsByClassName(String name) {
    ArrayList<Object> result = new ArrayList<Object>();

    for (ObjectEntry o : objects) {
      if (o.getObject() != null && o.getObject().getClass().getName().equals(name)) {
        result.add(o.getObject());
        //                System.out.println("TRUE: O1: " + o.getObject()
        //                        + ", " + o.getObject().getClass().getName()
        //                        + " O2: " + name);
      } else {
        //                System.out.println("FALSE: O1: " + o.getObject()
        //                        + ", " + o.getObject().getClass().getName()
        //                        + " O2: " + name);
      }
    }

    return result;
  }
Exemplo n.º 5
0
  /**
   * Adds an object to the inspector.
   *
   * @param o the object that is to be added
   * @param objID the ID of the object (only used when loading from file or if replacing instance,
   *     because then we need to restore the ID) if <code>null</code> the id will be generated
   *     (usual case)
   * @return <code>true</code>, if successfully added, i.e. is not already atached to the inspector;
   *     <code>false</code> otherwise
   */
  public boolean addObject(Object o, Integer objID) {

    System.out.println(">> Add Object: " + o);

    boolean alreadyAdded = false;

    // search for an already existing reference to object o
    for (ObjectEntry oE : objects) {
      if (oE.getObject() == o) {
        System.out.println(" --> Object already added!");
        alreadyAdded = true;
        break;
      }
    }

    // Object annotations
    ObjectInfo objectInfo = null;

    Annotation[] objectAnnotations = o.getClass().getAnnotations();

    for (Annotation a : objectAnnotations) {
      if (a.annotationType().equals(ObjectInfo.class)) {
        objectInfo = (ObjectInfo) a;
        break;
      }
    }

    if (objectInfo != null && objectInfo.instances() <= numberOfInstances(o.getClass().getName())) {
      System.err.println(
          ">> Cannot add Object: only " + objectInfo.instances() + " instances allowed!");
      return false;
    }

    if (!alreadyAdded) {
      ObjectDescription oDesc = generateObjectDescription(o, objID, true);
      if (oDesc == null) {
        return false;
      }
      getObjectDescriptions().add(oDesc);
    } // end if(!alredyAdded)

    return !alreadyAdded;
  }
Exemplo n.º 6
0
  /**
   * Removes object with specific ID from the inspector.
   *
   * @param ID the ID of the object that is to be removed
   */
  public void removeObject(int ID) {

    // remove object entry
    ObjectEntry removedElement1 = null;
    for (ObjectEntry oEntry : objects) {
      if (oEntry.getID() == ID) {
        removedElement1 = oEntry;
        break;
      }
    }
    objects.remove(removedElement1);

    // remove object description
    ObjectDescription removedElement2 = null;
    for (ObjectDescription oDesc : objectDescriptions) {
      if (oDesc.getID() == ID) {
        removedElement2 = oDesc;
        break;
      }
    }
    objectDescriptions.remove(removedElement2);
  }
Exemplo n.º 7
0
  public ObjectDescription generateObjectDescription(Object o, Integer objID, boolean addObject) {

    ObjectDescription result = null;

    // Object annotations
    ObjectInfo objectInfo = null;

    Annotation[] objectAnnotations = o.getClass().getAnnotations();

    for (Annotation a : objectAnnotations) {
      if (a.annotationType().equals(ObjectInfo.class)) {
        objectInfo = (ObjectInfo) a;
        break;
      }
    }

    if (addObject) {
      ObjectEntry oEntry = new ObjectEntry(o);
      if (objID != null) {
        objects.addWithID(oEntry, objID);
      } else {
        objects.add(oEntry);
        objID = oEntry.getID();
      }
    }

    boolean hasCustomReferenceMethod = false;

    // we need special treatment for proxy objects
    if (o instanceof ProxyObject) {
      ProxyObject proxy = (ProxyObject) o;

      result = proxy.getObjectDescription();

      result.setName(o.getClass().getName());

      result.setID(objID);

      // update objID of methods
      for (MethodDescription mDesc : result.getMethods()) {
        mDesc.setObjectID(objID);
      }

    } else {
      result = new ObjectDescription();
      result.setInfo(objectInfo);

      result.setName(o.getClass().getName());

      result.setID(objID);

      Class<?> c = o.getClass();

      Method[] theMethods = null;

      if (objectInfo != null && objectInfo.showInheritedMethods()) {
        theMethods = c.getMethods();
      } else {
        ArrayList<Method> methods = new ArrayList<Method>();

        // methods declared by class c
        for (Method m : c.getDeclaredMethods()) {
          methods.add(m);
        }

        // methods declared in superclasses of c
        for (Method m : c.getMethods()) {
          MethodInfo info = m.getAnnotation(MethodInfo.class);
          // if m is marked as inheritGUI this method will
          // be visualized even if it is a method declared
          // in a superclass of c
          if (info != null && info.inheritGUI()) {
            if (!methods.contains(m)) {
              methods.add(m);
            }
          }
        }

        Method[] tmpArray = new Method[methods.size()];

        theMethods = methods.toArray(tmpArray);
      }

      for (int i = 0; i < theMethods.length; i++) {

        // filter groovy object specific methods
        String method = theMethods[i].getName();
        boolean isGroovyObject = o instanceof GroovyObject;
        boolean isGroovyMethod =
            method.equals("setProperty")
                || method.equals("getProperty")
                || method.equals("invokeMethod")
                || method.equals("getMetaClass")
                || method.equals("setMetaClass"); // ||
        //                            // the following methods are only present
        //                            // for Groovy >= 1.6
        //                            method.equals("super$1$wait") ||
        //                            method.equals("super$1$toString") ||
        //                            method.equals("super$1$notify") ||
        //                            method.equals("super$1$notifyAll") ||
        //                            method.equals("super$1$getClass") ||
        //                            method.equals("super$1$equals") ||
        //                            method.equals("super$1$clone") ||
        //                            method.equals("super$1$hashCode") ||
        //                            method.equals("super$1$finalize");

        // For Groovy >= 1.6 private methods have $ sign in their
        // name, e.g.,
        //   private doSomething()
        // will be changed to
        //   this$2$doSomething()
        // which is unfortunately accessible and thus will be
        // visualized by vrl.
        // To prevent groovys strange behavior
        // methods with $ sign are ignored and treated as private.
        boolean isPrivateGroovyMethod = method.contains("$");

        // TODO improve that code!
        if ((isGroovyObject && isGroovyMethod) || isPrivateGroovyMethod) {
          continue;
        }

        Class[] parameterTypes = theMethods[i].getParameterTypes();

        Annotation[][] allParameterAnnotations = theMethods[i].getParameterAnnotations();

        ArrayList<String> paramNames = new ArrayList<String>();

        ArrayList<ParamInfo> paramAnnotations = new ArrayList<ParamInfo>();
        ArrayList<ParamGroupInfo> paramGroupAnnotations = new ArrayList<ParamGroupInfo>();

        // retrieving annotation information for each parameter
        for (int j = 0; j < allParameterAnnotations.length; j++) {

          Annotation[] annotations = allParameterAnnotations[j];

          // add name element and set it to null
          // because we don't know if parameter j is annotated
          paramNames.add(null);
          paramAnnotations.add(null);
          paramGroupAnnotations.add(null);

          // check all annotations of parameter j
          // if we have a ParamInfo annotation retrieve data
          // and store it as element of paramName
          for (Annotation a : annotations) {
            if (a.annotationType().equals(ParamInfo.class)) {
              ParamInfo n = (ParamInfo) a;
              if (!n.name().equals("")) {
                paramNames.set(j, n.name());
              }
              paramAnnotations.set(j, n);
            } else {
              if (a.annotationType().equals(ParamGroupInfo.class)) {
                ParamGroupInfo n = (ParamGroupInfo) a;
                paramGroupAnnotations.set(j, n);
              }
            }
          } // end for a
        } // end for j

        // convert list to array
        String[] parameterNames = paramNames.toArray(new String[paramNames.size()]);

        ParamInfo[] parameterAnnotations =
            paramAnnotations.toArray(new ParamInfo[paramAnnotations.size()]);

        ParamGroupInfo[] parameterGroupAnnotations =
            paramGroupAnnotations.toArray(new ParamGroupInfo[paramGroupAnnotations.size()]);

        Class returnType = theMethods[i].getReturnType();

        String methodString = theMethods[i].getName();
        String methodTitle = "";
        String returnValueName = null;

        int modifiers = theMethods[i].getModifiers();

        String modifierString = Modifier.toString(modifiers);

        // Method Annotations
        Annotation[] annotations = theMethods[i].getAnnotations();
        MethodInfo methodInfo = null;

        OutputInfo outputInfo = null;

        boolean interactive = true;
        boolean hide = false;

        for (Annotation a : annotations) {
          if (a.annotationType().equals(MethodInfo.class)) {
            MethodInfo n = (MethodInfo) a;

            methodTitle = n.name();
            interactive = n.interactive();
            hide = n.hide();
            returnValueName = n.valueName();

            methodInfo = n;
          }

          if (a.annotationType().equals(OutputInfo.class)) {
            outputInfo = (OutputInfo) a;
          }
        }

        if (theMethods[i].getAnnotation(ReferenceMethodInfo.class) != null) {
          hasCustomReferenceMethod = true;
          MethodDescription customReferenceMethod =
              new MethodDescription(
                  objID,
                  0,
                  methodString,
                  methodTitle,
                  null,
                  parameterTypes,
                  parameterNames,
                  parameterAnnotations,
                  parameterGroupAnnotations,
                  returnType,
                  returnValueName,
                  interactive,
                  methodInfo,
                  outputInfo);

          if (customReferenceMethod.getParameterTypes() == null
              || customReferenceMethod.getParameterTypes().length != 1) {
            throw new IllegalArgumentException(
                " Cannot to use "
                    + "\""
                    + methodString
                    + "\" as reference method"
                    + " because the number of parameters does not"
                    + " match. Exactly one parameter must be"
                    + " provided.");
          }

          if (customReferenceMethod.getReturnType() == void.class
              || customReferenceMethod.getReturnType().isPrimitive()) {
            throw new IllegalArgumentException(
                " Cannot to use "
                    + "\""
                    + methodString
                    + "\" as reference method"
                    + " because it does not return an object."
                    + " Returning primitives or void is not"
                    + " allowed.");
          }

          customReferenceMethod.setMethodType(MethodType.CUSTOM_REFERENCE);

          result.addMethod(customReferenceMethod);
        } //
        // TODO: at the moment method id is always 0 and will only be
        //      set from corresponding method representations
        //
        //      is it necessary to change that?
        else if (modifierString.contains("public")
            && (methodInfo == null || !methodInfo.ignore())) {
          result.addMethod(
              new MethodDescription(
                  objID,
                  0,
                  methodString,
                  methodTitle,
                  null,
                  parameterTypes,
                  parameterNames,
                  parameterAnnotations,
                  parameterGroupAnnotations,
                  returnType,
                  returnValueName,
                  interactive,
                  methodInfo,
                  outputInfo));
        }
      } // end for i

      // Object name
      if (objectInfo != null && !objectInfo.name().equals("")) {
        result.setName(objectInfo.name());
      }
    } // end else if (o instanceof ProxyObject)

    if (!hasCustomReferenceMethod) {
      result.addMethod(MethodDescription.createReferenceMethod(objID, o.getClass()));
    }

    return result;
  }