Ejemplo n.º 1
0
 private void release(RevWalk revWalk) {
   try {
     MethodUtils.invokeMethod(revWalk, "release", null); // $NON-NLS-1$
   } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
     try {
       MethodUtils.invokeMethod(revWalk, "close", null); // $NON-NLS-1$
     } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e1) {
       StatusHandler.log(
           new Status(
               IStatus.ERROR,
               ID_PLUGIN,
               "Failed to release revWalk " + revWalk,
               e1)); //$NON-NLS-1$
     }
   }
 }
Ejemplo n.º 2
0
 private static Object parsePrimitive(Object val, Class toClass) {
   try {
     return MethodUtils.invokeStaticMethod(toClass, "valueOf", val);
   } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
     throw unableToParseException(val, toClass, e);
   }
 }
  public final Object doRequest(String actionName, String method, Object... args) {
    LOG.info(
        String.format(
            "##################################> Processando request '%s.%s'\n",
            actionName, method));
    Object ret = null;

    if (actionName != null && !actionMap.containsKey(actionName)) {
      throw new AroundException(String.format("'%s' NAO EXISTE", actionName));
    }
    BaseAction act = actionMap.get(actionName);

    try {
      ret = MethodUtils.invokeExactMethod(act, method, args);
      LOG.info(String.format("Metodo '%s.%s' chamado", actionName, method));
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
      LOG.trace(String.format("Metodo '%s' da action '%s' nao encontrado!", method, actionName), e);
    }

    LOG.info(
        String.format(
            "##################################> FINAL Processando request '%s.%s'\n",
            actionName, method));

    return ret;
  }
Ejemplo n.º 4
0
  /**
   * Attempts to parse given String to an instance of a given class. The class may also have a
   * generic type. It will throw {@link java.lang.IllegalStateException} in case this method was not
   * able to parse the value.
   *
   * @param str String to parse
   * @param toClass a class to turn value into
   * @param generic generic class
   * @return parsed value, an instance of the given class
   */
  public static Object parseString(String str, Class<?> toClass, Class<?> generic) {
    if (isBlank(str, toClass)) {
      return (String.class.isAssignableFrom(toClass)) ? "" : null;
    }

    if (isDateOrPeriod(toClass)) {
      return parseDateOrPeriod(toClass, str);
    }

    try {
      if (toClass.isEnum()) {
        Class<? extends Enum> enumClass = (Class<? extends Enum>) toClass;
        return Enum.valueOf(enumClass, str);
      }

      if (Collection.class.isAssignableFrom(toClass)) {
        return parseStringToCollection(str, toClass, generic);
      } else if (Map.class.isAssignableFrom(toClass)) {
        return parseStringToMap(str);
      } else if (Locale.class.isAssignableFrom(toClass)) {
        return LocaleUtils.toLocale(str);
      } else if (Byte[].class.isAssignableFrom(toClass)) {
        return ArrayUtils.toObject(str.getBytes());
      } else {
        return MethodUtils.invokeStaticMethod(toClass, "valueOf", str);
      }
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
      throw new IllegalStateException("Unable to parse value " + str + " to " + toClass, e);
    }
  }
