简体   繁体   中英

java - how to check a method on every method

public class Test
{
    private static boolean enable = true;

    public static boolean isEnable()
    {
        return enable;
    }

    public static String getName(int id)
    {
        if(isEnable())
            return "test";
        else
            return null;
    }

    public static ArrayList<String> getVars(int id, String str)
    {
        if(isEnable())
        {
            ArrayList<String> list = new ArrayList<String>();
            list.add("test1");
            list.add("test2");
            list.add("test3");
            return list;
        }

        return new ArrayList<String>();
    }

    //and more method contains enable check
}

how to check enable in single method and skip from all method

Unless you're using some funky aspect oriented library such as AspectJ there's no way to automatically inject code before/after each method in a class.

You need to do it "manually" as you have shown in your example code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM