public static List<Method> getMethods(final Object object) {
   final List<Method> methods =
       ReflectionUtils.getAllMethods(object.getClass())
           .stream() //
           .filter(
               method -> !IGNORE_BY_CLASSNAME.contains(method.getDeclaringClass().getName())) //
           .filter(method -> !method.getName().contains("$jacoco")) //
           .sorted((method1, method2) -> method1.getName().compareTo(method2.getName())) //
           .collect(Collectors.toList());
   final Set<String> uniqueNames = Sets.newHashSet();
   // TODO handle overloaded methods with same name correct (now they will filtered)
   return methods
       .stream()
       .filter(
           method -> {
             if (uniqueNames.contains(method.getName())) {
               return false;
             }
             uniqueNames.add(method.getName());
             return true;
           })
       .filter(
           method ->
               !method.getDeclaringClass().equals(ConsoleScript.class)
                   || !method.getName().equals("run"))
       .collect(Collectors.toList());
 }
  public static Method resolveMethod(final Class<? extends IMethod> aMethod) {
    String methodOgnl = aMethod.getSimpleName();

    try {
      String methodName;
      Class<?>[] paramTypes;
      if (methodOgnl.contains(tokenSeparator)) {
        methodName = methodOgnl.substring(0, methodOgnl.indexOf(tokenSeparator));
        String[] params =
            methodOgnl.substring(methodOgnl.indexOf(tokenSeparator) + 1).split(tokenSeparator);
        paramTypes = new Class<?>[params.length];
        for (int i = 0; i < params.length; i++) {
          String typeName = params[i].replace(arrayDescriptor, "[]").replace(pathSeparator, '.');
          paramTypes[i] = ReflectionUtils.forName(typeName);
        }
      } else {
        methodName = methodOgnl;
        paramTypes = null;
      }

      return resolveClassOf(aMethod).getDeclaredMethod(methodName, paramTypes);
    } catch (Exception e) {
      throw new ReflectionsException("could not resolve to method " + aMethod.getName(), e);
    }
  }
  @Test
  public void runCodegen() throws IOException {
    final Rule rule = parser.parseRule(ruleForTest(), true).withId("1");

    final String sourceCode = CodeGenerator.sourceCodeForRule(rule);
    Files.write(sourceCode, OUTFILE.toFile(), StandardCharsets.UTF_8);

    log.info("Code:\n{}", sourceCode);

    try {

      ClassLoader ruleClassloader = new ClassLoader() {};
      //noinspection unchecked
      Class<GeneratedRule> rule$1 =
          (Class<GeneratedRule>)
              JCC.loadFromJava(
                  ruleClassloader,
                  "org.graylog.plugins.pipelineprocessor.$dynamic.rules.rule$1",
                  sourceCode);

      //noinspection unchecked
      final Set<Constructor> constructors =
          ReflectionUtils.getConstructors(rule$1, input -> input.getParameterCount() == 1);
      final Constructor onlyElement = Iterables.getOnlyElement(constructors);
      final GeneratedRule generatedRule = (GeneratedRule) onlyElement.newInstance(functionRegistry);

      final Message message = new Message("hello", "jenkins.torch.sh", Tools.nowUTC());
      message.addField("message", "#1234");
      message.addField("something_that_doesnt_exist", "foo");
      final EvaluationContext context = new EvaluationContext(message);

      final boolean when = generatedRule.when(context);
      if (when) {
        generatedRule.then(context);
      }
      log.info("created dynamic rule {} matches: {}", generatedRule.name(), when);

      assertThat(context.currentMessage().hasField("some_identifier")).isTrue();

    } catch (InvocationTargetException
        | ClassNotFoundException
        | InstantiationException
        | IllegalAccessException e) {
      log.error("Cannot load dynamically created class!", e);
    }
  }
 public static List<Field> getFields(final Object object) {
   final List<Field> fields =
       ReflectionUtils.getAllFields(object.getClass())
           .stream() //
           .filter(field -> !IGNORE_BY_CLASSNAME.contains(field.getDeclaringClass().getName())) //
           .filter(field -> !field.getName().equals("this$0")) //
           .filter(field -> !field.getName().contains("$jacoco")) //
           .sorted((field1, field2) -> field1.getName().compareTo(field2.getName())) //
           .collect(Collectors.toList());
   final Set<String> uniqueNames = Sets.newHashSet();
   return fields
       .stream()
       .filter(
           field -> {
             if (uniqueNames.contains(field.getName())) {
               return false;
             }
             uniqueNames.add(field.getName());
             return true;
           })
       .collect(Collectors.toList());
 }
示例#5
0
 static java.util.Set<Method> relevantMethods(Class<?> c, Class<? extends Annotation> a) {
   return ReflectionUtils.getAllMethods(c, Predicates.and(ReflectionUtils.withAnnotation(a)));
 }