Skip to content

Commit ee6ffa8

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 69989bb commit ee6ffa8

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_class.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,9 @@ 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)
880+
.has_value();
880881
}
881882

882883
bool java_bytecode_convert_class(

jbmc/src/java_bytecode/java_bytecode_parse_tree.cpp

+21-14
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,30 @@ void java_bytecode_parse_treet::annotationt::element_value_pairt::output(
9999
out << expr2java(value, ns);
100100
}
101101

102-
bool java_bytecode_parse_treet::does_annotation_exist(
102+
/// Find an annotation given its name
103+
/// \param annotations: A vector of annotationt
104+
/// \param annotation_type_name: An irep_idt representing the name of the
105+
/// annotation class, e.g. java::java.lang.SuppressWarnings
106+
/// \return The first annotation with the given name in annotations if one
107+
/// exists, an empty optionalt otherwise.
108+
optionalt<java_bytecode_parse_treet::annotationt>
109+
java_bytecode_parse_treet::find_annotation(
103110
const annotationst &annotations,
104111
const irep_idt &annotation_type_name)
105112
{
106-
return
107-
std::find_if(
108-
annotations.begin(),
109-
annotations.end(),
110-
[&annotation_type_name](const annotationt &annotation)
111-
{
112-
if(annotation.type.id() != ID_pointer)
113-
return false;
114-
typet type = annotation.type.subtype();
115-
return
116-
type.id() == ID_symbol
117-
&& to_symbol_type(type).get_identifier() == annotation_type_name;
118-
}) != annotations.end();
113+
const auto annotation_it = std::find_if(
114+
annotations.begin(),
115+
annotations.end(),
116+
[&annotation_type_name](const annotationt &annotation) {
117+
if(annotation.type.id() != ID_pointer)
118+
return false;
119+
const typet &type = annotation.type.subtype();
120+
return type.id() == ID_symbol &&
121+
to_symbol_type(type).get_identifier() == annotation_type_name;
122+
});
123+
if(annotation_it == annotations.end())
124+
return {};
125+
return *annotation_it;
119126
}
120127

121128
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
@@ -54,7 +54,7 @@ class java_bytecode_parse_treet
5454

5555
typedef std::vector<annotationt> annotationst;
5656

57-
static bool does_annotation_exist(
57+
static optionalt<annotationt> find_annotation(
5858
const annotationst &annotations,
5959
const irep_idt &annotation_type_name);
6060

jbmc/src/java_bytecode/java_class_loader.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar(
103103

104104
static bool is_overlay_class(const java_bytecode_parse_treet::classt &c)
105105
{
106-
return java_bytecode_parse_treet::does_annotation_exist(
107-
c.annotations, ID_overlay_class);
106+
return java_bytecode_parse_treet::find_annotation(
107+
c.annotations, ID_overlay_class)
108+
.has_value();
108109
}
109110

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

0 commit comments

Comments
 (0)