示例#1
0
 public static void setEnv(Map<String, String> newenv) {
   try {
     Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
     Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
     theEnvironmentField.setAccessible(true);
     Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
     env.putAll(newenv);
     Field theCaseInsensitiveEnvironmentField =
         processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
     theCaseInsensitiveEnvironmentField.setAccessible(true);
     Map<String, String> cienv =
         (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
     cienv.putAll(newenv);
   } catch (NoSuchFieldException e) {
     try {
       Class[] classes = Collections.class.getDeclaredClasses();
       Map<String, String> env = System.getenv();
       for (Class cl : classes) {
         if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
           Field field = cl.getDeclaredField("m");
           field.setAccessible(true);
           Object obj = field.get(env);
           Map<String, String> map = (Map<String, String>) obj;
           map.clear();
           map.putAll(newenv);
         }
       }
     } catch (Exception e2) {
       e2.printStackTrace();
     }
   } catch (Exception e1) {
     e1.printStackTrace();
   }
 }
示例#2
0
  void removeMockedClasses(@Nonnull List<Class<?>> previousMockedClasses) {
    int currentMockedClassCount = mockedClasses.size();

    if (currentMockedClassCount > 0) {
      int previousMockedClassCount = previousMockedClasses.size();

      if (previousMockedClassCount == 0) {
        mockedClasses.clear();
        mockedTypesAndInstances.clear();
      } else if (previousMockedClassCount < currentMockedClassCount) {
        mockedClasses.retainAll(previousMockedClasses);
        mockedTypesAndInstances.keySet().retainAll(previousMockedClasses);
      }
    }
  }
示例#3
0
文件: Query.java 项目: aaberg/sql2o
  private PreparedStatement buildPreparedStatement(boolean allowArrayParameters) {
    // array parameter handling
    parsedQuery =
        ArrayParameters.updateQueryAndParametersIndexes(
            parsedQuery, paramNameToIdxMap, parameters, allowArrayParameters);

    // prepare statement creation
    if (preparedStatement == null) {
      try {
        if (columnNames != null && columnNames.length > 0) {
          preparedStatement =
              connection.getJdbcConnection().prepareStatement(parsedQuery, columnNames);
        } else if (returnGeneratedKeys) {
          preparedStatement =
              connection
                  .getJdbcConnection()
                  .prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS);
        } else {
          preparedStatement = connection.getJdbcConnection().prepareStatement(parsedQuery);
        }
      } catch (SQLException ex) {
        throw new Sql2oException(
            String.format("Error preparing statement - %s", ex.getMessage()), ex);
      }
      connection.registerStatement(preparedStatement);
    }

    // parameters assignation to query
    for (Map.Entry<String, ParameterSetter> parameter : parameters.entrySet()) {
      for (int paramIdx : paramNameToIdxMap.get(parameter.getKey())) {
        try {
          parameter.getValue().setParameter(paramIdx, preparedStatement);
        } catch (SQLException e) {
          throw new RuntimeException(
              String.format("Error adding parameter '%s' - %s", parameter.getKey(), e.getMessage()),
              e);
        }
      }
    }
    // the parameters need to be cleared, so in case of batch, only new parameters will be added
    parameters.clear();

    return preparedStatement;
  }