Ejemplo n.º 5
0
  private void setRelationProperty(Object instance, FieldRecord fieldRecord)
      throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException,
          IllegalAccessException, InstantiationException, CannotCompileException {
    String fieldName = fieldRecord.getName();
    String methodName = MemberUtil.getSetterName(fieldName);
    Class<?> clazz = instance.getClass().getClassLoader().loadClass(instance.getClass().getName());
    Field field = FieldUtils.getField(clazz, fieldName, true);
    Class<?> parameterType = field.getType();
    Object value = null;
    MotechDataService serviceForRelatedClass = null;
    TypeDto type = getType(fieldRecord);

    if (StringUtils.isNotEmpty(ObjectUtils.toString(fieldRecord.getValue()))) {
      if (type.equals(TypeDto.ONE_TO_MANY_RELATIONSHIP)
          || type.equals(TypeDto.MANY_TO_MANY_RELATIONSHIP)) {
        Class<?> genericType =
            (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
        serviceForRelatedClass =
            DataServiceHelper.getDataService(bundleContext, genericType.getName());
      } else if (type.equals(TypeDto.MANY_TO_ONE_RELATIONSHIP)
          || type.equals(TypeDto.ONE_TO_ONE_RELATIONSHIP)) {
        serviceForRelatedClass =
            DataServiceHelper.getDataService(bundleContext, parameterType.getName());
      }

      value = buildRelatedInstances(serviceForRelatedClass, parameterType, fieldRecord.getValue());
    }

    Method method = MethodUtils.getAccessibleMethod(instance.getClass(), methodName, parameterType);
    invokeMethod(method, instance, value, methodName, fieldName);
  }
Ejemplo n.º 6
0
  private void setProperty(
      Object instance, FieldRecord fieldRecord, MotechDataService service, Long deleteValueFieldId)
      throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException,
          IllegalAccessException {
    String fieldName = fieldRecord.getName();
    TypeDto type = getType(fieldRecord);

    String methodName = "set" + StringUtils.capitalize(fieldName);
    ComboboxHolder holder = type.isCombobox() ? new ComboboxHolder(instance, fieldRecord) : null;
    String methodParameterType = getMethodParameterType(type, holder);

    ClassLoader classLoader = instance.getClass().getClassLoader();

    Class<?> parameterType;
    Object parsedValue;
    if (Byte[].class.getName().equals(methodParameterType)
        || byte[].class.getName().equals(methodParameterType)) {
      parameterType = getCorrectByteArrayType(methodParameterType);

      parsedValue = parseBlobValue(fieldRecord, service, fieldName, deleteValueFieldId, instance);
    } else {
      parameterType = classLoader.loadClass(methodParameterType);

      parsedValue = parseValue(holder, methodParameterType, fieldRecord, classLoader);
    }

    validateNonEditableField(fieldRecord, instance, parsedValue);

    Method method = MethodUtils.getAccessibleMethod(instance.getClass(), methodName, parameterType);

    if (method == null && TypeHelper.hasPrimitive(parameterType)) {
      method =
          MethodUtils.getAccessibleMethod(
              instance.getClass(), methodName, TypeHelper.getPrimitive(parameterType));
      // if the setter is for a primitive, but we have a null, we leave the default
      if (method != null && parsedValue == null) {
        return;
      }
    }

    invokeMethod(method, instance, parsedValue, methodName, fieldName);
  }
Ejemplo n.º 7
0
 public void afterPropertiesSet() throws Exception {
   if (usePlaceholderProperties) {
     OrderComparator.sort(loaders);
     for (PropertiesLoaderSupport loader : loaders) {
       PROPERTIES.putAll((Properties) MethodUtils.invokeMethod(loader, "mergeProperties", null));
     }
   }
   PROPERTIES.putAll(mergeProperties());
   // 检查某些关键的配置顶是否存在,不存在就报初始化错误
   String[] keys = this.necessaryConfigs;
   if (keys != null) {
     for (String key : keys) {
       key = StringUtils.trimToEmpty(key);
       if (!PROPERTIES.containsKey(key)) {
         throw new IllegalStateException(
             "Can not find property \"" + key + "\" in configuration file.");
       }
     }
   }
 }
Ejemplo n.º 8
0
  /**
   * Creates a Stem annotation with same begin and end as the AnnotationFS fs, the value is the
   * stemmed value derived by applying the featurepath.
   *
   * @param jcas the JCas
   * @param fs the AnnotationFS where the Stem annotation is created
   * @throws AnalysisEngineProcessException if the {@code stem} method from the snowball stemmer
   *     cannot be invoked.
   */
  private void createStemAnnotation(JCas jcas, AnnotationFS fs)
      throws AnalysisEngineProcessException {
    // Check for blank text, it makes no sense to add a stem then (and raised an exception)
    String value = fp.getValue(fs);
    if (!StringUtils.isBlank(value)) {
      if (lowerCase) {
        // Fixme - should use locale/language defined in CAS.
        value = value.toLowerCase(Locale.US);
      }

      Stem stemAnnot = new Stem(jcas, fs.getBegin(), fs.getEnd());
      SnowballProgram programm = getSnowballProgram(jcas);
      programm.setCurrent(value);

      try {
        // The patched snowball from Lucene has this as a method on SnowballProgram
        // but if we have some other snowball also in the classpath, Java might
        // choose to use the other. So to be safe, we use a reflection here.
        // -- REC, 2011-04-17
        MethodUtils.invokeMethod(programm, "stem", null);
      } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
      }

      stemAnnot.setValue(programm.getCurrent());
      stemAnnot.addToIndexes(jcas);

      // Try setting the "stem" feature on Tokens.
      Feature feat = fs.getType().getFeatureByBaseName("stem");
      if (feat != null
          && feat.getRange() != null
          && jcas.getTypeSystem().subsumes(feat.getRange(), stemAnnot.getType())) {
        fs.setFeatureValue(feat, stemAnnot);
      }
    }
  }
Ejemplo n.º 9
0
 private Map<Long, String> buildDisplayValuesMap(Collection values)
     throws InvocationTargetException, IllegalAccessException {
   Map<Long, String> displayValues = new HashMap<>();
   for (Object obj : values) {
     Method method = MethodUtils.getAccessibleMethod(obj.getClass(), "getId", (Class[]) null);
     Long key = (Long) method.invoke(obj);
     String uiRepresentation = UIRepresentationUtil.uiRepresentationString(obj);
     if (uiRepresentation != null) {
       displayValues.put(
           key,
           uiRepresentation.length() > UI_REPRESENTATION_MAX_LENGTH
               ? uiRepresentation.substring(0, UI_REPRESENTATION_MAX_LENGTH + 1) + ELLIPSIS
               : uiRepresentation);
     } else {
       String toStringResult = obj.toString();
       displayValues.put(
           key,
           toStringResult.length() > TO_STRING_MAX_LENGTH
               ? toStringResult.substring(0, TO_STRING_MAX_LENGTH + 1) + ELLIPSIS
               : toStringResult);
     }
   }
   return displayValues;
 }