Skip to content

Commit

Permalink
Minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
Erhan Bagdemir committed Nov 29, 2024
1 parent 95ac195 commit 3b01f71
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
13 changes: 6 additions & 7 deletions src/main/java/net/reevik/mikron/ioc/ManagedInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
public class ManagedInstance {
private static final Logger LOG = LoggerFactory.getLogger(ManagedInstance.class);
private final Object instance;
private final String managedInstanceName;
private final String instanceName;
private final MikronContext context;

public ManagedInstance(Object instance,
String managedInstanceName, MikronContext context) {
public ManagedInstance(Object instance, String managedInstanceName, MikronContext context) {
this.instance = instance;
this.managedInstanceName = managedInstanceName;
this.instanceName = managedInstanceName;
this.context = context;
}

Expand All @@ -46,7 +45,7 @@ public void wire() {
}

public void configSetup() {
configSetup(managedInstanceName);
configSetup(instanceName);
}

public void configSetup(String configurationSourceKey) {
Expand Down Expand Up @@ -86,7 +85,7 @@ public Object getInstance() {
return instance;
}

public String getManagedInstanceName() {
return managedInstanceName;
public String getInstanceName() {
return instanceName;
}
}
14 changes: 7 additions & 7 deletions src/main/java/net/reevik/mikron/ioc/MikronContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void register(Object instance, String name) {
managedInstances.clear();
managedInstances.put(name, new ManagedInstance(instance, name, this));
initializeContext();
initializeWiring();
initializeConfigurations();
postConstructAll();
}
Expand All @@ -99,19 +100,20 @@ private void initializeContext() {
registerSelf();
for (var annotationResource : classpathResourceRepository.findClassesBy(Managed.class)) {
var componentName = getName(annotationResource);
var propBasedInstanceCreation = createInstancePerPropertyFile(annotationResource,
componentName);
var propBasedInstanceCreation = createInstanceByPropertyFile(annotationResource, componentName);
// It is possible that there is no configuration file at all, so we need to instantiate the
// component by name.
if (!propBasedInstanceCreation) {
managedInstances.put(componentName, initObject(annotationResource, componentName));
}
}
}

private void initializeWiring() {
managedInstances.values().forEach(ManagedInstance::wire);
}

private boolean createInstancePerPropertyFile(ManagedDefinition<Managed> annotationResource,
String componentName) {
private boolean createInstanceByPropertyFile(ManagedDefinition<Managed> annotationResource, String componentName) {
var propBasedInstanceCreation = false;
for (var propFile : propertiesRepository.getPropertyClassNames()) {
if (propFile.startsWith(componentName) && !managedInstances.containsKey(propFile)) {
Expand Down Expand Up @@ -160,9 +162,7 @@ private ManagedInstance instantiateByDefault(ManagedDefinition<Managed> annotati
try {
var clazz = annotationResource.clazz();
var constructor = clazz.getConstructor();
var managedInstance = new ManagedInstance(constructor.newInstance(), name, this);
executeIfAnnotated(Initialize.class, managedInstance);
return managedInstance;
return new ManagedInstance(constructor.newInstance(), name, this);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 3b01f71

Please sign in to comment.