View | Details | Raw Unified | Return to bug 270148
Collapse All | Expand All

(-)model/org/eclipse/jdt/internal/core/util/CodeSnippetParsingUtil.java (-11 / +24 lines)
Lines 33-39 Link Here
33
33
34
	public RecordedParsingInformation recordedParsingInformation;
34
	public RecordedParsingInformation recordedParsingInformation;
35
35
36
	private RecordedParsingInformation getRecordedParsingInformation(CompilationResult compilationResult, CommentRecorderParser parser) {
36
	private RecordedParsingInformation getRecordedParsingInformation(CompilationResult compilationResult, int[][] commentPositions) {
37
		int problemsCount = compilationResult.problemCount;
37
		int problemsCount = compilationResult.problemCount;
38
		CategorizedProblem[] problems = null;
38
		CategorizedProblem[] problems = null;
39
		if (problemsCount != 0) {
39
		if (problemsCount != 0) {
Lines 44-57 Link Here
44
				System.arraycopy(compilationResultProblems, 0, (problems = new CategorizedProblem[problemsCount]), 0, problemsCount);
44
				System.arraycopy(compilationResultProblems, 0, (problems = new CategorizedProblem[problemsCount]), 0, problemsCount);
45
			}
45
			}
46
		}
46
		}
47
		return new RecordedParsingInformation(problems, compilationResult.getLineSeparatorPositions(), parser.getCommentsPositions());
47
		return new RecordedParsingInformation(problems, compilationResult.getLineSeparatorPositions(), commentPositions);
48
	}
48
	}
49
49
50
	public ASTNode[] parseClassBodyDeclarations(char[] source, Map settings, boolean recordParsingInformation) {
50
	public ASTNode[] parseClassBodyDeclarations(char[] source, Map settings, boolean recordParsingInformation) {
51
		return parseClassBodyDeclarations(source, 0, source.length, settings, recordParsingInformation);
51
		return parseClassBodyDeclarations(source, 0, source.length, settings, recordParsingInformation, false);
52
	}
52
	}
53
53
54
	public ASTNode[] parseClassBodyDeclarations(char[] source, int offset, int length, Map settings, boolean recordParsingInformation) {
54
	public ASTNode[] parseClassBodyDeclarations(
55
			char[] source,
56
			int offset,
57
			int length,
58
			Map settings,
59
			boolean recordParsingInformation,
60
			boolean enabledStatementRecovery) {
55
		if (source == null) {
61
		if (source == null) {
56
			throw new IllegalArgumentException();
62
			throw new IllegalArgumentException();
57
		}
63
		}
Lines 63-69 Link Here
63
69
64
		CommentRecorderParser parser = new CommentRecorderParser(problemReporter, false);
70
		CommentRecorderParser parser = new CommentRecorderParser(problemReporter, false);
65
		parser.setMethodsFullRecovery(false);
71
		parser.setMethodsFullRecovery(false);
66
		parser.setStatementsRecovery(false);
72
		parser.setStatementsRecovery(enabledStatementRecovery);
67
73
68
		ICompilationUnit sourceUnit =
74
		ICompilationUnit sourceUnit =
69
			new CompilationUnit(
75
			new CompilationUnit(
Lines 76-82 Link Here
76
		ASTNode[] result = parser.parseClassBodyDeclarations(source, offset, length, compilationUnitDeclaration);
82
		ASTNode[] result = parser.parseClassBodyDeclarations(source, offset, length, compilationUnitDeclaration);
77
83
78
		if (recordParsingInformation) {
84
		if (recordParsingInformation) {
79
			this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, parser);
85
			this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, compilationUnitDeclaration.comments);
80
		}
86
		}
81
		return result;
87
		return result;
82
	}
88
	}
Lines 103-109 Link Here
103
		CompilationUnitDeclaration compilationUnitDeclaration = parser.dietParse(sourceUnit, compilationResult);
109
		CompilationUnitDeclaration compilationUnitDeclaration = parser.dietParse(sourceUnit, compilationResult);
104
110
105
		if (recordParsingInformation) {
111
		if (recordParsingInformation) {
106
			this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, parser);
112
			this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, compilationUnitDeclaration.comments);
107
		}
113
		}
108
114
109
		if (compilationUnitDeclaration.ignoreMethodBodies) {
115
		if (compilationUnitDeclaration.ignoreMethodBodies) {
Lines 152-161 Link Here
152
				compilerOptions.defaultEncoding);
158
				compilerOptions.defaultEncoding);
