Java Code Examples for com.klarna.reflection.ReflectionUtils

The following examples show how to use com.klarna.reflection.ReflectionUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedSetupScripts(Class testClass, HiveShellBuilder hiveShellBuilder) {
	Set<Field> setupScriptFields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveSetupScript.class));
	for (Field setupScriptField : setupScriptFields) {
		if (ReflectionUtils.isOfType(setupScriptField, String.class)) {
			String script = ReflectionUtils.getStaticFieldValue(testClass, setupScriptField.getName(), String.class);
			hiveShellBuilder.addSetupScript(script);
		} else if (ReflectionUtils.isOfType(setupScriptField, File.class) ||
				ReflectionUtils.isOfType(setupScriptField, Path.class)) {
			Path path = getMandatoryPathFromField(testClass, setupScriptField);
			hiveShellBuilder.addSetupScript(readAll(path));
		} else {
			throw new IllegalArgumentException(
					"Field annotated with @HiveSetupScript currently only supports type String, File and Path");
		}
	}
}
 
Example 2
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedResources(Class testClass, HiveShellBuilder workFlowBuilder) throws IOException {
	Set<Field> fields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveResource.class));

	for (Field resourceField : fields) {

		HiveResource annotation = resourceField.getAnnotation(HiveResource.class);
		String targetFile = annotation.targetFile();

		if (ReflectionUtils.isOfType(resourceField, String.class)) {
			String data = ReflectionUtils.getStaticFieldValue(testClass, resourceField.getName(), String.class);
			workFlowBuilder.addResource(targetFile, data);
		} else if (ReflectionUtils.isOfType(resourceField, File.class) ||
				ReflectionUtils.isOfType(resourceField, Path.class)) {
			Path dataFile = getMandatoryPathFromField(testClass, resourceField);
			workFlowBuilder.addResource(targetFile, dataFile);
		} else {
			throw new IllegalArgumentException(
					"Fields annotated with @HiveResource currently only supports field type String, File or Path");
		}
	}
}
 
Example 3
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedSetupScripts(Class testClass, HiveShellBuilder hiveShellBuilder) {
	Set<Field> setupScriptFields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveSetupScript.class));
	for (Field setupScriptField : setupScriptFields) {
		if (ReflectionUtils.isOfType(setupScriptField, String.class)) {
			String script = ReflectionUtils.getStaticFieldValue(testClass, setupScriptField.getName(), String.class);
			hiveShellBuilder.addSetupScript(script);
		} else if (ReflectionUtils.isOfType(setupScriptField, File.class) ||
				ReflectionUtils.isOfType(setupScriptField, Path.class)) {
			Path path = getMandatoryPathFromField(testClass, setupScriptField);
			hiveShellBuilder.addSetupScript(readAll(path));
		} else {
			throw new IllegalArgumentException(
					"Field annotated with @HiveSetupScript currently only supports type String, File and Path");
		}
	}
}
 
Example 4
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedResources(Class testClass, HiveShellBuilder workFlowBuilder) throws IOException {
	Set<Field> fields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveResource.class));

	for (Field resourceField : fields) {

		HiveResource annotation = resourceField.getAnnotation(HiveResource.class);
		String targetFile = annotation.targetFile();

		if (ReflectionUtils.isOfType(resourceField, String.class)) {
			String data = ReflectionUtils.getStaticFieldValue(testClass, resourceField.getName(), String.class);
			workFlowBuilder.addResource(targetFile, data);
		} else if (ReflectionUtils.isOfType(resourceField, File.class) ||
				ReflectionUtils.isOfType(resourceField, Path.class)) {
			Path dataFile = getMandatoryPathFromField(testClass, resourceField);
			workFlowBuilder.addResource(targetFile, dataFile);
		} else {
			throw new IllegalArgumentException(
					"Fields annotated with @HiveResource currently only supports field type String, File or Path");
		}
	}
}
 
