Skip to content

Commit

Permalink
FORGE-1096 Fixed spurious ArrayIndexOutOfBoundsException.
Browse files Browse the repository at this point in the history
The exception was thrown on rewriting the CompilationUnit, and
was most likely caused due to use of a lower source level.

Setting the source level to 1.7 gets rid of the exception.
  • Loading branch information
VineetReynolds committed Aug 7, 2013
1 parent 187d9cd commit 33ef7c6
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 33ef7c6

Please sign in to comment.