From: <jbo...@li...> - 2006-05-23 02:35:40
|
Author: mic...@jb... Date: 2006-05-22 22:35:27 -0400 (Mon, 22 May 2006) New Revision: 4358 Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionFixer.java labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/FunctionFixerTest.java Log: JBRULES-273 Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionFixer.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionFixer.java 2006-05-23 02:07:18 UTC (rev 4357) +++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionFixer.java 2006-05-23 02:35:27 UTC (rev 4358) @@ -41,35 +41,42 @@ public String fix(String raw, Pattern pattern) { if (raw == null) return null; StringBuffer buf = new StringBuffer(); - int lastIndex = 0; + int startIndex = 0, lastIndex = 0; - Matcher matcher = pattern.matcher(raw); - - while(matcher.find()) { - String pre = matcher.group(1); - if (matcher.group(1) != null) { - String trimmedPre = pre.trim(); - if (trimmedPre.endsWith( "." ) || trimmedPre.endsWith( "new" )) { - //leave alone - continue; - } + Matcher matcher = pattern.matcher(raw); + while (matcher.find(startIndex)) { + startIndex = getStartIndex(matcher); + + String pre = matcher.group(1); + if (matcher.group(1) != null) { + String trimmedPre = pre.trim(); + if (trimmedPre.endsWith( "." ) || trimmedPre.endsWith( "new" )) { + //leave alone + continue; + } + } + String function = matcher.group(2).trim(); + //if we have a reserve d work, DO NOT TOUCH ! + if (KEYWORDS.contains( function )) continue; + + String params = matcher.group(3).trim(); + + String target = ucFirst(function) + "." + function + "(" + params + ")"; + + buf.append( raw.substring( lastIndex, matcher.start( 2 ) ) ); + buf.append( target ); + + lastIndex = matcher.end(); } - String function = matcher.group(2).trim(); - //if we have a reserved work, DO NOT TOUCH ! - if (KEYWORDS.contains( function )) continue; - - String params = matcher.group(3).trim(); - - String target = ucFirst(function) + "." + function + "(" + params + ")"; - - buf.append( raw.substring( lastIndex, matcher.start( 2 ) ) ); - buf.append( target ); - lastIndex = matcher.end(); - } + buf.append( raw.substring( lastIndex ) ); return buf.toString(); } + private int getStartIndex(Matcher matcher) { + return matcher.start(3) <= 0 ? matcher.end() + 1 : matcher.start(3); + } + private String ucFirst(String name) { return name.toUpperCase().charAt( 0 ) + name.substring( 1 ); } Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/FunctionFixerTest.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/FunctionFixerTest.java 2006-05-23 02:07:18 UTC (rev 4357) +++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/FunctionFixerTest.java 2006-05-23 02:35:27 UTC (rev 4358) @@ -71,12 +71,31 @@ fixer.fix( "\nfor(int i=0; i < 2; i++) { /*do noithing*/ }" ) ); } - public void testNestedInAMethod() { + public void testMultipleInABracket() { FunctionFixer fixer = new FunctionFixer(); - assertEquals( "obj.method(Foo.foo(bar));", - fixer.fix( "obj.method(foo(bar));" ) ); + assertEquals( "if (Foo.foo(bar)) { Bar.bar(baz); }", + fixer.fix( "if (foo(bar)) { bar(baz); }" ) ); } + + public void testInBrackets() { + FunctionFixer fixer = new FunctionFixer(); + assertEquals( "if (Foo.foo(bar))", + fixer.fix( "if (foo(bar))" ) ); + + + } + + public void testAlreadyAdded() { + FunctionFixer fixer = new FunctionFixer(); + assertEquals( "Foo.foo(bar)", + fixer.fix( "Foo.foo(bar)" ) ); + + + + } + + } \ No newline at end of file |