Example 5
Source Project: HiveRunner   Source File: HiveRunnerCore.java    License: Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedSetupScripts(Object testCase, HiveShellBuilder workFlowBuilder) {
  Set<Field> setupScriptFields = ReflectionUtils.getAllFields(testCase.getClass(),
      withAnnotation(HiveSetupScript.class));

  for (Field setupScriptField : setupScriptFields) {
    if (ReflectionUtils.isOfType(setupScriptField, String.class)) {
      String script = ReflectionUtils.getFieldValue(testCase, setupScriptField.getName(), String.class);
      workFlowBuilder.addSetupScript(script);
    } else if (ReflectionUtils.isOfType(setupScriptField, File.class) ||
        ReflectionUtils.isOfType(setupScriptField, Path.class)) {
      Path path = getMandatoryPathFromField(testCase, setupScriptField);
      workFlowBuilder.addSetupScript(readAll(path));
    } else {
      throw new IllegalArgumentException(
          "Field annotated with @HiveSetupScript currently only supports type String, File and Path");
    }
  }
}
 
Example 6
Source Project: HiveRunner   Source File: HiveRunnerCore.java    License: Apache License 2.0 6 votes vote down vote up
private void loadAnnotatedResources(Object testCase, HiveShellBuilder workFlowBuilder) throws IOException {
  Set<Field> fields = ReflectionUtils.getAllFields(testCase.getClass(), withAnnotation(HiveResource.class));

  for (Field resourceField : fields) {

    HiveResource annotation = resourceField.getAnnotation(HiveResource.class);
    String targetFile = annotation.targetFile();

    if (ReflectionUtils.isOfType(resourceField, String.class)) {
      String data = ReflectionUtils.getFieldValue(testCase, resourceField.getName(), String.class);
      workFlowBuilder.addResource(targetFile, data);
    } else if (ReflectionUtils.isOfType(resourceField, File.class) ||
        ReflectionUtils.isOfType(resourceField, Path.class)) {
      Path dataFile = getMandatoryPathFromField(testCase, resourceField);
      workFlowBuilder.addResource(targetFile, dataFile);
    } else {
      throw new IllegalArgumentException(
          "Fields annotated with @HiveResource currently only supports field type String, File or Path");
    }
  }
}
 
Example 7
Source Project: HiveRunner   Source File: StandaloneHiveRunner.java    License: Apache License 2.0 6 votes vote down vote up
private TestRule getHiveRunnerConfigRule(Object target) {
    return new TestRule() {
        @Override
        public Statement apply(Statement base, Description description) {
            Set<Field> fields = ReflectionUtils.getAllFields(target.getClass(),
                    Predicates.and(
                            withAnnotation(HiveRunnerSetup.class),
                            withType(HiveRunnerConfig.class)));

            Preconditions.checkState(fields.size() <= 1,
                    "Exact one field of type HiveRunnerConfig should to be annotated with @HiveRunnerSetup");

            /*
             Override the config with test case config. Taking care to not replace the config instance since it
              has been passes around and referenced by some of the other test rules.
              */
            if (!fields.isEmpty()) {
                config.override(ReflectionUtils
                        .getFieldValue(target, fields.iterator().next().getName(), HiveRunnerConfig.class));
            }

            return base;
        }
    };
}
 
Example 8
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 5 votes vote down vote up
private HiveShellField loadScriptsUnderTest(final Class testClass, HiveShellBuilder hiveShellBuilder) {
	try {
		Set<Field> fields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveSQL.class));

		Preconditions.checkState(fields.size() == 1, "Exactly one field should to be annotated with @HiveSQL");

		final Field field = fields.iterator().next();
		List<Path> scripts = new ArrayList<>();
		HiveSQL annotation = field.getAnnotation(HiveSQL.class);
		for (String scriptFilePath : annotation.files()) {
			Path file = Paths.get(Resources.getResource(scriptFilePath).toURI());
			Preconditions.checkState(Files.exists(file), "File " + file + " does not exist");
			scripts.add(file);
		}

		Charset charset = annotation.encoding().equals("") ?
				Charset.defaultCharset() : Charset.forName(annotation.encoding());

		final boolean isAutoStart = annotation.autoStart();

		hiveShellBuilder.setScriptsUnderTest(scripts, charset);

		return new HiveShellField() {
			@Override
			public void setShell(HiveShell shell) {
				ReflectionUtils.setStaticField(testClass, field.getName(), shell);
			}

			@Override
			public boolean isAutoStart() {
				return isAutoStart;
			}
		};
	} catch (Throwable t) {
		throw new IllegalArgumentException("Failed to init field annotated with @HiveSQL: " + t.getMessage(), t);
	}
}
 
