Esempio n. 1
0
 /** evaluate the link function */
 public static Data link(VMethod m, Object[] o) throws VisADException {
   Data ans = null;
   if (o != null) {
     for (int i = 0; i < o.length; i++) {
       // convert VRealTypes to RealTypes
       if (o[i] instanceof VRealType) {
         o[i] = ((VRealType) o[i]).getRealType();
       }
     }
   }
   try {
     ans = (Data) FormulaUtil.invokeMethod(m.getMethod(), o);
   } catch (ClassCastException exc) {
     if (FormulaVar.DEBUG) exc.printStackTrace();
     throw new VisADException("Link error: invalid linked method");
   } catch (IllegalAccessException exc) {
     if (FormulaVar.DEBUG) exc.printStackTrace();
     throw new VisADException("Link error: cannot access linked method");
   } catch (IllegalArgumentException exc) {
     if (FormulaVar.DEBUG) exc.printStackTrace();
     throw new VisADException("Link error: bad method argument");
   } catch (InvocationTargetException exc) {
     if (FormulaVar.DEBUG) exc.getTargetException().printStackTrace();
     throw new VisADException("Link error: linked method threw an exception");
   }
   if (ans == null) {
     throw new VisADException("Link error: linked method returned null data");
   }
   return ans;
 }
