Fix for Bug 526996: CompilationUnit.setCommentTable(...) throws
IllegalStateException
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/dom/StandAloneASTParserTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/dom/StandAloneASTParserTest.java
index 9df7f76..9254c0b 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/dom/StandAloneASTParserTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/dom/StandAloneASTParserTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2016 IBM Corporation and others.
+ * Copyright (c) 2010, 2017 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -677,4 +677,99 @@
 	    parser.setCompilerOptions(options1);
 	    assertNotNull(parser.createAST(null));
 	}
-}
+	public void testBug526996_001() {
+		File rootDir = new File(System.getProperty("java.io.tmpdir"));
+		String contents = 
+				"public class X {\n" +
+				"    public X() {\n" +
+				"        this.f16132b =\n" +
+				"/*\n" +
+				"        at jadx.api.JavaClass.decompile(JavaClass.java:62)\n" +
+				"*/\n" +
+				"\n" +
+				"            /* JADX WARNING: inconsistent code. */\n" +
+				"            /* Code decompiled incorrectly, please refer to instructions dump. */\n" +
+				"            public final C1984r m22030a() {\n" +
+				"            }\n" +
+				"        }\n" +
+				"\n";
+		
+		File file = new File(rootDir, "X.java");
+		Writer writer = null;
+		try {
+			try {
+				writer = new BufferedWriter(new FileWriter(file));
+				writer.write(contents);
+			} catch (IOException e1) {
+				// ignore
+			}
+		} finally {
+			if (writer != null) {
+				try {
+					writer.close();
+				} catch(IOException e) {
+					// ignore
+				}
+			}
+		}
+		String contents2 =
+				"public class Y {\n" +
+				"\n" +
+				"    /* JADX WARNING: inconsistent code. */\n" +
+				"    protected void finalize() {\n" +
+				"        for (i =\n" +
+				"/*\n" +
+				"        at jadx.core.codegen.InsnGen.makeInsn(InsnGen.java:220)\n" +
+				"*/\n" +
+				"        public void close() { }\n" +
+				"    }\n" ;
+
+		File fileY = new File(rootDir, "Y.java");
+		Writer writer2 = null;
+		try {
+			try {
+				writer2 = new BufferedWriter(new FileWriter(fileY));
+				writer2.write(contents2);
+			} catch (IOException e) {
+				// ignore
+			}
+		} finally {
+			try {
+				if (writer2 != null) writer2.close();
+			} catch(IOException e) {
+				// ignore
+			}
+		}
+		try {
+			final FileASTRequestor astRequestor = new FileASTRequestor() {
+				@Override
+				public void acceptAST(String sourceFilePath, CompilationUnit ast) {
+					super.acceptAST(sourceFilePath, ast);
+					System.out.println(sourceFilePath);
+				}
+			};
+			ASTParser parser = ASTParser.newParser(AST.JLS9);
+			parser.setResolveBindings(true);
+			parser.setStatementsRecovery(true);
+			parser.setBindingsRecovery(true);
+			parser.setKind(ASTParser.K_COMPILATION_UNIT);
+			parser.setEnvironment(new String[0], new String[] { rootDir.getAbsolutePath() }, null, true);
+		    String[] files = null;
+			try {
+				files = new String[] {file.getCanonicalPath(), fileY.getCanonicalPath()};
+				System.out.println("Building...");
+				parser.createASTs(files,
+						null,
+						new String[0],
+						astRequestor,
+						null);
+			} catch (IOException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+		} finally {
+			file.delete();
+			fileY.delete();
+		}
+	}
+}
\ No newline at end of file
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/CommentRecorderParser.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/CommentRecorderParser.java
index 3eb9ba0..b3e58f3 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/CommentRecorderParser.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/CommentRecorderParser.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2017 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -288,6 +288,7 @@
 	private void pushOnCommentsStack(int start, int end) {
 
 		for (int i=start; i<=end; i++) {
+			if (this.scanner.commentPtr < i) break;
 			// First see if comment hasn't been already stored
 			int scannerStart = this.scanner.commentStarts[i]<0 ? -this.scanner.commentStarts[i] : this.scanner.commentStarts[i];
 			int commentStart = this.commentPtr == -1 ? -1 : (this.commentStarts[this.commentPtr]<0 ? -this.commentStarts[this.commentPtr] : this.commentStarts[this.commentPtr]);