public ClassBuilder < T > withMethod ( String methodName , Expression expression ) { if ( methodName . contains ( "(" ) ) { Method method = Method . getMethod ( methodName ) ; return withMethod ( method , expression ) ; } Method foundMethod = null ; List < List < java . lang . reflect . Method > > listOfMethods = new ArrayList <> ( ) ; listOfMethods . add ( asList ( Object . class . getMethods ( ) ) ) ; listOfMethods . add ( asList ( mainClass . getMethods ( ) ) ) ; listOfMethods . add ( asList ( mainClass . getDeclaredMethods ( ) ) ) ; for ( Class < ? > type : otherClasses ) { listOfMethods . add ( asList ( type . getMethods ( ) ) ) ; listOfMethods . add ( asList ( type . getDeclaredMethods ( ) ) ) ; } for ( List < java . lang . reflect . Method > list : listOfMethods ) { for ( java . lang . reflect . Method m : list ) { if ( m . getName ( ) . equals ( methodName ) ) { Method method = getMethod ( m ) ; if ( foundMethod != null && ! method . equals ( foundMethod ) ) throw new IllegalArgumentException ( "Method " + method + " collides with " + foundMethod ) ; foundMethod = method ; } } } Preconditions . check ( foundMethod != null , "Could not find method '" + methodName + "'" ) ; return withMethod ( foundMethod , expression ) ; }
CCreates a new method for a dynamic class