-
Notifications
You must be signed in to change notification settings - Fork 273
[TG-4345] Parse thrown exceptions in java #2607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thk123
merged 10 commits into
diffblue:develop
from
jeannielynnmoulton:jeannie/ParseThrownExceptions2
Jul 31, 2018
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fbad2d9
Rename variable extends to super_class
jeannielynnmoulton 5994dd8
Add method to get super class from java class type.
jeannielynnmoulton 7fcc42d
Adds const to get/set_outer_class
jeannielynnmoulton 5c7dcac
Parses the exception attribute
jeannielynnmoulton eb88509
Use parsed information for thrown exceptions.
jeannielynnmoulton 565c999
Unit tests throws exceptions parsing.
jeannielynnmoulton aa83622
Unit tests method get_super_class
jeannielynnmoulton 1134bba
Creates java_method_typet which extends code_typet
jeannielynnmoulton a6e7c4b
Refactors interface for exceptions to not use irepts.
jeannielynnmoulton 655248a
Add unit test for when there are no exceptions.
jeannielynnmoulton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public class ChildClass extends ParentClass { | ||
} | ||
|
||
class ParentClass extends GrandparentClass { | ||
} | ||
|
||
class GrandparentClass { | ||
} |
Binary file not shown.
Binary file added
BIN
+200 Bytes
jbmc/unit/java_bytecode/java_bytecode_parser/GrandparentClass.class
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+519 Bytes
jbmc/unit/java_bytecode/java_bytecode_parser/ThrowsExceptions.class
Binary file not shown.
18 changes: 18 additions & 0 deletions
18
jbmc/unit/java_bytecode/java_bytecode_parser/ThrowsExceptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.io.*; | ||
|
||
public class ThrowsExceptions { | ||
|
||
public static void test() throws CustomException, IOException { | ||
StringReader sr = new StringReader(""); | ||
sr.read(); | ||
throw new CustomException(); | ||
} | ||
|
||
public static void testNoExceptions() { | ||
StringReader sr = new StringReader(""); | ||
} | ||
|
||
} | ||
|
||
class CustomException extends Exception { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
#include <java-testing-utils/load_java_class.h> | ||
#include <java_bytecode/java_bytecode_convert_class.h> | ||
#include <java_bytecode/java_bytecode_convert_method.h> | ||
#include <java_bytecode/java_bytecode_parse_tree.h> | ||
#include <java_bytecode/java_types.h> | ||
#include <testing-utils/catch.hpp> | ||
|
@@ -596,4 +597,45 @@ SCENARIO( | |
} | ||
} | ||
} | ||
|
||
GIVEN("A method that may or may not throw exceptions") | ||
{ | ||
const symbol_tablet &new_symbol_table = load_java_class( | ||
"ThrowsExceptions", "./java_bytecode/java_bytecode_parser"); | ||
WHEN("Parsing the exceptions attribute for a method that throws exceptions") | ||
{ | ||
THEN("We should be able to get the list of exceptions it throws") | ||
{ | ||
const symbolt &method_symbol = | ||
new_symbol_table.lookup_ref("java::ThrowsExceptions.test:()V"); | ||
const java_method_typet method = | ||
to_java_method_type(method_symbol.type); | ||
const std::vector<irep_idt> exceptions = method.throws_exceptions(); | ||
REQUIRE(exceptions.size() == 2); | ||
REQUIRE( | ||
std::find( | ||
exceptions.begin(), | ||
exceptions.end(), | ||
irept("CustomException").id()) != exceptions.end()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to construct an irept and then get the |
||
REQUIRE( | ||
std::find( | ||
exceptions.begin(), | ||
exceptions.end(), | ||
irept("java.io.IOException").id()) != exceptions.end()); | ||
} | ||
} | ||
WHEN( | ||
"Parsing the exceptions attribute for a method that throws no exceptions") | ||
{ | ||
THEN("We should be able to get the list of exceptions it throws") | ||
{ | ||
const symbolt &method_symbol = new_symbol_table.lookup_ref( | ||
"java::ThrowsExceptions.testNoExceptions:()V"); | ||
const java_method_typet method = | ||
to_java_method_type(method_symbol.type); | ||
const std::vector<irep_idt> exceptions = method.throws_exceptions(); | ||
REQUIRE(exceptions.size() == 0); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To add a bit more type safety, you might want to add a
ID_C_java_method_type
so a caller can be sure that it really is ajava_method_typet
not acode_typet
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would this be a comment with
C_
in the name instead ofID_java_method_type
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistency with other types, which isn't a great reason. I think you're probably right in fact, if I have a C style function pointer that probably shouldn't be treated equivalently to a static java function that doesn't throw any exceptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been added in #2661