is there any way to copy often-used dynamic routines into a java method?

atomAnt :

New to java, and I'm currently relying on lots of output messages to trace activity. I'd like a way to do this cleanly, to make 'semi-redundant' code as inconspicuous as possible.

Originally, I was doing this: (Note the System.out.println statements.)

package hamburger;
...
public class Hamburger extends Application {
    ...
    public static void main(String args[]){                  
        System.out.println("--->  hamburger.Hamburger.main");  
        ...
        String [] drink = {"pepsi"};
        NoCoke.onlypepsi(drink);
        ...  ...
public class NoCoke
    public static void main(String args[]){                  
        System.out.println("--->  hamburger.NoCoke.main");  
        ...
    static void justpepsi(String... commandIn){
        System.out.println("--->  hamburger.NoCoke.justpepsi");  
        ... 
        try{ to get coke anyway... 

Of course, when I moved things around or renamed stuff, all these literals had to be updated, so I started doing things dynamically using these three lines:

public class Hamburger
public static void main(String[] args) { 
    String nameClass  = new Throwable().getStackTrace()[0].getClassName(); 
    String nameMethod = new Throwable().getStackTrace()[0].getMethodName(); 
    System.out.println("--->  "+nameClass+"."+nameMethod);  

public class NoCoke
public static void main(String args[]){                  
    <AND HERE>
    public static void justpepsi(String... commandIn){
    <AND HERE>      

It works fine:

--->  hamburger.Hamburger.main
--->  hamburger.NoCoke.main
--->  hamburger.NoCoke.justpepsi

But I hate the way it looks, takes up space, etc.

Is there anything like a 'copy-book' in java? - a doc that contains executable code that can be dropped anywhere? Or is there a procedure to define the code as something like 'a String' that could be declared in the constructor, something like this:

StringThing nameMessageRoutine = new (whatever...) StringThing
   "String nameClass  = new Throwable()...<etc>...
    System.out.println('--->  '+nameClass+'.'+nameMethod);'"  

...that I could just 'import' or 'refer to' like this:

public class Hamburger extends Application {
public static void main(String args[]){ 
    import: nameMessageRoutine;         //outside of the package member

public class NoCoke
public static void main(String args[]){                  
    reference: nameMessageRoutine;      //defined in the constructor
Daniel Williams :

This method prints information about from where it was called:

static void printCurrent() {
    final StackTraceElement ste = new Throwable().getStackTrace()[1];
    String methodName = ste.getMethodName();
    String className = ste.getClassName();
    String lineNum = ""+ste.getLineNumber();
    System.out.println(className + ", " + methodName + ", Line " + lineNum);
}

For example:

package randomTest;

public class MainClass {
    public static void main(String[] args) {
        printCurrent();
    }
}

The output is

randomTest.MainClass, main, Line 131

EDIT: I know this doesn't exactly answer the question, but it does accomplish the final goal of tracing code activity. To answer the question (as per the title), there is no way in pure Java to automatically insert routines into marked places.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157145&siteId=1