Skip to content

Commit

Permalink
tests for INCOMPATIBLE_THROWS_CLAUSE
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Feb 8, 2024
1 parent a709a53 commit 5fa9230
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,39 @@ public void testWildcardSuperType() throws Exception {
"reduce", "visibility");
}

@Test public void testIncompatibleThrowsClause() throws Exception {
var source = "class Foo extends test.ExceptionThrowing {"
+ "def void ioException() throws Exception {} }";
var model = parse(source);
validationHelper.assertError(model, MY_METHOD, INCOMPATIBLE_THROWS_CLAUSE,
source.lastIndexOf("Exception"), "Exception".length(),
"Exception", "is not", "compatible", "throws", "clause");
}

/**
* Two incompatible exceptions from three supertypes;
* the marker is set on the offending exceptions only.
* https://github.com/eclipse/xtext/issues/2912
*/
@Test public void testIncompatibleThrowsClauseFromMultipleSuperTypes() throws Exception {
var source = "class Foo extends test.ExceptionThrowing "
+ "implements test.ExceptionThrowingInterface, test.ExceptionThrowingInterface2 {"
+ "def void nullPointerException() throws "
+ "NullPointerException, java.io.IOException, java.io.FileNotFoundException {} "
+ "}";
var model = parse(source);
var expectedSuffix = " is not compatible with the throws clause in "
+ "ExceptionThrowing.nullPointerException(), ExceptionThrowingInterface.nullPointerException() and ExceptionThrowingInterface2.nullPointerException()";
validationHelper.assertError(model, MY_METHOD, INCOMPATIBLE_THROWS_CLAUSE,
source.indexOf("java.io.IOException"), "java.io.IOException".length(),
"declared exception IOException",
expectedSuffix);
validationHelper.assertError(model, MY_METHOD, INCOMPATIBLE_THROWS_CLAUSE,
source.indexOf("java.io.FileNotFoundException"), "java.io.FileNotFoundException".length(),
"declared exception FileNotFoundException",
expectedSuffix);
}

@Test public void testDuplicateParameter() throws Exception {
var source = "class Foo { def void foo(int x, int x) {} }";
var model = parse(source);
Expand Down
24 changes: 24 additions & 0 deletions org.eclipse.xtext.xbase.tests/testdata/test/ExceptionThrowing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2024 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package test;

import java.io.IOException;

/**
* @author Lorenzo Bettini - Initial contribution and API
*/
public class ExceptionThrowing {

public void ioException() throws IOException {
}

public void nullPointerException() throws NullPointerException {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 2024 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package test;

/**
* @author Lorenzo Bettini - Initial contribution and API
*/
interface ExceptionThrowingInterface {

void nullPointerException() throws NullPointerException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
* Copyright (c) 2024 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package test;

/**
* @author Lorenzo Bettini - Initial contribution and API
*/
interface ExceptionThrowingInterface2 {

void nullPointerException() throws NullPointerException;

}

0 comments on commit 5fa9230

Please sign in to comment.