Skip to content

Fix mechanical CodeQL note findings: reflection, boxing, deprecated closes - #134

Open
vharseko wants to merge 1 commit into
OpenIdentityPlatform:masterfrom
vharseko:codeql-note-safe-mechanics
Open

vharseko wants to merge 1 commit into
OpenIdentityPlatform:masterfrom
vharseko:codeql-note-safe-mechanics

Conversation

@vharseko

Copy link
Copy Markdown
Member

Closes ~68 of the open note-severity CodeQL alerts (no security rating) that are safe, mechanical fixes: java/deprecated-call (Class.newInstance, SQLUtil.closeQuietly, plexus IOUtil.close), java/inefficient-boxed-constructor, java/inefficient-string-constructor, java/ignored-error-status-of-call, java/inefficient-key-set-iterator, java/inefficient-empty-string-test, java/missing-space-in-concatenation, java/unknown-javadoc-parameter.

Class.newInstance() — 20 sites, 16 files

Replaced with getDeclaredConstructor().newInstance(), which lets the constructor's own checked exceptions surface via InvocationTargetException instead of being silently mixed into InstantiationException/IllegalAccessException. Checked every site first: each is already inside a catch (Exception …)/catch (Throwable …) or declared throws Exception, so the extra checked NoSuchMethodException needs no new handling anywhere.

Boxed constructors — 19 sites

new Integer/Long/Boolean/Double/Float/Character(String)valueOf/parseX in AttributeTypeUtil, RandomGenerator, SQLUtil; new String(x)x in AttributeTypeUtil. SQLUtil.string2Timestamp/string2Date use Long.parseLong (a primitive is what the Date/Timestamp constructor needs, so no boxing round-trip at all).

Deprecated closes — 26 sites

  • SQLUtil.closeQuietly(Connection/Statement/ResultSet) (deprecated in favour of IOUtil.quietClose) is replaced at its 23 call sites, including its own 6 internal uses.
  • Found and fixed a real regression along the way: IOUtil.quietClose(Connection) did not check isClosed() before calling close(), unlike the SQLUtil.closeQuietly(Connection) it replaces. Harmless against a real JDBC driver (Connection.close() on an already-closed connection is a spec-mandated no-op), but two tests using a strict-call-sequence mock (DatabaseConnectionTest.testDispose, SQLUtilTests.quietConnectionClose) caught the difference immediately. IOUtil.quietClose(Connection) now checks isClosed() first, matching what it replaces and what its own javadoc promises.
  • Plexus IOUtil.close(Closeable) — its javadoc says "deprecated: use try-with-resources instead" — is replaced by try-with-resources at its 3 call sites in the maven plugin.

ignored-error-status-of-call — 3 sites

XMLConfiguration.validate: getParentFile().mkdir()'s return value is checked now, distinguishing "the directory already existed" from a real failure. GroovyDataProvider (×2): a createNewFile() failure now throws into the existing catch (IOException) instead of being silently swallowed and only surfacing indirectly (and confusingly) through the following canWrite() check.

Small mechanical ones

PrettyStringBuilder: map.keySet().iterator() + map.get(key)map.entrySet().iterator(). StringUtil.isEmpty and an XSD annotation check: "".equals(x) / x.equals("")x.isEmpty(). A missing space in a concatenated log message (ActiveDirectoryChangeLogSyncStrategy) and a stray comma in a @param javadoc tag (MultiOpTests) are fixed.

Tests

No new tests: these are mechanical replacements with identical behaviour (verified case by case for newInstance), except the IOUtil.quietClose(Connection) fix, which is proven by the two existing tests that caught the regression and now pass.

Local runs of all 11 touched modules, all green: connector-framework 186, dbcommon 86, connector-test-common 5, connector-framework-internal 469 (2 skipped, same as on master), connector-framework-osgi (compiles), connector-framework-contract 43, maven-plugin (compiles), connector-framework-server 29, databasetable-connector 78, ldap-connector 159 (embedded OpenDJ), xml-connector 81.

Left open on purpose

java/missing-override-annotation (1327 — mechanically safe but would touch ~200 files and conflict with the six other open CodeQL PRs), java/deprecated-call on MessagesUtil.*Legacy (67 — the framework's own deprecated API, its only path for script arguments today), java/uncaught-number-format-exception (62 — needs a per-site judgment call, not mechanical), java/unused-parameter / java/constants-only-interface / java/jdk-internal-api-access (public API or no JDK alternative), java/chained-type-tests (needs an actual refactor), java/call-to-object-tostring / java/local-shadows-field / java/confusing-method-signature (judgment calls, not mechanics).

…loses

Class.newInstance() is replaced with getDeclaredConstructor().newInstance()
at every call site (20): all are already inside a catch (Exception) or
catch (Throwable), or declared throws Exception, so the extra checked
NoSuchMethodException needs no new handling.

new Integer/Long/Boolean/Double/Float/Character(String) become valueOf/
parseX (19 sites); new String(x) becomes x.

SQLUtil.closeQuietly(Connection/Statement/ResultSet), deprecated in favour
of IOUtil.quietClose, is replaced at its 23 call sites, including its own
internal use. IOUtil.quietClose(Connection) was missing the isClosed()
check SQLUtil.closeQuietly(Connection) has, so it called close() on an
already-closed connection where the old code did not; two tests using a
strict-sequence mock caught it. IOUtil.quietClose(Connection) now checks
isClosed() first, matching what it replaces.
Plexus IOUtil.close(Closeable) ("deprecated: use try-with-resources
instead") is replaced by try-with-resources at its 3 call sites in the
maven plugin.

XMLConfiguration.validate and GroovyDataProvider no longer ignore the
return value of mkdir()/createNewFile(): a real failure now surfaces
through the existing IOException handling instead of being silently
masked by the following canWrite() check.

Small ones: PrettyStringBuilder iterates entrySet() instead of keySet()
followed by get(); StringUtil.isEmpty and an XSD annotation check use
isEmpty() instead of comparing to ""; a missing space in a concatenated
message and a stray comma in a @PARAM tag are fixed.
@vharseko vharseko added java Pull requests that update java code framework OpenICF-java-framework labels Sep 18, 2026
@vharseko vharseko added dbcommon OpenICF-dbcommon connector:ldap LDAP connector connector:xml XML connector connector:databasetable Database table connector maven-plugin OpenICF-maven-plugin refactoring Code cleanup / tech debt, no behavior change labels Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

connector:databasetable Database table connector connector:ldap LDAP connector connector:xml XML connector dbcommon OpenICF-dbcommon framework OpenICF-java-framework java Pull requests that update java code maven-plugin OpenICF-maven-plugin refactoring Code cleanup / tech debt, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant