コード例 #1
0
ファイル: BaseTypeChecker.java プロジェクト: jsinglet/checklt
  /**
   * Invokes the constructor belonging to the class named by {@code name} having the given parameter
   * types on the given arguments. Returns {@code null} if the class cannot be found, or the
   * constructor does not exist or cannot be invoked on the given arguments.
   *
   * @param <T> the type to which the constructor belongs
   * @param name the name of the class to which the constructor belongs
   * @param paramTypes the types of the constructor's parameters
   * @param args the arguments on which to invoke the constructor
   * @return the result of the constructor invocation on {@code args}, or null if the constructor
   *     does not exist or could not be invoked
   */
  @SuppressWarnings("unchecked")
  public static <T> T invokeConstructorFor(String name, Class<?>[] paramTypes, Object[] args) {

    // Load the class.
    Class<T> cls = null;
    try {
      cls = (Class<T>) Class.forName(name);
    } catch (Exception e) {
      // no class is found, simply return null
      return null;
    }

    assert cls != null : "reflectively loading " + name + " failed";

    // Invoke the constructor.
    try {
      Constructor<T> ctor = cls.getConstructor(paramTypes);
      return ctor.newInstance(args);
    } catch (Throwable t) {
      if (t instanceof InvocationTargetException) {
        Throwable err = t.getCause();
        String msg;
        if (err instanceof CheckerError) {
          msg = err.getMessage();
        } else {
          msg = err.toString();
        }
        SourceChecker.errorAbort(
            "InvocationTargetException when invoking constructor for class "
                + name
                + "; Underlying cause: "
                + msg,
            t);
      } else {
        SourceChecker.errorAbort(
            "Unexpected "
                + t.getClass().getSimpleName()
                + " for "
                + "class "
                + name
                + " when invoking the constructor; parameter types: "
                + Arrays.toString(paramTypes),
            // + " and args: " + Arrays.toString(args),
            t);
      }
      return null; // dead code
    }
  }
コード例 #2
0
ファイル: BaseTypeChecker.java プロジェクト: jsinglet/checklt
 @Override
 public void initChecker() {
   super.initChecker();
   this.supportedQuals = this.createSupportedTypeQualifiers();
   this.qualHierarchy = this.getQualifierHierarchy();
   this.typeHierarchy = this.createTypeHierarchy();
 }