1

Say I have the following class

class Application {
    public Application() {...}

    public void doSomething(final String logs) {
        final String[] lines = logs.split("\\n");
        for (final String line: lines) {
           // Pass the line to every single checkForProp# item and do something with the response
        }
    } 

    private Optional<Action> checkForProp1(final String line) {
       // Check if line has certain thing
       // If so return an Action
    }

    // More of these "checks" here
}

Let's say every single response, would be added to a queue, and then returned something is done on that queue.

So instead of I calling each method manually, I want to have maybe an array of checker methods, and automatically loop through them, pass in the line, and add the response to a queue.

Can this be achieved?

5
  • you can use interface for this Commented Oct 25, 2017 at 5:23
  • You could probably create an interfacr and create a class, you'd be stuck having to create instances for every single one, it's probably easier adding the checks manually Commented Oct 25, 2017 at 5:23
  • stackoverflow.com/questions/40383246/… ? Commented Oct 25, 2017 at 5:23
  • If order of checks doesn't matter, then you probably could use some annotation to "mark" checks and some reflection to call them (find methods annotated with @XXX then call them) Commented Oct 25, 2017 at 5:25
  • @RC I like the annotation method! Commented Oct 25, 2017 at 5:30

2 Answers 2

2

You can implement your Action as interface:

public interface Action{
   public void fireAction();
}

Those classes which implement Action will then override the method defined in the interface, such as checking a String.

Add the instances of Action to the list and you loop them accordingly.

Example:

for(Action a : actions)
    a.fireAction();
Sign up to request clarification or add additional context in comments.

Comments

0

If i understood your question correctly then following code may be help you.

import java.lang.reflect.Method;

public class AClass {

private void aMethod(){
    System.out.println(" in a");
}

private void bMethod(){
    System.out.println(" in b");
}

private void cMethod(){
    System.out.println(" in c");
}

private void dMethod(){
    System.out.println(" in d");
}

//50 more methods. 

//method call the rest
public void callAll() {
     Method[] methods = this.getClass().getDeclaredMethods();
    try{
    for (Method m : methods) {

        if (m.getName().endsWith("Method")) {
            //do stuff..
            m.invoke(this,null);
        }
    }
    }catch(Exception e){

    }
}
 public static void main(String[] args) {
AClass a=new AClass();
a.callAll();
 }
    }

Here Java Reflection is used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.