153
159
154
		CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit);
160
		CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit);
155
		Expression result = parser.parseExpression(source, offset, length, new CompilationUnitDeclaration(problemReporter, compilationResult, source.length));
161
		CompilationUnitDeclaration unit = new CompilationUnitDeclaration(problemReporter, compilationResult, source.length);
162
		Expression result = parser.parseExpression(source, offset, length, unit);
156
163
157
		if (recordParsingInformation) {
164
		if (recordParsingInformation) {
158
			this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, parser);
165
			this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, unit.comments);
159
		}
166
		}
160
		return result;
167
		return result;
161
	}
168
	}
Lines 164-170 Link Here
164
		return parseStatements(source, 0, source.length, settings, recordParsingInformation, enabledStatementRecovery);
171
		return parseStatements(source, 0, source.length, settings, recordParsingInformation, enabledStatementRecovery);
165
	}
172
	}
166
173
167
	public ConstructorDeclaration parseStatements(char[] source, int offset, int length, Map settings, boolean recordParsingInformation, boolean enabledStatementRecovery) {
174
	public ConstructorDeclaration parseStatements(
175
			char[] source,
176
			int offset,
177
			int length,
178
			Map settings,
179
			boolean recordParsingInformation,
180
			boolean enabledStatementRecovery) {
168
		if (source == null) {
181
		if (source == null) {
169
			throw new IllegalArgumentException();
182
			throw new IllegalArgumentException();
170
		}
183
		}
Lines 197-203 Link Here
197
		parser.parse(constructorDeclaration, compilationUnitDeclaration, true);
210
		parser.parse(constructorDeclaration, compilationUnitDeclaration, true);
198
211
199
		if (recordParsingInformation) {
212
		if (recordParsingInformation) {
200
			this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, parser);
213
			this.recordedParsingInformation = getRecordedParsingInformation(compilationResult, compilationUnitDeclaration.comments);
201
		}
214
		}
202
		return constructorDeclaration;
215
		return constructorDeclaration;
203
	}
216
	}
(-)compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java (-9 / +93 lines)
Lines 9670-9675 Link Here
9670
	}
9670
	}
