Skip to content

Commit

Permalink
feat: improve lazy startup
Browse files Browse the repository at this point in the history
Signed-off-by: Joke de Buhr <joke@xckk.de>
  • Loading branch information
joke committed Nov 13, 2022
1 parent 52f76ac commit e77be55
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void onReset(final Instrumentation instrumentation, final ResettableClass
@Override
public void onBeforeWarmUp(final Set<Class<?>> types, final ResettableClassFileTransformer classFileTransformer) {
log.trace("onBeforeWarmUp");

super.onBeforeWarmUp(types, classFileTransformer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.joke.spockmockable.agent;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.spockframework.runtime.extension.ExtensionException;

import javax.inject.Inject;
Expand All @@ -17,8 +19,10 @@ public class PropertyReader {

private static final String METHODS_FILE = "/META-INF/spock-mockable.properties";

@SuppressWarnings("OrphanedFormatString")
protected Properties load() {
@Getter(lazy = true, onMethod_ = {@NotNull, @SuppressWarnings("NullAway")})
private final Properties properties = load();

private Properties load() {
final Properties properties = new Properties();
try (final InputStream stream = getClass().getResourceAsStream(METHODS_FILE)) {
if (stream == null) {
Expand All @@ -29,8 +33,13 @@ protected Properties load() {
properties.load(stream);
return properties;
} catch (final IOException e) {
throw new ExtensionException("Unable to read properties file '%s' containing mockable class information", e)
.withArgs(METHODS_FILE);
throw createExtensionException(e);
}
}

@SuppressWarnings("OrphanedFormatString")
private ExtensionException createExtensionException(final IOException e) {
return new ExtensionException("Unable to read properties file '%s' containing mockable class information", e)
.withArgs(METHODS_FILE);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.github.joke.spockmockable.agent;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Arrays;
import java.util.Properties;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Stream;
Expand All @@ -17,27 +18,28 @@
@Slf4j
@Getter
@Singleton
@RequiredArgsConstructor(onConstructor_ = @Inject)
public class ReferenceLoader {

private final SortedSet<String> classes;
private final SortedSet<String> packages;
private final PropertyReader propertyReader;

@Inject
public ReferenceLoader() {
final Properties properties = new PropertyReader().load();
classes = extractProperty(properties, "classes");
packages = extractProperty(properties, "packages");
@Getter(lazy = true, onMethod_ = {@NotNull, @SuppressWarnings("NullAway")})
private final SortedSet<String> classes = extractProperty("classes");

@Getter(lazy = true, onMethod_ = {@NotNull, @SuppressWarnings("NullAway")})
private final SortedSet<String> packages = extractProperty("packages");

boolean hasClasses() {
return !getPackages().isEmpty() || !getClasses().isEmpty();
}

private static TreeSet<String> extractProperty(final Properties properties, final String classes) {
return ofNullable(properties.getProperty(classes))
private TreeSet<String> extractProperty(final String propertyName) {
return ofNullable(propertyReader)
.map(PropertyReader::getProperties)
.map(properties -> properties.getProperty(propertyName))
.map(string -> string.split(","))
.map(Arrays::stream)
.orElseGet(Stream::empty)
.collect(toCollection(TreeSet::new));
}

boolean hasClasses() {
return !packages.isEmpty() || !classes.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class GlobalExtension implements IGlobalExtension {
static {
MockableController.init();
}

}

0 comments on commit e77be55

Please sign in to comment.