Skip to content

Commit c3c93d9

Browse files
committed
Generalize and rename does_annotation_exist
Making the function return an optionalt instead of a boolean provides some additional functionality without adding much complexity to the existing code that calls it.
1 parent 6fea32a commit c3c93d9

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_class.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,8 @@ void java_bytecode_convert_classt::add_array_types(symbol_tablet &symbol_table)
875875

876876
bool java_bytecode_convert_classt::is_overlay_method(const methodt &method)
877877
{
878-
return java_bytecode_parse_treet::does_annotation_exist(
879-
method.annotations, ID_overlay_method);
878+
return java_bytecode_parse_treet::find_annotation(
879+
method.annotations, ID_overlay_method).has_value();
880880
}
881881

882882
bool java_bytecode_convert_class(

jbmc/src/java_bytecode/java_bytecode_parse_tree.cpp

+22-14
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,31 @@ void java_bytecode_parse_treet::annotationt::element_value_pairt::output(
121121
out << expr2java(value, ns);
122122
}
123123

124-
bool java_bytecode_parse_treet::does_annotation_exist(
124+
/// Find an annotation given its name
125+
/// \param annotations: A vector of annotationt
126+
/// \param annotation_type_name: An irep_idt representing the name of the
127+
/// annotation class, e.g. java::java.lang.SuppressWarnings
128+
/// \return The first annotation with the given name in annotations if one
129+
/// exists, an empty optionalt otherwise.
130+
optionalt<java_bytecode_parse_treet::annotationt>
131+
java_bytecode_parse_treet::find_annotation(
125132
const annotationst &annotations,
126133
const irep_idt &annotation_type_name)
127134
{
128-
return
129-
std::find_if(
130-
annotations.begin(),
131-
annotations.end(),
132-
[&annotation_type_name](const annotationt &annotation)
133-
{
134-
if(annotation.type.id() != ID_pointer)
135-
return false;
136-
typet type = annotation.type.subtype();
137-
return
138-
type.id() == ID_symbol
139-
&& to_symbol_type(type).get_identifier() == annotation_type_name;
140-
}) != annotations.end();
135+
const auto annotation_it = std::find_if(
136+
annotations.begin(),
137+
annotations.end(),
138+
[&annotation_type_name](const annotationt &annotation)
139+
{
140+
if(annotation.type.id() != ID_pointer)
141+
return false;
142+
const typet &type = annotation.type.subtype();
143+
return type.id() == ID_symbol &&
144+
to_symbol_type(type).get_identifier() == annotation_type_name;
145+
});
146+
if(annotation_it == annotations.end())
147+
return {};
148+
return *annotation_it;
141149
}
142150

143151
void java_bytecode_parse_treet::methodt::output(std::ostream &out) const

jbmc/src/java_bytecode/java_bytecode_parse_tree.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class java_bytecode_parse_treet
4444

4545
typedef std::vector<annotationt> annotationst;
4646

47-
static bool does_annotation_exist(
47+
static optionalt<annotationt> find_annotation(
4848
const annotationst &annotations,
4949
const irep_idt &annotation_type_name);
5050

jbmc/src/java_bytecode/java_class_loader.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar(
106106

107107
static bool is_overlay_class(const java_bytecode_parse_treet::classt &c)
108108
{
109-
return java_bytecode_parse_treet::does_annotation_exist(
110-
c.annotations, ID_overlay_class);
109+
return java_bytecode_parse_treet::find_annotation(
110+
c.annotations, ID_overlay_class).has_value();
111111
}
112112

113113
/// Check through all the places class parse trees can appear and returns the

0 commit comments

Comments
 (0)