简体   繁体   English

我可以使用正则表达式在Java类中查找方法吗?

[英]Can I use regular expressions to find a method on a class in java?

I know how to find a method in java using a fixed string, 我知道如何在Java中使用固定字符串查找方法,

someClass.getMethod("foobar", argTypes);

but is there a way to use a regular expression rather than a fixed string to find a method on a given class? 但是有没有办法使用正则表达式而不是固定字符串在给定类上查找方法?

An example of the usage might be if I wanted to find a method that was called either "foobar" or "fooBar". 例如,如果我想找到一个名为“ foobar”或“ fooBar”的方法,可能是这种用法的一个例子。 Using a regular expression like "foo[Bb]ar" would match either of these method names. 使用类似“ foo [Bb] ar”的正则表达式将匹配这些方法名称中的任何一个。

You should apply your regexp on getDeclaredMethods () reflection method (or GetMethods() if you want only the public ones). 您应该将regexp应用于getDeclaredMethods ()反射方法(如果只希望使用公共方法,则应使用GetMethods())。

[Warning: both methods will throw a SecurityException if there is a security manager.] [警告:如果有安全管理器,则这两个方法都将引发SecurityException。

You apply it on each name of each method returned by getDeclaredMethod() and only memorize in a Collection the compliant Methods. 您可以将其应用于getDeclaredMethod()返回的每个方法的每个名称,并且仅将兼容的方法存储在Collection中。

Something like! 就像是!

try
{
  final Pattern aMethodNamePattern = Pattern.compile("foo[Bb]ar");
  final List<Method> someMethods = aClass.getDeclaredMethods();
  final List<Method> someCompliantMethods = new ArrayList<Method>();
  for(final Method aMethod: someMethods)
  {
    final String aMethodName = aMethod.getName();
    final Matcher aMethodNameMatcher = aMethodNamePattern.getMatcher(aMethodName);
    if(aMethodNameMatcher.matches() == true)
    {
       someCompliantMethods.add(aMethod);
    }
}
catch(...) // catch all exceptions like SecurityException, IllegalAccessException, ...

Not directly. 不直接。 You could loop over all the methods and check each. 您可以遍历所有方法并进行检查。

Pattern p = Pattern.compile("foo[Bb]ar");
for(Method m : someClass.getMethods()) {
  if(p.matcher(m.getName()).matches()) {
    return m; 
  }
}

You could do it by iterating over ALL the methods on a class and matching them that way. 您可以通过遍历类上的所有方法并以这种方式进行匹配来实现。

Not that simple, but it would do the trick 不是那么简单,但是可以解决问题

    ArrayList<Method> matches = new ArrayList<Method>();
    for(Method meth : String.class.getMethods()) {
        if (meth.getName().matches("lengt.")){
            matches.add(meth);
        }
    }

No, you can't do that, but you can get a list of the method a class has and apply the regexp to them. 不,您不能这样做,但是您可以获取类具有的方法的列表,然后将正则表达式应用于它们。

Method[] getMethods( String regexp, Class clazz, Object ... argTypes ){
    List<Method> toReturn = new ArrayList<Method>();
    for( Method m : clazz.getDeclaredMethods() ){ 
         if( m.getName().matches( regExp ) ) { // validate argTypes aswell here...
             toReturn.add( m );
         }
    }
    return toReturn.toArray(); 
}

Well something like that.... 像这样...

When I want look for some method using simple name pattern I use this org.reflections 当我想使用简单的名称模式寻找某种方法时,我使用此org.reflections

For example looking for some methods which are public and name start with get: 例如,寻找一些公共方法和以get开头的名称:

Set<Method> getters = ReflectionUtils.getAllMethods(SomeClass.class, ReflectionUtils.withModifier(Modifier.PUBLIC), ReflectionUtils.withPrefix("get"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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