9671
}
9671
}
9672
public ASTNode[] parseClassBodyDeclarations(char[] source, int offset, int length, CompilationUnitDeclaration unit) {
9672
public ASTNode[] parseClassBodyDeclarations(char[] source, int offset, int length, CompilationUnitDeclaration unit) {
9673
	boolean oldDiet = this.diet;
9673
	/* automaton initialization */
9674
	/* automaton initialization */
9674
	initialize();
9675
	initialize();
9675
	goForClassBodyDeclarations();
9676
	goForClassBodyDeclarations();
Lines 9685-9711 Link Here
9685
	this.nestedType = 1;
9686
	this.nestedType = 1;
9686
9687
9687
	/* unit creation */
9688
	/* unit creation */
9688
	this.referenceContext = unit;
9689
	TypeDeclaration referenceContextTypeDeclaration = new TypeDeclaration(unit.compilationResult);
9690
	referenceContextTypeDeclaration.name = Util.EMPTY_STRING.toCharArray();
9691
	referenceContextTypeDeclaration.fields = new FieldDeclaration[0];
9689
	this.compilationUnit = unit;
9692
	this.compilationUnit = unit;
9693
	unit.types = new TypeDeclaration[1];
9694
	unit.types[0] = referenceContextTypeDeclaration;
9695
	this.referenceContext = unit;
9690
9696
9691
	/* run automaton */
9697
	/* run automaton */
9692
	try {
9698
	try {
9699
		this.diet = true;
9693
		parse();
9700
		parse();
9694
	} catch (AbortCompilation ex) {
9701
	} catch (AbortCompilation ex) {
9695
		this.lastAct = ERROR_ACTION;
9702
		this.lastAct = ERROR_ACTION;
9703
	} finally {
9704
		this.diet = oldDiet;
9696
	}
9705
	}
9697
9706
9707
	ASTNode[] result = null;
9698
	if (this.lastAct == ERROR_ACTION) {
9708
	if (this.lastAct == ERROR_ACTION) {
9699
		return null;
9709
		if (!this.options.performMethodsFullRecovery && !this.options.performStatementsRecovery) {
9710
			return null;
9711
		}
9712
		// collect all body declaration inside the compilation unit except the default constructor
9713
		final List bodyDeclarations = new ArrayList();
9714
		ASTVisitor visitor = new ASTVisitor() {
9715
			public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
9716
				if (!methodDeclaration.isDefaultConstructor()) {
9717
					bodyDeclarations.add(methodDeclaration);
9718
				}
9719
				return false;
9720
			}
9721
			public boolean visit(FieldDeclaration fieldDeclaration, MethodScope scope) {
9722
				bodyDeclarations.add(fieldDeclaration);
9723
				return false;
9724
			}
9725
			public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) {
9726
				bodyDeclarations.add(memberTypeDeclaration);
9727
				return false;
9728
			}
9729
		};
9730
		unit.ignoreFurtherInvestigation = false;
9731
		unit.traverse(visitor, unit.scope);
9732
		unit.ignoreFurtherInvestigation = true;
9733
		result = (ASTNode[]) bodyDeclarations.toArray(new ASTNode[bodyDeclarations.size()]);
9734
	} else {
9735
		int astLength;
9736
		if (this.astLengthPtr > -1 && (astLength = this.astLengthStack[this.astLengthPtr--]) != 0) {
9737
			result = new ASTNode[astLength];
9738
			this.astPtr -= astLength;
9739
			System.arraycopy(this.astStack, this.astPtr + 1, result, 0, astLength);
9740
		}
9741
	}
9742
	boolean containsInitializers = false;
9743
	TypeDeclaration typeDeclaration = null;
9744
	for (int i = 0, max = result.length; i< max; i++) {
9745
		// parse each class body declaration
9746
		ASTNode node = result[i];
9747
		if (node instanceof TypeDeclaration) {
9748
			((TypeDeclaration) node).parseMethods(this, unit);
9749
		} else if (node instanceof AbstractMethodDeclaration) {
9750
			((AbstractMethodDeclaration) node).parseStatements(this, unit);
9751
		} else if (node instanceof FieldDeclaration) {
9752
			FieldDeclaration fieldDeclaration = (FieldDeclaration) node;
9753
			switch(fieldDeclaration.getKind()) {
9754
				case AbstractVariableDeclaration.INITIALIZER:
9755
					containsInitializers = true;
9756
					if (typeDeclaration == null) {
9757
						typeDeclaration = referenceContextTypeDeclaration;
9758
					}
9759
					if (typeDeclaration.fields == null) {
9760
						typeDeclaration.fields = new FieldDeclaration[1];
9761
						typeDeclaration.fields[0] = fieldDeclaration;
9762
					} else {
9763
						int length2 = typeDeclaration.fields.length;
9764
						FieldDeclaration[] temp = new FieldDeclaration[length2 + 1];
9765
						System.arraycopy(typeDeclaration.fields, 0, temp, 0, length2);
9766
						temp[length2] = fieldDeclaration;
9767
						typeDeclaration.fields = temp;
9768
					}
9769
					break;
9770
			}
9771
		}
9772
		if (this.lastAct == ERROR_ACTION && (!this.options.performMethodsFullRecovery && !this.options.performStatementsRecovery)) {
9773
			return null;
9774
		}
9700
	}
9775
	}
9701
	int astLength;
9776
	if (containsInitializers) {
9702
	if (this.astLengthPtr > -1 && (astLength = this.astLengthStack[this.astLengthPtr--]) != 0) {
9777
		FieldDeclaration[] fieldDeclarations = typeDeclaration.fields;
9703
		ASTNode[] result = new ASTNode[astLength];
9778
		for (int i = 0, max = fieldDeclarations.length; i < max; i++) {
9704
		this.astPtr -= astLength;
9779
			((Initializer) fieldDeclarations[i]).parseStatements(this, typeDeclaration , unit);
9705
		System.arraycopy(this.astStack, this.astPtr + 1, result, 0, astLength);
9780
			if (this.lastAct == ERROR_ACTION && (!this.options.performMethodsFullRecovery && !this.options.performStatementsRecovery)) {
9706
		return result;
9781
				return null;
9782
			}
9783
		}
9707
	}
9784
	}
9708
	return null;
9785
	return result;
9709
}
9786
}
9710
public Expression parseExpression(char[] source, int offset, int length, CompilationUnitDeclaration unit) {
9787
public Expression parseExpression(char[] source, int offset, int length, CompilationUnitDeclaration unit) {
9711
9788
Lines 10414-10419 Link Here
10414
	}
10491
	}
10415
	/* update recovery state with current error state of the parser */
10492
	/* update recovery state with current error state of the parser */
10416
	updateRecoveryState();
10493
	updateRecoveryState();
