Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,33 @@ public void endVisit(MethodDeclaration node) {
}

@Override
/**
* Must create a FamixJavaThrownException relation and thus must find the appropriate FamixJavaException type
*
* There is a difficulty with UnionType:
* <code>catch (SAXException | IOException e) { throw e; }</code>
* where JDT reports java.lang.object for the type of the exception thrown !?!?!<BR>
* There are also other cases where the type is unknown. <BR>
* In these case, we force <code>java.lang.Throwable</code>
*/
public boolean visit(ThrowStatement node) {
TType thrownExceptionType = null;
TThrowable excepFmx = null;
ITypeBinding exceptTypeBnd = null;

Method meth = (Method) this.context.topMethod();
TType thrownExceptionType = this.referredType(node.getExpression().resolveTypeBinding(), (TNamedEntity) context.topType(), true);
TThrowable excepFmx;

exceptTypeBnd = node.getExpression().resolveTypeBinding();
if ( (exceptTypeBnd != null) && (! exceptTypeBnd.getQualifiedName().equals("java.lang.Object")) ) {
thrownExceptionType = this.referredType(exceptTypeBnd, (TNamedEntity) context.topType(), true);
}

if (thrownExceptionType == null) {
excepFmx = dico.ensureFamixException(null, "Throwable", null, false, EntityDictionary.UNKNOWN_MODIFIERS) ;
}
else {
excepFmx = dico.asException( thrownExceptionType);}
excepFmx = dico.asException( thrownExceptionType);
}
if (excepFmx != null) {
dico.createFamixThrownException(meth, excepFmx);
}
Expand All @@ -98,6 +116,9 @@ public boolean visit(ThrowStatement node) {
* The FormalParameter is represented by a SingleVariableDeclaration
*
* We set the type of the catchClause variable here because it would be more difficult in VisitorTypeRefRef
* <p>
* TODO handle UnionType such as in <code>catch (SAXException|IOException e)</code>
* see {@linkplain https://github.com/moosetechnology/VerveineJ/issues/185 }
*/
@Override
public boolean visit(CatchClause node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,8 @@ public void testOwnerOfObjectIsNotAlwaysJavaLang() {
@Test
/*
* Issue: https://github.com/moosetechnology/VerveineJ/issues/180
* Regression test ensuring that a class named Object does not always have "java.lang" as owner
*/
* Call of a (String) method directly on a BlockText
*/
public void testInvocationReceiverCanBeATextBlock() {
parse(new String[]{"src/test/resources/ad_hoc/TextBlocks.java"});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import static org.junit.Assert.*;

import org.eclipse.jdt.core.dom.ThrowStatement;

public class VerveineJTest_Exceptions extends VerveineJTest_Basic {

/**
Expand Down Expand Up @@ -147,4 +149,19 @@ public void testStubExpressionInThrow() {
assertEquals( "Throwable", ((org.moosetechnology.model.famix.famixjavaentities.Exception)inferredException).getName());
}

@Test
/*
* Not really testing the intended special case in <code>VisitorExceptionRef.visit(ThrowStatement)</code>
* because cannot get <code>node.getExpression().resolveTypeBinding()</code> to return <code>Object</code>
* But at least, it is testing a case with <code>UnionType</code>
*/
public void testThrowingUnionTypeException() {
org.moosetechnology.model.famix.famixjavaentities.Method throwerMethod = detectFamixElement(org.moosetechnology.model.famix.famixjavaentities.Method.class , "unionTypeThrower");

assertNotNull(throwerMethod);

assertEquals(1,throwerMethod.getThrownExceptions().size());
assertEquals("Throwable", ((TNamedEntity)firstElt(throwerMethod.getThrownExceptions())).getName() );
}

}
16 changes: 16 additions & 0 deletions app/src/test/resources/exceptions/CatchUnionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

package exceptions;

import java.io.IOError;
import org.xml.sax.SAXException;

class CatchUnionException {
public void unionTypeThrower() {
try {
// nothing
}
catch (SAXException | IOError e) {
throw e;
}
}
}