|
17 | 17 |
|
18 | 18 | import java.io.IOException; |
19 | 19 | import java.util.List; |
| 20 | +import java.util.Locale; |
20 | 21 | import junit.framework.Test; |
21 | 22 | import org.eclipse.core.runtime.CoreException; |
22 | 23 | import org.eclipse.jdt.core.BindingKey; |
|
29 | 30 | import org.eclipse.jdt.core.JavaCore; |
30 | 31 | import org.eclipse.jdt.core.JavaModelException; |
31 | 32 | import org.eclipse.jdt.core.dom.*; |
| 33 | +import org.eclipse.jdt.internal.compiler.CompilationResult; |
| 34 | +import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies; |
| 35 | +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; |
32 | 36 | import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
| 37 | +import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
| 38 | +import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; |
| 39 | +import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; |
33 | 40 | import org.eclipse.jdt.internal.core.ResolvedBinaryMethod; |
| 41 | +import org.eclipse.jdt.internal.core.dom.SourceRangeVerifier; |
| 42 | +import org.eclipse.jdt.internal.core.util.CommentRecorderParser; |
34 | 43 |
|
35 | 44 | @SuppressWarnings({"rawtypes"}) |
36 | 45 | public class ASTConverter18Test extends ConverterTestSetup { |
@@ -5513,4 +5522,80 @@ public void acceptBinding(String bindingKey, IBinding binding) { |
5513 | 5522 | parser.createASTs(new ICompilationUnit[] {this.workingCopy}, new String[0], requestor, null); |
5514 | 5523 | } |
5515 | 5524 |
|
| 5525 | +public void testIssue4712() throws JavaModelException { |
| 5526 | + String contents = |
| 5527 | + """ |
| 5528 | + public class Test { |
| 5529 | + public String test() { |
| 5530 | + // comment; insert System.out.println |
| 5531 | + return "'test' called"; |
| 5532 | + } |
| 5533 | + } |
| 5534 | + """; |
| 5535 | + this.workingCopy = getWorkingCopy("/Converter18/src/test432051/X.java", contents, true/*computeProblems*/); |
| 5536 | + CompilerOptions options = new CompilerOptions(); |
| 5537 | + options.sourceLevel = ClassFileConstants.JDK25; |
| 5538 | + options.complianceLevel = ClassFileConstants.JDK25; |
| 5539 | + options.docCommentSupport = true; |
| 5540 | + CommentRecorderParser parser = |
| 5541 | + new CommentRecorderParser( |
| 5542 | + new ProblemReporter( |
| 5543 | + DefaultErrorHandlingPolicies.proceedWithAllProblems(), |
| 5544 | + options, |
| 5545 | + new DefaultProblemFactory(Locale.getDefault())), |
| 5546 | + false); |
| 5547 | + char[] sourceChars = this.workingCopy.getSource().toCharArray(); |
| 5548 | + org.eclipse.jdt.internal.compiler.batch.CompilationUnit unit = |
| 5549 | + new org.eclipse.jdt.internal.compiler.batch.CompilationUnit(sourceChars, this.workingCopy.getElementName(), null); |
| 5550 | + CompilationResult compilationResult = new CompilationResult(unit, 0, 0, 10); |
| 5551 | + CompilationUnitDeclaration cud = parser.parse(unit, compilationResult); |
| 5552 | + org.eclipse.jdt.internal.compiler.ast.TypeDeclaration typeDecl = cud.types[0]; |
| 5553 | + char[][] tokens = { "System".toCharArray(), "out".toCharArray() }; |
| 5554 | + long[] positions = { 0L, 0L }; |
| 5555 | + org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference systemOut = |
| 5556 | + new org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference(tokens, positions, 0, 0); |
| 5557 | + org.eclipse.jdt.internal.compiler.ast.MessageSend printlnCall = |
| 5558 | + new org.eclipse.jdt.internal.compiler.ast.MessageSend(); |
| 5559 | + printlnCall.receiver = systemOut; |
| 5560 | + printlnCall.selector = "println".toCharArray(); |
| 5561 | + printlnCall.arguments = null; |
| 5562 | + printlnCall.sourceStart = typeDecl.sourceStart; |
| 5563 | + printlnCall.sourceEnd = typeDecl.sourceEnd; |
| 5564 | + printlnCall.nameSourcePosition = 0L; |
| 5565 | + org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration testMethod = null; |
| 5566 | + for (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration method : typeDecl.methods) { |
| 5567 | + if (new String(method.selector).equals("test")) { |
| 5568 | + testMethod = method; |
| 5569 | + break; |
| 5570 | + } |
| 5571 | + } |
| 5572 | + org.eclipse.jdt.internal.compiler.ast.Statement[] stmts = new org.eclipse.jdt.internal.compiler.ast.Statement[2]; |
| 5573 | + stmts[0] = printlnCall; |
| 5574 | + stmts[1] = testMethod.statements[0]; |
| 5575 | + testMethod.statements = stmts; |
| 5576 | + int reconcileFlags = ICompilationUnit.ENABLE_STATEMENTS_RECOVERY |
| 5577 | + | ICompilationUnit.ENABLE_BINDINGS_RECOVERY |
| 5578 | + | ICompilationUnit.FORCE_PROBLEM_DETECTION; |
| 5579 | + boolean oldDebug = SourceRangeVerifier.DEBUG; |
| 5580 | + boolean oldDebugThrow = SourceRangeVerifier.DEBUG_THROW; |
| 5581 | + try { |
| 5582 | + SourceRangeVerifier.DEBUG = false; |
| 5583 | + SourceRangeVerifier.DEBUG_THROW = false; |
| 5584 | + long start = System.currentTimeMillis(); |
| 5585 | + AST.convertCompilationUnit( |
| 5586 | + AST.getJLSLatest(), |
| 5587 | + cud, |
| 5588 | + options.getMap(), |
| 5589 | + false, |
| 5590 | + (org.eclipse.jdt.internal.core.CompilationUnit) this.workingCopy, |
| 5591 | + reconcileFlags, |
| 5592 | + null |
| 5593 | + ); |
| 5594 | + // The convertCompilationUnit method would need to take less than one second to complete. |
| 5595 | + assertTrue((System.currentTimeMillis() - start) < 1000); |
| 5596 | + } finally { |
| 5597 | + SourceRangeVerifier.DEBUG = oldDebug; |
| 5598 | + SourceRangeVerifier.DEBUG_THROW = oldDebugThrow; |
| 5599 | + } |
| 5600 | +} |
5516 | 5601 | } |
0 commit comments