10494
	if (getFirstToken() == TokenNameAND) {
10495
		if (this.referenceContext instanceof CompilationUnitDeclaration) {
10496
			TypeDeclaration typeDeclaration = new TypeDeclaration(this.referenceContext.compilationResult());
10497
			typeDeclaration.name = Util.EMPTY_STRING.toCharArray();
10498
			this.currentElement = this.currentElement.add(typeDeclaration, 0);
10499
		}
10500
	}
10417
10501
10418
	if (this.lastPosistion < this.scanner.currentPosition) {
10502
	if (this.lastPosistion < this.scanner.currentPosition) {
10419
		this.lastPosistion = this.scanner.currentPosition;
10503
		this.lastPosistion = this.scanner.currentPosition;
(-)dom/org/eclipse/jdt/core/dom/ASTParser.java (-17 / +7 lines)
Lines 1099-1127 Link Here
1099
					return compilationUnit;
1099
					return compilationUnit;
1100
				}
1100
				}
1101
			case K_CLASS_BODY_DECLARATIONS :
1101
			case K_CLASS_BODY_DECLARATIONS :
1102
				final org.eclipse.jdt.internal.compiler.ast.ASTNode[] nodes = codeSnippetParsingUtil.parseClassBodyDeclarations(this.rawSource, this.sourceOffset, this.sourceLength, this.compilerOptions, true);
1102
				final org.eclipse.jdt.internal.compiler.ast.ASTNode[] nodes = codeSnippetParsingUtil.parseClassBodyDeclarations(this.rawSource, this.sourceOffset, this.sourceLength, this.compilerOptions, true, this.statementsRecovery);
1103
				recordedParsingInformation = codeSnippetParsingUtil.recordedParsingInformation;
1103
				recordedParsingInformation = codeSnippetParsingUtil.recordedParsingInformation;
1104
				comments = recordedParsingInformation.commentPositions;
1104
				comments = recordedParsingInformation.commentPositions;
1105
				if (comments != null) {
1105
				if (comments != null) {
1106
					converter.buildCommentsTable(compilationUnit, comments);
1106
					converter.buildCommentsTable(compilationUnit, comments);
1107
				}
1107
				}
1108
				compilationUnit.setLineEndTable(recordedParsingInformation.lineEnds);
1108
				compilationUnit.setLineEndTable(recordedParsingInformation.lineEnds);
1109
				if (nodes != null) {
1109
				TypeDeclaration typeDeclaration = converter.convert(nodes);
1110
					TypeDeclaration typeDeclaration = converter.convert(nodes);
1110
				typeDeclaration.setSourceRange(this.sourceOffset, this.sourceOffset + this.sourceLength);
1111
					typeDeclaration.setSourceRange(this.sourceOffset, this.sourceOffset + this.sourceLength);
1111
				rootNodeToCompilationUnit(typeDeclaration.getAST(), compilationUnit, typeDeclaration, codeSnippetParsingUtil.recordedParsingInformation, null);
1112
					rootNodeToCompilationUnit(typeDeclaration.getAST(), compilationUnit, typeDeclaration, codeSnippetParsingUtil.recordedParsingInformation, null);
1112
				ast.setDefaultNodeFlag(0);
1113
					ast.setDefaultNodeFlag(0);
1113
				ast.setOriginalModificationCount(ast.modificationCount());
1114
					ast.setOriginalModificationCount(ast.modificationCount());
1114
				return typeDeclaration;
1115
					return typeDeclaration;
1116
				} else {
1117
					CategorizedProblem[] problems = recordedParsingInformation.problems;
1118
					if (problems != null) {
1119
						compilationUnit.setProblems(problems);
1120
					}
1121
					ast.setDefaultNodeFlag(0);
1122
					ast.setOriginalModificationCount(ast.modificationCount());
1123
					return compilationUnit;
1124
				}
1125
		}
1115
		}
1126
		throw new IllegalStateException();
1116
		throw new IllegalStateException();
1127
	}
1117
	}