Esempio n. 2
0
  private void bindEventHandler(
      Component componentRoot, Object controller, Method method, UiHandler eventListener) {
    String componentId = eventListener.value();
    Component component = Clara.findComponentById(componentRoot, componentId);
    if (component == null) {
      throw new BinderException("No component found for id: " + componentId + ".");
    }

    Class<?> eventType =
        (method.getParameterTypes().length > 0 ? method.getParameterTypes()[0] : null);
    if (eventType == null) {
      throw new BinderException("Couldn't figure out event type for method " + method + ".");
    }

    Method addListenerMethod = getAddListenerMethod(component.getClass(), eventType);
    if (addListenerMethod != null) {
      try {
        Object listener =
            createListenerProxy(
                addListenerMethod.getParameterTypes()[0], eventType, method, controller);
        addListenerMethod.invoke(component, listener);
        // TODO exception handling
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
Esempio n. 3
0
  private void bindDataSource(
      Component componentRoot, Object controller, Method method, UiDataSource dataSource) {
    String componentId = dataSource.value();
    Component component = Clara.findComponentById(componentRoot, componentId);
    Class<?> dataSourceClass = method.getReturnType();

    try {
      // Vaadin data model consists of Property/Item/Container
      // objects and each of them have a Viewer interface.
      if (isContainer(dataSourceClass) && component instanceof Container.Viewer) {
        ((Container.Viewer) component)
            .setContainerDataSource((Container) method.invoke(controller));
      } else if (isProperty(dataSourceClass) && component instanceof Property.Viewer) {
        ((Property.Viewer) component).setPropertyDataSource((Property) method.invoke(controller));
      } else if (isItem(dataSourceClass) && component instanceof Item.Viewer) {
        ((Item.Viewer) component).setItemDataSource((Item) method.invoke(controller));
      }
      // TODO exception handling
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
  }
  public void createDataset(Dataset dataset) {
    try {
      MongoProvider provider = new MongoProvider("localhost", 10000, "test", null, null);
      dsDao = new DatasetDao();
      orgDao = new OrganizationDao();
      org = new Organization();
      org.setOrganizationId("orgId");
      ArrayList<Dataset> datasets = new ArrayList<>();
      datasets.add(dataset);
      org.setDatasets(datasets);
      org.setOrganizationUri("testUri");
      org.setHarvestingMetadata(new HarvestingMetadata());

      Field field;
      try {
        field = DatasetDao.class.getDeclaredField("provider");
        field.setAccessible(true);
        field.set(dsDao, provider);

        field = OrganizationDao.class.getDeclaredField("provider");
        field.setAccessible(true);
        field.set(orgDao, provider);
      } catch (NoSuchFieldException e) {
        e.printStackTrace();
      } catch (SecurityException e) {
        e.printStackTrace();
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
      dsDao.createDatasetForOrganization(org, dataset);
      orgDao.create(org);
      System.out.println(
          "Dataset: "
              + dsDao.getByName(dataset.getName()).getName()
              + " is saved in a virtual MongoDB");
      try {
        List<Dataset> allDatasetsByOrganization = orgDao.getAllDatasetsByOrganization("orgId");
        System.out.println("Datasets:");
        for (Dataset ds : allDatasetsByOrganization) {
          System.out.println(ds.getName());
        }
      } catch (NoOrganizationExceptionFound e) {
        e.printStackTrace();
      }
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }
Esempio n. 5
0
  private void bindField(
      Component componentRoot, Object controller, Field field, UiField annotation) {
    String componentId = annotation.value();
    Component component = Clara.findComponentById(componentRoot, componentId);
    if (component == null) {
      throw new BinderException("No component found for id: " + componentId + ".");
    }

    try {
      field.setAccessible(true);
      field.set(controller, component);
      // TODO exception handling
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
  }
Esempio n. 6
0
 protected synchronized Message receiveMessage() throws IOException {
   if (messageBuffer.size() > 0) {
     Message m = (Message) messageBuffer.get(0);
     messageBuffer.remove(0);
     return m;
   }
   try {
     InetSocketAddress remoteAddress = (InetSocketAddress) channel.receive(receiveBuffer);
     if (remoteAddress != null) {
       int len = receiveBuffer.position();
       receiveBuffer.rewind();
       receiveBuffer.get(buf, 0, len);
       try {
         IP address = IP.fromInetAddress(remoteAddress.getAddress());
         int port = remoteAddress.getPort();
         extractor.appendData(buf, 0, len, new SocketDescriptor(address, port));
         receiveBuffer.clear();
         extractor.updateAvailableMessages();
         return extractor.nextMessage();
       } catch (EOFException exc) {
         exc.printStackTrace();
         System.err.println(buf.length + ", " + len);
       } catch (InvocationTargetException exc) {
         exc.printStackTrace();
       } catch (IllegalAccessException exc) {
         exc.printStackTrace();
       } catch (InstantiationException exc) {
         exc.printStackTrace();
       } catch (IllegalArgumentException e) {
         e.printStackTrace();
       } catch (InvalidCompressionMethodException e) {
         e.printStackTrace();
       }
     }
   } catch (ClosedChannelException exc) {
     if (isKeepAlive()) {
       throw exc;
     }
   }
   return null;
 }
Esempio n. 7
0
  /**
   * オブジェクトのメソッドを呼び出す
   *
   * @param Object o メソッドを呼び出したいオブジェクト
   * @param String methodName 呼び出したいメソッドの名前
   * @param Object... args メソッドのパラメータ
   * @throws NoSuchMethodException
   */
  public static Object invoke(Object o, String methodName, Object... args)
      throws SecurityException, ClassNotFoundException, NoSuchMethodException {
    StringBuffer tempMessage = new StringBuffer();
    Method method = null;

    //		//argsArray:メソッドの引数の型配列
    //		Class<?>[] argsArray = new Class<?>[args.length];
    //		for(int i = 0; i < args.length; i++){
    //			argsArray[i] = getClassObject(args[i]);
    //			//スーパークラスを引数にとれるように修正
    //		}
    //		try {
    //			method = o.getClass().getMethod(methodName, argsArray);
    //		} catch (NoSuchMethodException e) {
    //			try{
    //				method = o.getClass().getDeclaredMethod(methodName, argsArray);
    //			}catch (NoSuchMethodException e2) {
    //					message = "NoSuchMethodException\r\n";
    //					display.append(message);
    //			}
    //		}
    /////////////////////////////////////////
    // argsArray:メソッドの引数の型配列
    Class<?>[] argsArray = new Class<?>[args.length];
    for (int i = 0; i < args.length; i++) {
      argsArray[i] = getClassObject(args[i]);
    }
    method = getMethod(o, args, methodName, argsArray);

    ////////////////////////////////////////
    tempMessage.append("method: " + method.getName() + " is called.\r\n");

    if (Modifier.isPrivate(method.getModifiers())) {
      method.setAccessible(true);
    }
    if (o.equals(null)) {
      if (Modifier.isStatic(method.getModifiers()) == false) {
        throw new NoSuchMethodException();
      }
    }

    try {
      Type returnType = method.getGenericReturnType();
      tempMessage.append("return type: " + returnType.toString() + "\r\n");
      if (returnType.equals(Void.TYPE)) {
        tempMessage.append("return value: " + "なし\r\n");
      } else {
        tempMessage.append("return value: " + method.invoke(o, args).toString() + "\r\n");
      }
      message = tempMessage.toString();
      display.append(message);
      return method.invoke(o, args);
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
      message = tempMessage.append("IllegalArgumentException\r\n").toString();
      display.append(message);
    } catch (IllegalAccessException e) {
      e.printStackTrace();
      message = tempMessage.append("IllegalAccessException\r\n").toString();
      display.append(message);
    } catch (InvocationTargetException e) {
      e.printStackTrace();
      tempMessage.append("InvocationTargetException\r\n");
      message = tempMessage.append(e.getCause().toString() + "\r\n").toString();
      display.append(message);
    }
    return null;
  }
  public static void main(String[] args) {

    // Obtain the class object if we know the name of the class
    Class rental = RentCar.class;
    try {
      // get the absolute name of the class
      String rentalClassPackage = rental.getName();
      System.out.println("Class Name is: " + rentalClassPackage);

      // get the simple name of the class (without package info)
      String rentalClassNoPackage = rental.getSimpleName();
      System.out.println("Class Name without package is: " + rentalClassNoPackage);

      // get the package name of the class
      Package rentalPackage = rental.getPackage();
      System.out.println("Package Name is: " + rentalPackage);

      // get all the constructors of the class
      Constructor[] constructors = rental.getConstructors();
      System.out.println("Constructors are: " + Arrays.toString(constructors));

      // get constructor with specific argument
      Constructor constructor = rental.getConstructor(Integer.TYPE);

      // initializing an object of the RentCar class
      RentCar rent = (RentCar) constructor.newInstance(455);

      // get all methods of the class including declared methods of
      // superclasses
      // in that case, superclass of RentCar is the class java.lang.Object
      Method[] allmethods = rental.getMethods();
      System.out.println("Methods are: " + Arrays.toString(allmethods));
      for (Method method : allmethods) {
        System.out.println("method = " + method.getName());
      }

      // get all methods declared in the class
      // but excludes inherited methods.
      Method[] declaredMethods = rental.getDeclaredMethods();
      System.out.println("Declared Methods are: " + Arrays.toString(declaredMethods));
      for (Method dmethod : declaredMethods) {
        System.out.println("method = " + dmethod.getName());
      }

      // get method with specific name and parameters
      Method oneMethod = rental.getMethod("computeRentalCost", new Class[] {Integer.TYPE});
      System.out.println("Method is: " + oneMethod);

      // call computeRentalCost method with parameter int
      oneMethod.invoke(rent, 4);

      // get all the parameters of computeRentalCost
      Class[] parameterTypes = oneMethod.getParameterTypes();
      System.out.println(
          "Parameter types of computeRentalCost() are: " + Arrays.toString(parameterTypes));

      // get the return type of computeRentalCost
      Class returnType = oneMethod.getReturnType();
      System.out.println("Return type is: " + returnType);

      // gets all the public member fields of the class RentCar
      Field[] fields = rental.getFields();

      System.out.println("Public Fields are: ");
      for (Field oneField : fields) {
        // get public field name
        Field field = rental.getField(oneField.getName());
        String fieldname = field.getName();
        System.out.println("Fieldname is: " + fieldname);

        // get public field type
        Object fieldType = field.getType();
        System.out.println("Type of field " + fieldname + " is: " + fieldType);

        // get public field value
        Object value = field.get(rent);
        System.out.println("Value of field " + fieldname + " is: " + value);
      }

      // How to access private member fields of the class

      // getDeclaredField() returns the private field
      Field privateField = RentCar.class.getDeclaredField("type");

      String name = privateField.getName();
      System.out.println("One private Fieldname is: " + name);
      // makes this private field instance accessible
      // for reflection use only, not normal code
      privateField.setAccessible(true);

      // get the value of this private field
      String fieldValue = (String) privateField.get(rent);
      System.out.println("fieldValue = " + fieldValue);

    } catch (NoSuchFieldException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
  }