Skip to content

Pretty constructors in java parse tree #2152

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

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 3 additions & 2 deletions src/java_bytecode/java_bytecode_convert_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ void java_bytecode_convert_method_lazy(
method_symbol.mode=ID_java;
method_symbol.location=m.source_location;
method_symbol.location.set_function(method_identifier);

if(m.is_public)
member_type.set_access(ID_public);
else if(m.is_protected)
Expand All @@ -363,7 +364,7 @@ void java_bytecode_convert_method_lazy(
else
member_type.set_access(ID_default);

if(is_constructor(method_symbol.base_name))
if(m.is_constructor())
{
method_symbol.pretty_name=
id2string(class_symbol.pretty_name)+"."+
Expand Down Expand Up @@ -563,7 +564,7 @@ void java_bytecode_convert_methodt::convert(
// The pretty name of a constructor includes the base name of the class
// instead of the internal method name "<init>". For regular methods, it's
// just the base name of the method.
if(is_constructor(method_symbol.base_name))
if(m.is_constructor())
{
method_symbol.pretty_name = id2string(class_symbol.pretty_name) + "." +
id2string(class_symbol.base_name) + "()";
Expand Down
28 changes: 15 additions & 13 deletions src/java_bytecode/java_bytecode_parse_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,16 @@ void java_bytecode_parse_treet::classt::output(std::ostream &out) const
out << " extends " << extends;
out << " {" << '\n';

for(fieldst::const_iterator
it=fields.begin();
it!=fields.end();
it++)
for(const auto &f : fields)
{
it->output(out);
f.output(name, out);
}

out << '\n';

for(methodst::const_iterator
it=methods.begin();
it!=methods.end();
it++)
for(const auto &m : methods)
{
it->output(out);
m.output(name, out);
}

out << '}' << '\n';
Expand Down Expand Up @@ -139,7 +133,9 @@ bool java_bytecode_parse_treet::does_annotation_exist(
}) != annotations.end();
}

void java_bytecode_parse_treet::methodt::output(std::ostream &out) const
void java_bytecode_parse_treet::methodt::output(
const irep_idt &class_name,
std::ostream &out) const
{
symbol_tablet symbol_table;
namespacet ns(symbol_table);
Expand Down Expand Up @@ -174,7 +170,11 @@ void java_bytecode_parse_treet::methodt::output(std::ostream &out) const
if(is_synchronized)
out << "synchronized ";

out << name;
if(is_constructor())
out << class_name;
else
out << name;

out << " : " << descriptor;

out << '\n';
Expand Down Expand Up @@ -218,7 +218,9 @@ void java_bytecode_parse_treet::methodt::output(std::ostream &out) const
out << '\n';
}

void java_bytecode_parse_treet::fieldt::output(std::ostream &out) const
void java_bytecode_parse_treet::fieldt::output(
const irep_idt &class_name,
std::ostream &out) const
{
for(const auto &annotation : annotations)
{
Expand Down
20 changes: 17 additions & 3 deletions src/java_bytecode/java_bytecode_parse_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class java_bytecode_parse_treet
bool is_public, is_protected, is_private, is_static, is_final;
annotationst annotations;

virtual void output(std::ostream &out) const = 0;
virtual void output(
const irep_idt &class_name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like something the methodt should store rather than something every user has to pass to output()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this would be an antipattern; its not unusual to require context for output, say think of indentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is reasonable to pass this in, I also think it is reasonable for the method to know what class it belongs to FWIW.

std::ostream &out) const = 0;

membert():
is_public(false), is_protected(false),
Expand All @@ -80,6 +82,12 @@ class java_bytecode_parse_treet
{
public:
irep_idt base_name;

bool is_constructor() const
{
return base_name=="<init>";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nooo - this re-duplicates the check that <init> is the name of constructors that I've painstakingly removed once! Either leave as is of remove the is_constructor in java_bytecode_convert_method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is_constructor in java_bytecode_convert_method can be entirely removed. I'd replace the call in java_bytecode_convert_method.cpp:381 to use this new method. The other two uses java_bytecode_convert_method.cpp:575 and 1327 can be replaced by type.get_is_constructor().

}

bool is_native, is_abstract, is_synchronized;
source_locationt source_location;

Expand Down Expand Up @@ -155,7 +163,9 @@ class java_bytecode_parse_treet
typedef std::vector<stack_map_table_entryt> stack_map_tablet;
stack_map_tablet stack_map_table;

virtual void output(std::ostream &out) const;
void output(
const irep_idt &class_name,
std::ostream &out) const override;

methodt():
is_native(false),
Expand All @@ -171,7 +181,11 @@ class java_bytecode_parse_treet
{
public:
virtual ~fieldt() = default;
virtual void output(std::ostream &out) const;

void output(
const irep_idt &class_name,
std::ostream &out) const override;

bool is_enum;
};

Expand Down