Example 9
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 5 votes vote down vote up
private Path getMandatoryPathFromField(Class testClass, Field resourceField) {
	Path path;
	if (ReflectionUtils.isOfType(resourceField, File.class)) {
		File dataFile = ReflectionUtils.getStaticFieldValue(testClass, resourceField.getName(), File.class);
		path = Paths.get(dataFile.toURI());
	} else if (ReflectionUtils.isOfType(resourceField, Path.class)) {
		path = ReflectionUtils.getStaticFieldValue(testClass, resourceField.getName(), Path.class);
	} else {
		throw new IllegalArgumentException(
				"Only Path or File type is allowed on annotated field " + resourceField);
	}

	Preconditions.checkArgument(Files.exists(path), "File %s does not exist", path);
	return path;
}
 
Example 10
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 5 votes vote down vote up
private void loadAnnotatedProperties(Class testClass, HiveShellBuilder workFlowBuilder) {
	for (Field hivePropertyField : ReflectionUtils.getAllFields(testClass, withAnnotation(HiveProperties.class))) {
		Preconditions.checkState(ReflectionUtils.isOfType(hivePropertyField, Map.class),
				"Field annotated with @HiveProperties should be of type Map<String, String>");
		workFlowBuilder.putAllProperties(
				ReflectionUtils.getStaticFieldValue(testClass, hivePropertyField.getName(), Map.class));
	}
}
 
Example 11
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 5 votes vote down vote up
private HiveShellField loadScriptsUnderTest(final Class testClass, HiveShellBuilder hiveShellBuilder) {
	try {
		Set<Field> fields = ReflectionUtils.getAllFields(testClass, withAnnotation(HiveSQL.class));

		Preconditions.checkState(fields.size() == 1, "Exactly one field should to be annotated with @HiveSQL");

		final Field field = fields.iterator().next();
		List<Path> scripts = new ArrayList<>();
		HiveSQL annotation = field.getAnnotation(HiveSQL.class);
		for (String scriptFilePath : annotation.files()) {
			Path file = Paths.get(Resources.getResource(scriptFilePath).toURI());
			Preconditions.checkState(Files.exists(file), "File " + file + " does not exist");
			scripts.add(file);
		}

		Charset charset = annotation.encoding().equals("") ?
				Charset.defaultCharset() : Charset.forName(annotation.encoding());

		final boolean isAutoStart = annotation.autoStart();

		hiveShellBuilder.setScriptsUnderTest(scripts, charset);

		return new HiveShellField() {
			@Override
			public void setShell(HiveShell shell) {
				ReflectionUtils.setStaticField(testClass, field.getName(), shell);
			}

			@Override
			public boolean isAutoStart() {
				return isAutoStart;
			}
		};
	} catch (Throwable t) {
		throw new IllegalArgumentException("Failed to init field annotated with @HiveSQL: " + t.getMessage(), t);
	}
}
 
Example 12
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 5 votes vote down vote up
private Path getMandatoryPathFromField(Class testClass, Field resourceField) {
	Path path;
	if (ReflectionUtils.isOfType(resourceField, File.class)) {
		File dataFile = ReflectionUtils.getStaticFieldValue(testClass, resourceField.getName(), File.class);
		path = Paths.get(dataFile.toURI());
	} else if (ReflectionUtils.isOfType(resourceField, Path.class)) {
		path = ReflectionUtils.getStaticFieldValue(testClass, resourceField.getName(), Path.class);
	} else {
		throw new IllegalArgumentException(
				"Only Path or File type is allowed on annotated field " + resourceField);
	}

	Preconditions.checkArgument(Files.exists(path), "File %s does not exist", path);
	return path;
}
 
Example 13
Source Project: flink   Source File: FlinkStandaloneHiveRunner.java    License: Apache License 2.0 5 votes vote down vote up
private void loadAnnotatedProperties(Class testClass, HiveShellBuilder workFlowBuilder) {
	for (Field hivePropertyField : ReflectionUtils.getAllFields(testClass, withAnnotation(HiveProperties.class))) {
		Preconditions.checkState(ReflectionUtils.isOfType(hivePropertyField, Map.class),
				"Field annotated with @HiveProperties should be of type Map<String, String>");
		workFlowBuilder.putAllProperties(
				ReflectionUtils.getStaticFieldValue(testClass, hivePropertyField.getName(), Map.class));
	}
}
 
