Skip to content

Commit

Permalink
Merge pull request #8 from VineetReynolds/FORGE-1096
Browse files Browse the repository at this point in the history
FORGE-1096 Fixed spurious ArrayIndexOutOfBoundsException.
  • Loading branch information
gastaldi committed Aug 7, 2013
2 parents 187d9cd + 33ef7c6 commit 5fff96e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Expand Up @@ -9,8 +9,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
Expand All @@ -23,6 +25,7 @@
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.TextEdit;
import org.jboss.forge.parser.JavaParser;
Expand Down Expand Up @@ -622,7 +625,11 @@ public String toString()

try
{
TextEdit edit = unit.rewrite(document, null);
@SuppressWarnings("rawtypes")
Map options = JavaCore.getOptions();
options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_7);
options.put(CompilerOptions.OPTION_Encoding, "UTF-8");
TextEdit edit = unit.rewrite(document, options);
edit.apply(document);
}
catch (Exception e)
Expand Down
Expand Up @@ -13,6 +13,7 @@ public void testSupportsGenericsSource() throws Exception {
JavaSource<?> source = JavaParser.parse("public class Test{public void test() {java.util.List<String> s = new java.util.ArrayList<String>(); for (String item : s){}}}");
Assert.assertFalse(source.hasSyntaxErrors());
}

@Test
public void testSupportsGenericsSourceFromConstructor() throws Exception {
JavaSource<?> source = JavaParser.parse("public class Test{public Test() {java.util.List<String> s = new java.util.ArrayList<String>(); for (String item : s){}}}");
Expand All @@ -25,4 +26,24 @@ public void testSupportsGenericsSourceFromMethod() throws Exception {
source.addMethod("public void test() {java.util.List<String> s = new java.util.ArrayList<String>(); for (String item : s){}}");
Assert.assertFalse(source.hasSyntaxErrors());
}

@Test
public void testSupportsGenericsSourceFromAddedConstructor() throws Exception {
JavaClass source = JavaParser.parse(JavaClass.class, "public class Test{}");
// Add a new method to get JDT to recognize the new ASTs
source.addMethod().setConstructor(true).setBody("java.util.List<String> s = new java.util.ArrayList<String>(); for (String item : s){}");
// Forces a rewrite to happen via AbstractJavaSource
source.toString();
Assert.assertFalse(source.hasSyntaxErrors());
}

@Test
public void testSupportsGenericsSourceFromAddedMethod() throws Exception {
JavaClass source = JavaParser.parse(JavaClass.class, "public class Test{}");
// Add a new method to get JDT to recognize the new ASTs
source.addMethod().setName("test").setBody("java.util.List<String> s = new java.util.ArrayList<String>(); for (String item : s){}");
// Forces a rewrite to happen via AbstractJavaSource
source.toString();
Assert.assertFalse(source.hasSyntaxErrors());
}
}

0 comments on commit 5fff96e

Please sign in to comment.