(-)dom/org/eclipse/jdt/core/dom/ASTConverter.java (-1 / +1 lines)
Lines 938-944 Link Here
938
	public TypeDeclaration convert(org.eclipse.jdt.internal.compiler.ast.ASTNode[] nodes) {
938
	public TypeDeclaration convert(org.eclipse.jdt.internal.compiler.ast.ASTNode[] nodes) {
939
		final TypeDeclaration typeDecl = new TypeDeclaration(this.ast);
939
		final TypeDeclaration typeDecl = new TypeDeclaration(this.ast);
940
		typeDecl.setInterface(false);
940
		typeDecl.setInterface(false);
941
		int nodesLength = nodes.length;
941
		int nodesLength = nodes == null ? 0 : nodes.length;
942
		for (int i = 0; i < nodesLength; i++) {
942
		for (int i = 0; i < nodesLength; i++) {
943
			org.eclipse.jdt.internal.compiler.ast.ASTNode node = nodes[i];
943
			org.eclipse.jdt.internal.compiler.ast.ASTNode node = nodes[i];
944
			if (node instanceof org.eclipse.jdt.internal.compiler.ast.Initializer) {
944
			if (node instanceof org.eclipse.jdt.internal.compiler.ast.Initializer) {
(-)model/org/eclipse/jdt/internal/core/CreateTypeMemberOperation.java (-7 / +10 lines)
Lines 90-103 Link Here
90
		} else {
90
		} else {
91
			TypeDeclaration typeDeclaration = (TypeDeclaration) node;
91
			TypeDeclaration typeDeclaration = (TypeDeclaration) node;
92
			if ((typeDeclaration.getFlags() & ASTNode.MALFORMED) != 0) {
92
			if ((typeDeclaration.getFlags() & ASTNode.MALFORMED) != 0) {
93
				throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
93
				createdNodeSource = generateSyntaxIncorrectAST();
94
			}
94
				if (this.createdNode == null)
95
			List bodyDeclarations = typeDeclaration.bodyDeclarations();
95
					throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
96
			if (bodyDeclarations.size() == 0) {
96
			} else {
97
				throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
97
				List bodyDeclarations = typeDeclaration.bodyDeclarations();
98
				if (bodyDeclarations.size() == 0) {
99
					throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
100
				}
101
				this.createdNode = (ASTNode) bodyDeclarations.iterator().next();
102
				createdNodeSource = this.source;
98
			}
103
			}
99
			this.createdNode = (ASTNode) bodyDeclarations.iterator().next();
100
			createdNodeSource = this.source;
101
		}
104
		}
102
		if (this.alteredName != null) {
105
		if (this.alteredName != null) {
103
			SimpleName newName = this.createdNode.getAST().newSimpleName(this.alteredName);
106
			SimpleName newName = this.createdNode.getAST().newSimpleName(this.alteredName);
(-)src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST3_2.java (-16 / +150 lines)
Lines 122-128 Link Here
122
	static {
122
	static {
123
//		TESTS_NAMES = new String[] {"test0602"};
123
//		TESTS_NAMES = new String[] {"test0602"};
124
//		TESTS_RANGE = new int[] { 670, -1 };
124
//		TESTS_RANGE = new int[] { 670, -1 };
125
//		TESTS_NUMBERS =  new int[] { 702 };
125
//		TESTS_NUMBERS =  new int[] { 708 };
126
	}
126
	}
127
	public static Test suite() {
127
	public static Test suite() {
128
		return buildModelTestSuite(ASTConverterTestAST3_2.class);
128
		return buildModelTestSuite(ASTConverterTestAST3_2.class);
Lines 4115-4122 Link Here
4115
		parser.setCompilerOptions(JavaCore.getOptions());
4115
		parser.setCompilerOptions(JavaCore.getOptions());
4116
		ASTNode result2 = parser.createAST(null);
4116
		ASTNode result2 = parser.createAST(null);
4117
		assertNotNull("No node", result2);
4117
		assertNotNull("No node", result2);
4118
		assertTrue("not a compilation unit", result2.getNodeType() == ASTNode.COMPILATION_UNIT);
4118
		assertTrue("not a type declaration", result2.getNodeType() == ASTNode.TYPE_DECLARATION);
4119
		CompilationUnit compilationUnit = (CompilationUnit) result2;
4119
		CompilationUnit compilationUnit = (CompilationUnit) ((TypeDeclaration) result2).getParent();
4120
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4120
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4121
	}
4121
	}
4122
	/**
4122
	/**
Lines 4140-4147 Link Here
4140
		parser.setCompilerOptions(JavaCore.getOptions());
4140
		parser.setCompilerOptions(JavaCore.getOptions());
4141
		ASTNode result2 = parser.createAST(null);
4141
		ASTNode result2 = parser.createAST(null);
4142
		assertNotNull("No node", result2);
4142
		assertNotNull("No node", result2);
4143
		assertTrue("not a compilation unit", result2.getNodeType() == ASTNode.COMPILATION_UNIT);
4143
		assertTrue("not a type declaration", result2.getNodeType() == ASTNode.TYPE_DECLARATION);
4144
		CompilationUnit compilationUnit = (CompilationUnit) result2;
4144
		CompilationUnit compilationUnit = (CompilationUnit) ((TypeDeclaration) result2).getParent();
4145
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4145
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4146
	}
4146
	}
4147
	/**
4147
	/**
Lines 4165-4172 Link Here
4165
		parser.setCompilerOptions(JavaCore.getOptions());
4165
		parser.setCompilerOptions(JavaCore.getOptions());
4166
		ASTNode result2 = parser.createAST(null);
4166
		ASTNode result2 = parser.createAST(null);
4167
		assertNotNull("No node", result2);
4167
		assertNotNull("No node", result2);
4168
		assertTrue("not a compilation unit", result2.getNodeType() == ASTNode.COMPILATION_UNIT);
4168
		assertTrue("not a type declaration", result2.getNodeType() == ASTNode.TYPE_DECLARATION);
4169
		CompilationUnit compilationUnit = (CompilationUnit) result2;
4169
		CompilationUnit compilationUnit = (CompilationUnit) ((TypeDeclaration) result2).getParent();
4170
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4170
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4171
	}
4171
	}
4172
	/**
4172
	/**
Lines 10104-10120 Link Here
10104
		}
10104
		}
10105
	}
10105
	}
10106
	/**
10106
	/**
10107
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=264590
10107
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=270148
10108
	 */
10108
	 */
10109
	public void _test0702() throws JavaModelException {
10109
	public void test0702() throws JavaModelException {
10110
		final char[] source = ("void foo() {\n" + 
10111
				"	Integer I = new ${cursor}\n" +
10112
				"}").toCharArray();
10110
		ASTParser parser = ASTParser.newParser(AST.JLS3);
10113
		ASTParser parser = ASTParser.newParser(AST.JLS3);
10111
		parser.setKind(ASTParser.K_EXPRESSION);
10114
		parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
10112
		String commentsText = "/**\n * @generated\n */\n";
10115
		parser.setStatementsRecovery(true);
10113
		char[] source = commentsText.toCharArray();
10114
		parser.setSource(source);
10116
		parser.setSource(source);
10115
		parser.setSourceRange(0, source.length);
10117
		ASTNode root = parser.createAST(null);
10116
		parser.setCompilerOptions(JavaCore.getOptions());
10118
		assertEquals("Not a type declaration", ASTNode.TYPE_DECLARATION, root.getNodeType());
10117
		ASTNode result = parser.createAST(null);
10119
		TypeDeclaration typeDeclaration = (TypeDeclaration) root;
10118
		assertEquals("not a compilation unit", ASTNode.COMPILATION_UNIT, result.getNodeType());
10120
		List bodyDeclarations = typeDeclaration.bodyDeclarations();
10121
		assertEquals("Wrong size", 1, bodyDeclarations.size());
10122
		BodyDeclaration bodyDeclaration = (BodyDeclaration) bodyDeclarations.get(0);
10123
		assertEquals("Not a method declaration", ASTNode.METHOD_DECLARATION, bodyDeclaration.getNodeType());
10124
		MethodDeclaration declaration = (MethodDeclaration) bodyDeclaration;
10125
		// check if there is a body with one statement in it
10126
		assertEquals("No statement found", 1, declaration.getBody().statements().size());
10127
	}
10128
	/**
10129
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=270148
10130
	 */
10131
	public void test0703() throws JavaModelException {
10132
		final char[] source = ("public class Try {\n" + 
10133
				"	void foo() {\n" + 
10134
				"		Integer I = new ${cursor}\n" + 
10135
				"	}\n" + 
10136
				"}").toCharArray();
10137
		ASTParser parser = ASTParser.newParser(AST.JLS3);
10138
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
10139
		parser.setStatementsRecovery(true);
10140
		parser.setSource(source);
10141
		ASTNode root = parser.createAST(null);
10142
		assertEquals("Not a compilation declaration", ASTNode.COMPILATION_UNIT, root.getNodeType());
10143
		TypeDeclaration typeDeclaration = (TypeDeclaration) ((CompilationUnit) root).types().get(0);
10144
		List bodyDeclarations = typeDeclaration.bodyDeclarations();
10145
		assertEquals("Wrong size", 1, bodyDeclarations.size());
10146
		BodyDeclaration bodyDeclaration = (BodyDeclaration) bodyDeclarations.get(0);
10147
		assertEquals("Not a method declaration", ASTNode.METHOD_DECLARATION, bodyDeclaration.getNodeType());
10148
		MethodDeclaration declaration = (MethodDeclaration) bodyDeclaration;
10149
		// check if there is a body with one statement in it
10150
		assertEquals("No statement found", 1, declaration.getBody().statements().size());
10151
	}
10152
	/**
10153
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=270148
10154
	 */
10155
	public void test0704() throws JavaModelException {
10156
		final char[] source = ("{\n" + 
10157
				"	Integer I = new ${cursor}\n" +
10158
				"}").toCharArray();
10159
		ASTParser parser = ASTParser.newParser(AST.JLS3);
10160
		parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
10161
		parser.setStatementsRecovery(true);
10162
		parser.setSource(source);
10163
		ASTNode root = parser.createAST(null);
10164
		assertEquals("Not a type declaration", ASTNode.TYPE_DECLARATION, root.getNodeType());
10165
		TypeDeclaration typeDeclaration = (TypeDeclaration) root;
10166
		List bodyDeclarations = typeDeclaration.bodyDeclarations();
10167
		assertEquals("Wrong size", 1, bodyDeclarations.size());
10168
		BodyDeclaration bodyDeclaration = (BodyDeclaration) bodyDeclarations.get(0);
10169
		assertEquals("Not an initializer", ASTNode.INITIALIZER, bodyDeclaration.getNodeType());
10170
		Initializer initializer = (Initializer) bodyDeclaration;
10171
		// check if there is a body with one statement in it
10172
		assertEquals("No statement found", 1, initializer.getBody().statements().size());
10173
	}
10174
	/**
10175
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=270148
10176
	 */
10177
	public void test0705() throws JavaModelException {
10178
		final char[] source = ("{\n" + 
10179
				"	Integer I = new ${cursor}\n" +
10180
				"}\n" +
10181
				"{\n" + 
10182
				"	Integer I = new ${cursor}\n" +
10183
				"}").toCharArray();
10184
		ASTParser parser = ASTParser.newParser(AST.JLS3);
10185
		parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
10186
		parser.setStatementsRecovery(true);
10187
		parser.setSource(source);
10188
		ASTNode root = parser.createAST(null);
10189
		assertEquals("Not a type declaration", ASTNode.TYPE_DECLARATION, root.getNodeType());
10190
		TypeDeclaration typeDeclaration = (TypeDeclaration) root;
10191
		List bodyDeclarations = typeDeclaration.bodyDeclarations();
10192
		assertEquals("Wrong size", 2, bodyDeclarations.size());
10193
		BodyDeclaration bodyDeclaration = (BodyDeclaration) bodyDeclarations.get(0);
10194
		assertEquals("Not an initializer", ASTNode.INITIALIZER, bodyDeclaration.getNodeType());
10195
		Initializer initializer = (Initializer) bodyDeclaration;
10196
		// check if there is a body with one statement in it
10197
		assertEquals("No statement found", 1, initializer.getBody().statements().size());
10198
		bodyDeclaration = (BodyDeclaration) bodyDeclarations.get(1);
10199
		assertEquals("Not an initializer", ASTNode.INITIALIZER, bodyDeclaration.getNodeType());
10200
		initializer = (Initializer) bodyDeclaration;
10201
		// check if there is a body with one statement in it
10202
		assertEquals("No statement found", 1, initializer.getBody().statements().size());
10203
	}
10204
	/**
10205
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=270148
10206
	 */
10207
	public void test0706() throws JavaModelException {
10208
		final char[] source = ("public class Try {\n" + 
10209
				"	Integer i = new Integer() {\n" + 
10210
				"		Integer I = new ${cursor}\n" + 
10211
				"	};\"\n" + 
10212
				"}").toCharArray();
10213
		ASTParser parser = ASTParser.newParser(AST.JLS3);
10214
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
10215
		parser.setStatementsRecovery(true);
10216
		parser.setSource(source);
10217
		ASTNode root = parser.createAST(null);
10218
		assertEquals("Not a compilation declaration", ASTNode.COMPILATION_UNIT, root.getNodeType());
10219
		TypeDeclaration typeDeclaration = (TypeDeclaration) ((CompilationUnit) root).types().get(0);
10220
		List bodyDeclarations = typeDeclaration.bodyDeclarations();
10221
		assertEquals("Wrong size", 1, bodyDeclarations.size());
10222
	}
10223
	/**
10224
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=270148
10225
	 */
10226
	public void test0707() throws JavaModelException {
10227
		final char[] source = ("Integer i = new Integer() {\n" + 
10228
				"	Integer I = new ${cursor}\n" + 
10229
				"};").toCharArray();
10230
		ASTParser parser = ASTParser.newParser(AST.JLS3);
10231
		parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
10232
		parser.setStatementsRecovery(true);
10233
		parser.setSource(source);
10234
		ASTNode root = parser.createAST(null);
10235
		assertEquals("Not a type declaration", ASTNode.TYPE_DECLARATION, root.getNodeType());
10236
		List bodyDeclarations = ((TypeDeclaration) root).bodyDeclarations();
10237
		assertEquals("Wrong size", 1, bodyDeclarations.size());
10238
	}
10239
	/**
10240
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=270148
10241
	 */
10242
	public void test0708() throws JavaModelException {
10243
		final char[] source = ("System.out.println()\nint i;\n").toCharArray();
10244
		ASTParser parser = ASTParser.newParser(AST.JLS3);
10245
		parser.setKind(ASTParser.K_STATEMENTS);
10246
		parser.setStatementsRecovery(true);
10247
		parser.setSource(source);
10248
		ASTNode root = parser.createAST(null);
10249
		assertEquals("Not a block", ASTNode.BLOCK, root.getNodeType());
10250
		Block block = (Block) root;
10251
		List statements = block.statements();
10252
		assertEquals("Wrong size", 2, statements.size());
10119
	}
10253
	}
10120
}
10254
}
(-)src/org/eclipse/jdt/core/tests/dom/ASTConverterTest2.java (-6 / +6 lines)
Lines 4042-4049 Link Here
4042
		parser.setCompilerOptions(JavaCore.getOptions());
4042
		parser.setCompilerOptions(JavaCore.getOptions());
4043
		ASTNode result2 = parser.createAST(null);
4043
		ASTNode result2 = parser.createAST(null);
4044
		assertNotNull("No node", result2);
4044
		assertNotNull("No node", result2);
4045
		assertTrue("not a compilation unit", result2.getNodeType() == ASTNode.COMPILATION_UNIT);
4045
		assertTrue("not a type declaration", result2.getNodeType() == ASTNode.TYPE_DECLARATION);
4046
		CompilationUnit compilationUnit = (CompilationUnit) result2;
4046
		CompilationUnit compilationUnit = (CompilationUnit) ((TypeDeclaration) result2).getParent();
4047
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4047
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4048
	}
4048
	}
4049
	/**
4049
	/**
Lines 4067-4074 Link Here
4067
		parser.setCompilerOptions(JavaCore.getOptions());
4067
		parser.setCompilerOptions(JavaCore.getOptions());
4068
		ASTNode result2 = parser.createAST(null);
4068
		ASTNode result2 = parser.createAST(null);
4069
		assertNotNull("No node", result2);
4069
		assertNotNull("No node", result2);
4070
		assertTrue("not a compilation unit", result2.getNodeType() == ASTNode.COMPILATION_UNIT);
4070
		assertTrue("not a type declaration", result2.getNodeType() == ASTNode.TYPE_DECLARATION);
4071
		CompilationUnit compilationUnit = (CompilationUnit) result2;
4071
		CompilationUnit compilationUnit = (CompilationUnit) ((TypeDeclaration) result2).getParent();
4072
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4072
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4073
	}
4073
	}
4074
	/**
4074
	/**
Lines 4092-4099 Link Here
4092
		parser.setCompilerOptions(JavaCore.getOptions());
4092
		parser.setCompilerOptions(JavaCore.getOptions());
4093
		ASTNode result2 = parser.createAST(null);
4093
		ASTNode result2 = parser.createAST(null);
4094
		assertNotNull("No node", result2);
4094
		assertNotNull("No node", result2);
4095
		assertTrue("not a compilation unit", result2.getNodeType() == ASTNode.COMPILATION_UNIT);
4095
		assertTrue("not a type declaration", result2.getNodeType() == ASTNode.TYPE_DECLARATION);
4096
		CompilationUnit compilationUnit = (CompilationUnit) result2;
4096
		CompilationUnit compilationUnit = (CompilationUnit) ((TypeDeclaration) result2).getParent();
4097
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4097
		assertEquals("wrong problem size", 1, compilationUnit.getProblems().length);
4098
	}
4098
	}
4099
	/**
4099
	/**

Return to bug 270148