Skip to content

Commit

Permalink
Add Java IR.findModules(String name) API
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjseitz committed Nov 14, 2023
1 parent cbeab51 commit 7ef7193
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* More extensive test cases have been added.
* Add elfStackExec and elfStackSize AuxData definitions
* Add `IR.modules_named` helper method to Python API.
* Add `IR.findModules(String name)` helper method to Java API.

# 1.12.0

Expand Down
16 changes: 16 additions & 0 deletions java/com/grammatech/gtirb/IR.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ public List<Module> getModules() {
return Collections.unmodifiableList(this.modules);
}

/**
* Find modules by name.
*
* @return A list of all {@link Module} in this {@link IR} that have a
* matching name.
*/
public List<Module> findModules(String name) {
List<Module> modulesNamed = new ArrayList<Module>();
for (Module module : this.modules) {
if (name.equals(module.getName())) {
modulesNamed.add(module);
}
}
return modulesNamed;
}

/**
* Add a module to this {@link IR}.
*
Expand Down
31 changes: 31 additions & 0 deletions java/tests/TestIrSanity.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,35 @@ void testAddAndRemoveModules() throws Exception {
assertTrue(mod0.getIr().isEmpty());
assertTrue(mod2.getIr().isEmpty());
}

@Test
void testIrFindModules() throws Exception {
IR ir = new IR();

// test addModules (list)
List<Module> modules = new ArrayList<Module>();
modules.add(new Module(
"/opt/testModules/testModules-1.0.0/testModule0/bin/mod",
0x8FFFFFFF00000201L, 0x0L, FileFormat.ELF, ISA.X64, "mod0"));
modules.add(new Module(
"/opt/testModules/testModules-1.0.0/testModule1/bin/mod",
0x8FFFFFFF00000401L, 0x0L, FileFormat.ELF, ISA.X64, "mod1"));
modules.add(new Module(
"/opt/testModules/testModules-1.0.0/testModule1/bin/mod-dup",
0x8FFFFFFF00000401L, 0x0L, FileFormat.ELF, ISA.X64, "mod1"));
ir.addModules(modules);
assertTrue(ir.getModules().equals(modules));

List<Module> mod0_modules = ir.findModules("mod0");
assertEquals(1, mod0_modules.size());
for (Module module : mod0_modules) {
assertEquals("mod0", module.getName());
}

List<Module> mod1_modules = ir.findModules("mod1");
assertEquals(2, mod1_modules.size());
for (Module module : mod1_modules) {
assertEquals("mod1", module.getName());
}
}
}

0 comments on commit 7ef7193

Please sign in to comment.