Use static inner classes (#4002)

A static inner class does not keep an implicit reference to its enclosing instance.
This prevents a common cause of memory leaks and uses less memory per instance of the class.

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2024-01-03 12:35:26 +01:00
committed by GitHub
parent 10c0cf8211
commit 26a958cd4d
33 changed files with 38 additions and 38 deletions
@@ -1414,7 +1414,7 @@ public class RuleEngineImpl implements RuleManager, RegistryChangeListener<Modul
return result; return result;
} }
class OutputRef { static class OutputRef {
private final String moduleId; private final String moduleId;
private final String outputName; private final String outputName;
@@ -188,7 +188,7 @@ public class AnnotationActionModuleTypeProviderTest extends JavaTest {
} }
@ActionScope(name = "binding.test") @ActionScope(name = "binding.test")
private class TestActionProvider implements AnnotatedActions { private static class TestActionProvider implements AnnotatedActions {
@RuleAction(label = ACTION_LABEL, description = ACTION_DESCRIPTION, visibility = Visibility.HIDDEN, tags = { @RuleAction(label = ACTION_LABEL, description = ACTION_DESCRIPTION, visibility = Visibility.HIDDEN, tags = {
"tag1", "tag2" }) "tag1", "tag2" })
@@ -204,7 +204,7 @@ public class AnnotatedThingActionModuleTypeProviderTest extends JavaTest {
} }
@ThingActionsScope(name = "test") @ThingActionsScope(name = "test")
private class TestThingActionProvider implements ThingActions { private static class TestThingActionProvider implements ThingActions {
private @Nullable ThingHandler handler; private @Nullable ThingHandler handler;
@@ -250,7 +250,7 @@ public class ReferenceResolverUtilTest {
ReferenceResolver.splitReferenceToTokens("[2].value"))); ReferenceResolver.splitReferenceToTokens("[2].value")));
} }
public class B1<T> { public static class B1<T> {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final T value; private final T value;
@@ -259,7 +259,7 @@ public class ReferenceResolverUtilTest {
} }
} }
public class B2<T> { public static class B2<T> {
public T value; public T value;
public B2(T value) { public B2(T value) {
@@ -44,7 +44,7 @@ public class XmlDocumentReaderTest {
private static final String OHC_PACKAGE_PREFIX = "org.openhab.core."; private static final String OHC_PACKAGE_PREFIX = "org.openhab.core.";
private class ConfigDescriptionReader extends XmlDocumentReader<ConfigDescription> { private static class ConfigDescriptionReader extends XmlDocumentReader<ConfigDescription> {
@Override @Override
protected void registerConverters(XStream xstream) { protected void registerConverters(XStream xstream) {
} }
@@ -79,7 +79,7 @@ public class DeltaUsbSerialScanner {
/** /**
* Delta between two subsequent scan results. * Delta between two subsequent scan results.
*/ */
class Delta<T> { static class Delta<T> {
private final Set<T> added; private final Set<T> added;
private final Set<T> removed; private final Set<T> removed;
@@ -457,7 +457,7 @@ public abstract class AbstractDiscoveryService implements DiscoveryService {
/** /**
* Utility class to parse the key with parameters into the key and optional arguments. * Utility class to parse the key with parameters into the key and optional arguments.
*/ */
private final class ParsedKey { private static final class ParsedKey {
private static final int LIMIT = 2; private static final int LIMIT = 2;
@@ -452,7 +452,7 @@ public class ConfigDispatcher {
* Represents a result of parseLine(). * Represents a result of parseLine().
*/ */
@NonNullByDefault @NonNullByDefault
private class ParseLineResult { private static class ParseLineResult {
public @Nullable String pid; public @Nullable String pid;
public @Nullable String property; public @Nullable String property;
public @Nullable Object value; public @Nullable Object value;
@@ -82,7 +82,7 @@ public class LogHandler implements RESTResource {
/** /**
* Container for a log message * Container for a log message
*/ */
public class LogMessage { public static class LogMessage {
public long timestamp; public long timestamp;
public @Nullable String severity; public @Nullable String severity;
public @Nullable URL url; public @Nullable URL url;
@@ -530,7 +530,7 @@ public class SitemapResourceTest extends JavaTest {
when(sitemapProviderMock.getSitemap(SITEMAP_MODEL_NAME)).thenReturn(defaultSitemapMock); when(sitemapProviderMock.getSitemap(SITEMAP_MODEL_NAME)).thenReturn(defaultSitemapMock);
} }
private class TestItem extends GenericItem { private static class TestItem extends GenericItem {
public TestItem(String name) { public TestItem(String name) {
super("Number", name); super("Number", name);
@@ -131,7 +131,7 @@ public class JSONResponseTest {
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final class LargeEntity { private static final class LargeEntity {
private List<BigDecimal> randoms = getRandoms(); private List<BigDecimal> randoms = getRandoms();
@@ -69,7 +69,7 @@ public class Stream2JSONInputStreamTest {
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
private class DummyObject { private static class DummyObject {
private final String key; private final String key;
private final String value; private final String value;
@@ -121,7 +121,7 @@ public class MqttBrokerConnection {
* The callback will interact with the {@link AbstractReconnectStrategy} as well as inform registered * The callback will interact with the {@link AbstractReconnectStrategy} as well as inform registered
* {@link MqttConnectionObserver}s. * {@link MqttConnectionObserver}s.
*/ */
public class ConnectionCallback implements MqttClientConnectedListener, MqttClientDisconnectedListener { public static class ConnectionCallback implements MqttClientConnectedListener, MqttClientDisconnectedListener {
private final MqttBrokerConnection connection; private final MqttBrokerConnection connection;
private final Runnable cancelTimeoutFuture; private final Runnable cancelTimeoutFuture;
private CompletableFuture<Boolean> future = new CompletableFuture<>(); private CompletableFuture<Boolean> future = new CompletableFuture<>();
@@ -290,7 +290,7 @@ public class SemanticsMetadataProvider extends AbstractProvider<Metadata>
processItem(item); processItem(item);
} }
private class SemanticTagRegistryChangeListener implements RegistryChangeListener<SemanticTag> { private static class SemanticTagRegistryChangeListener implements RegistryChangeListener<SemanticTag> {
private SemanticsMetadataProvider provider; private SemanticsMetadataProvider provider;
@@ -157,7 +157,7 @@ public final class ThingStatusInfoI18nLocalizationService {
/** /**
* Utility class to parse the thing status description into the text reference and optional arguments. * Utility class to parse the thing status description into the text reference and optional arguments.
*/ */
private final class ParsedDescription { private static final class ParsedDescription {
private static final int LIMIT = 2; private static final int LIMIT = 2;
@@ -35,7 +35,7 @@ import org.openhab.core.types.StateDescriptionFragment;
public class StateChannelTypeBuilderImpl extends AbstractChannelTypeBuilder<StateChannelTypeBuilder> public class StateChannelTypeBuilderImpl extends AbstractChannelTypeBuilder<StateChannelTypeBuilder>
implements StateChannelTypeBuilder { implements StateChannelTypeBuilder {
private class StateChannelTypeImpl extends ChannelType { private static class StateChannelTypeImpl extends ChannelType {
private StateChannelTypeImpl(ChannelTypeUID uid, boolean advanced, String itemType, String label, private StateChannelTypeImpl(ChannelTypeUID uid, boolean advanced, String itemType, String label,
@Nullable String description, @Nullable String category, @Nullable Set<String> tags, @Nullable String description, @Nullable String category, @Nullable Set<String> tags,
@Nullable StateDescription state, @Nullable CommandDescription commandDescription, @Nullable StateDescription state, @Nullable CommandDescription commandDescription,
@@ -32,7 +32,7 @@ import org.openhab.core.types.EventDescription;
public class TriggerChannelTypeBuilderImpl extends AbstractChannelTypeBuilder<TriggerChannelTypeBuilder> public class TriggerChannelTypeBuilderImpl extends AbstractChannelTypeBuilder<TriggerChannelTypeBuilder>
implements TriggerChannelTypeBuilder { implements TriggerChannelTypeBuilder {
private class TriggerChannelTypeImpl extends ChannelType { private static class TriggerChannelTypeImpl extends ChannelType {
TriggerChannelTypeImpl(ChannelTypeUID uid, boolean advanced, String label, @Nullable String description, TriggerChannelTypeImpl(ChannelTypeUID uid, boolean advanced, String label, @Nullable String description,
@Nullable String category, @Nullable Set<String> tags, @Nullable EventDescription event, @Nullable String category, @Nullable Set<String> tags, @Nullable EventDescription event,
@Nullable URI configDescriptionURI) throws IllegalArgumentException { @Nullable URI configDescriptionURI) throws IllegalArgumentException {
@@ -382,7 +382,7 @@ public final class ProgressCallbackTest {
assertThat(fpiEvent.getFirmwareUpdateResultInfo().getResult(), is(expectedResult)); assertThat(fpiEvent.getFirmwareUpdateResultInfo().getResult(), is(expectedResult));
} }
class DummyFirmwareHandler implements FirmwareUpdateHandler { static class DummyFirmwareHandler implements FirmwareUpdateHandler {
@Override @Override
public Thing getThing() { public Thing getThing() {
@@ -298,7 +298,7 @@ public abstract class AbstractFileTransformationService<T> implements Transforma
return Arrays.stream(path.listFiles(new FileExtensionsFilter(validExtensions))).map(File::getName).toList(); return Arrays.stream(path.listFiles(new FileExtensionsFilter(validExtensions))).map(File::getName).toList();
} }
protected class FileExtensionsFilter implements FilenameFilter { protected static class FileExtensionsFilter implements FilenameFilter {
private final String[] validExtensions; private final String[] validExtensions;
@@ -31,7 +31,7 @@ import org.openhab.core.types.StateOption;
@NonNullByDefault @NonNullByDefault
public class StateDescriptionFragmentImpl implements StateDescriptionFragment { public class StateDescriptionFragmentImpl implements StateDescriptionFragment {
private class StateDescriptionImpl extends StateDescription { private static class StateDescriptionImpl extends StateDescription {
StateDescriptionImpl(@Nullable BigDecimal minimum, @Nullable BigDecimal maximum, @Nullable BigDecimal step, StateDescriptionImpl(@Nullable BigDecimal minimum, @Nullable BigDecimal maximum, @Nullable BigDecimal step,
@Nullable String pattern, boolean readOnly, @Nullable List<StateOption> options) { @Nullable String pattern, boolean readOnly, @Nullable List<StateOption> options) {
super(minimum, maximum, step, pattern, readOnly, options); super(minimum, maximum, step, pattern, readOnly, options);
@@ -273,7 +273,7 @@ public class ArithmeticGroupFunctionTest {
assertEquals(new DecimalType("2"), state); assertEquals(new DecimalType("2"), state);
} }
private class TestItem extends GenericItem { private static class TestItem extends GenericItem {
public TestItem(String name, State state) { public TestItem(String name, State state) {
super("Test", name); super("Test", name);
@@ -68,7 +68,7 @@ public class DateTimeGroupFunctionTest {
assertTrue(expectedDateTime.isEqual(((DateTimeType) state).getZonedDateTime())); assertTrue(expectedDateTime.isEqual(((DateTimeType) state).getZonedDateTime()));
} }
private class TestItem extends GenericItem { private static class TestItem extends GenericItem {
public TestItem(String name, State state) { public TestItem(String name, State state) {
super("Test", name); super("Test", name);
@@ -220,7 +220,7 @@ public class RuntimeRuleTest extends JavaOSGiTest {
}); });
} }
class TestItemProvider implements ItemProvider { static class TestItemProvider implements ItemProvider {
private final Collection<Item> items; private final Collection<Item> items;
TestItemProvider(Collection<Item> items) { TestItemProvider(Collection<Item> items) {
@@ -1076,7 +1076,7 @@ public class InboxOSGiTest extends JavaOSGiTest {
assertThat(future.get(), is(true)); assertThat(future.get(), is(true));
} }
class DummyThingHandlerFactory extends BaseThingHandlerFactory { static class DummyThingHandlerFactory extends BaseThingHandlerFactory {
public DummyThingHandlerFactory(ComponentContext context) { public DummyThingHandlerFactory(ComponentContext context) {
super.activate(context); super.activate(context);
@@ -88,7 +88,7 @@ public class ConfigDispatcherOSGiTest extends JavaOSGiTest {
configDispatcher = new ConfigDispatcher(configAdmin); configDispatcher = new ConfigDispatcher(configAdmin);
} }
private class CopyDirectoryRecursive extends SimpleFileVisitor<Path> { private static class CopyDirectoryRecursive extends SimpleFileVisitor<Path> {
private final Path sourceDir; private final Path sourceDir;
private final Path targetDir; private final Path targetDir;
@@ -136,7 +136,7 @@ public class BindingBaseClassesOSGiTest extends JavaOSGiTest {
managedThingProvider.getAll().forEach(t -> managedThingProvider.remove(t.getUID())); managedThingProvider.getAll().forEach(t -> managedThingProvider.remove(t.getUID()));
} }
class SimpleThingHandlerFactory extends BaseThingHandlerFactory { static class SimpleThingHandlerFactory extends BaseThingHandlerFactory {
private final Set<ThingHandler> handlers = new HashSet<>(); private final Set<ThingHandler> handlers = new HashSet<>();
@Override @Override
@@ -180,7 +180,7 @@ public class BindingBaseClassesOSGiTest extends JavaOSGiTest {
} }
} }
class SimpleBridgeHandler extends BaseBridgeHandler { static class SimpleBridgeHandler extends BaseBridgeHandler {
SimpleBridgeHandler(Bridge bridge) { SimpleBridgeHandler(Bridge bridge) {
super(bridge); super(bridge);
@@ -434,7 +434,7 @@ public class BindingBaseClassesOSGiTest extends JavaOSGiTest {
} }
} }
class YetAnotherThingHandlerFactory extends BaseThingHandlerFactory { static class YetAnotherThingHandlerFactory extends BaseThingHandlerFactory {
@Override @Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) { public boolean supportsThingType(ThingTypeUID thingTypeUID) {
@@ -40,7 +40,7 @@ import org.openhab.core.thing.type.ThingTypeBuilder;
@NonNullByDefault @NonNullByDefault
public class ThingEventOSGiTest extends JavaOSGiTest { public class ThingEventOSGiTest extends JavaOSGiTest {
class ThingEventSubscriber implements EventSubscriber { static class ThingEventSubscriber implements EventSubscriber {
private @Nullable Event lastReceivedEvent; private @Nullable Event lastReceivedEvent;
@@ -276,7 +276,7 @@ public class ThingStatusInfoI18nLocalizationServiceOSGiTest extends JavaOSGiTest
((SimpleThingHandler) thing.getHandler()).setThingStatusInfo(thingStatusInfo); ((SimpleThingHandler) thing.getHandler()).setThingStatusInfo(thingStatusInfo);
} }
private class SimpleThingHandlerFactory extends BaseThingHandlerFactory { private static class SimpleThingHandlerFactory extends BaseThingHandlerFactory {
@Override @Override
public void activate(ComponentContext componentContext) { public void activate(ComponentContext componentContext) {
@@ -294,7 +294,7 @@ public class ThingStatusInfoI18nLocalizationServiceOSGiTest extends JavaOSGiTest
} }
} }
private class SimpleThingHandler extends AbstractThingHandler { private static class SimpleThingHandler extends AbstractThingHandler {
SimpleThingHandler(Thing thing) { SimpleThingHandler(Thing thing) {
super(thing); super(thing);
@@ -316,7 +316,7 @@ public class ThingStatusInfoI18nLocalizationServiceOSGiTest extends JavaOSGiTest
} }
} }
private abstract class AbstractThingHandler extends BaseThingHandler { private abstract static class AbstractThingHandler extends BaseThingHandler {
public AbstractThingHandler(Thing thing) { public AbstractThingHandler(Thing thing) {
super(thing); super(thing);
} }
@@ -326,7 +326,7 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
} }
} }
class TestThingHandlerFactory extends BaseThingHandlerFactory { static class TestThingHandlerFactory extends BaseThingHandlerFactory {
@Override @Override
public void activate(final ComponentContext ctx) { public void activate(final ComponentContext ctx) {
@@ -451,7 +451,7 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
} }
} }
class TestThingHandlerFactory extends BaseThingHandlerFactory { static class TestThingHandlerFactory extends BaseThingHandlerFactory {
@Override @Override
public void activate(final ComponentContext ctx) { public void activate(final ComponentContext ctx) {
@@ -89,7 +89,7 @@ import org.openhab.core.types.TimeSeries;
@NonNullByDefault @NonNullByDefault
public class CommunicationManagerOSGiTest extends JavaOSGiTest { public class CommunicationManagerOSGiTest extends JavaOSGiTest {
private class ItemChannelLinkRegistryAdvanced extends ItemChannelLinkRegistry { private static class ItemChannelLinkRegistryAdvanced extends ItemChannelLinkRegistry {
public ItemChannelLinkRegistryAdvanced(ThingRegistry thingRegistry, ItemRegistry itemRegistry) { public ItemChannelLinkRegistryAdvanced(ThingRegistry thingRegistry, ItemRegistry itemRegistry) {
super(thingRegistry, itemRegistry); super(thingRegistry, itemRegistry);
} }
@@ -375,7 +375,7 @@ public class ThingUpdateOSGiTest extends JavaOSGiTest {
registerService(channelTypeRegistry); registerService(channelTypeRegistry);
} }
class TestThingHandlerFactory extends BaseThingHandlerFactory { static class TestThingHandlerFactory extends BaseThingHandlerFactory {
Logger logger = LoggerFactory.getLogger(TestThingHandlerFactory.class); Logger logger = LoggerFactory.getLogger(TestThingHandlerFactory.class);
@Override @Override
@@ -40,7 +40,7 @@ public class LinkEventOSGiTest extends JavaOSGiTest {
private @NonNullByDefault({}) ItemChannelLinkRegistry itemChannelLinkRegistry; private @NonNullByDefault({}) ItemChannelLinkRegistry itemChannelLinkRegistry;
private @NonNullByDefault({}) ItemChannelLinkEventSubscriber eventSubscriber; private @NonNullByDefault({}) ItemChannelLinkEventSubscriber eventSubscriber;
class ItemChannelLinkEventSubscriber implements EventSubscriber { static class ItemChannelLinkEventSubscriber implements EventSubscriber {
private @Nullable Event lastReceivedEvent; private @Nullable Event lastReceivedEvent;