Example 14
Source Project: HiveRunner   Source File: HiveRunnerCore.java    License: Apache License 2.0 5 votes vote down vote up
private HiveShellField loadScriptUnderTest(Object testCaseInstance, HiveShellBuilder hiveShellBuilder) {
  try {
    Set<Field> fields = ReflectionUtils.getAllFields(testCaseInstance.getClass(), withAnnotation(HiveSQL.class));

    Preconditions.checkState(fields.size() == 1, "Exact one field should to be annotated with @HiveSQL");

    Field field = fields.iterator().next();
    List<Path> scriptPaths = new ArrayList<>();
    HiveSQL annotation = field.getAnnotation(HiveSQL.class);
    for (String scriptFilePath : annotation.files()) {
      Path file = Paths.get(Resources.getResource(scriptFilePath).toURI());
      assertFileExists(file);
      scriptPaths.add(file);
    }

    Charset charset = annotation.encoding().equals("") ?
        Charset.defaultCharset() : Charset.forName(annotation.encoding());

    boolean isAutoStart = annotation.autoStart();

    hiveShellBuilder.setScriptsUnderTest(scriptPaths, charset);

    return new HiveShellField() {
      @Override
      public void setShell(HiveShell shell) {
        ReflectionUtils.setField(testCaseInstance, field.getName(), shell);
      }

      @Override
      public boolean isAutoStart() {
        return isAutoStart;
      }
    };
  } catch (Throwable t) {
    throw new IllegalArgumentException("Failed to init field annotated with @HiveSQL: " + t.getMessage(), t);
  }
}
 
Example 15
Source Project: HiveRunner   Source File: HiveRunnerCore.java    License: Apache License 2.0 5 votes vote down vote up
private Path getMandatoryPathFromField(Object testCase, Field resourceField) {
  Path path;
  if (ReflectionUtils.isOfType(resourceField, File.class)) {
    File dataFile = ReflectionUtils.getFieldValue(testCase, resourceField.getName(), File.class);
    path = Paths.get(dataFile.toURI());
  } else if (ReflectionUtils.isOfType(resourceField, Path.class)) {
    path = ReflectionUtils.getFieldValue(testCase, resourceField.getName(), Path.class);
  } else {
    throw new IllegalArgumentException(
        "Only Path or File type is allowed on annotated field " + resourceField);
  }

  Preconditions.checkArgument(Files.exists(path), "File %s does not exist", path);
  return path;
}
 
Example 16
Source Project: HiveRunner   Source File: HiveRunnerCore.java    License: Apache License 2.0 5 votes vote down vote up
private void loadAnnotatedProperties(Object testCase, HiveShellBuilder workFlowBuilder) {
  for (Field hivePropertyField : ReflectionUtils.getAllFields(testCase.getClass(),
      withAnnotation(HiveProperties.class))) {
    Preconditions.checkState(ReflectionUtils.isOfType(hivePropertyField, Map.class),
        "Field annotated with @HiveProperties should be of type Map<String, String>");
    workFlowBuilder.putAllProperties(
        ReflectionUtils.getFieldValue(testCase, hivePropertyField.getName(), Map.class));
  }
}
 
Example 17
Source Project: HiveRunner   Source File: HiveRunnerExtension.java    License: Apache License 2.0 5 votes vote down vote up
private void setupConfig(Object target) {
  Set<Field> fields = ReflectionUtils.getAllFields(target.getClass(),
      Predicates.and(
          withAnnotation(HiveRunnerSetup.class),
          withType(HiveRunnerConfig.class)));

  Preconditions.checkState(fields.size() <= 1,
      "Only one field of type HiveRunnerConfig should be annotated with @HiveRunnerSetup");

  if (!fields.isEmpty()) {
    config.override(ReflectionUtils
        .getFieldValue(target, fields.iterator().next().getName(), HiveRunnerConfig.class));
  }
}