acs-aem-commons
acs-aem-commons copied to clipboard
Code optimization suggestions
Required Information
- [ ] AEM Version, including Service Packs, Cumulative Fix Packs, etc: _______
- [ ] ACS AEM Commons Version: master
- [ ] Reproducible on Latest? yes/no
Hi,
I find that the private field resourceIgnores at Line 53 in the file 'acs-aem-commons/bundle/src/main/java/com/adobe/acs/commons/version/impl/EvolutionAnalyserImpl.java' on the master branch is only assigned and used in the method activate. Therefore, this field can be removed from the class, and become a local variable in the method activate. This transformation will normally reduce memory usage and improve readability of your code.
private String[] resourceIgnores; // line 53 this field can be replaced by local variable
protected void activate(final Map<String, String> config) {
propertyIgnores = PropertiesUtil.toStringArray(config.get(PROPERTY_IGNORES), new String[] { "" });
// String[] resourceIgnores = PropertiesUtil.toStringArray(config.get(RESOURCE_IGNORES), new String[] { "" });
resourceIgnores = PropertiesUtil.toStringArray(config.get(RESOURCE_IGNORES), new String[] { "" });
evolutionConfig = new EvolutionConfig(propertyIgnores, resourceIgnores);
log.debug("Ignored properties: {}", (Object[]) propertyIgnores);
log.debug("Ignored resources: {}", (Object[]) resourceIgnores);
}
Besides, there are other fields like this.I sorted them out and put them in the table below.
Location | Field |
---|---|
com.adobe.acs.commons.version.impl.EvolutionAnalyserImpl | propertyIgnores |
com.adobe.acs.commons.version.impl.EvolutionContextImpl | history |
com.adobe.acs.commons.version.impl.EvolutionContextImpl | resolver |
com.adobe.acs.commons.version.impl.EvolutionContextImpl | versionManager |
com.adobe.acs.commons.wcm.impl.AemEnvironmentIndicatorFilter | color |
com.adobe.acs.commons.wcm.impl.AemEnvironmentIndicatorFilter | cssOverride |
com.adobe.acs.commons.fam.Failure | error |
com.adobe.acs.commons.fam.Failure | stackTrace |
com.adobe.acs.commons.errorpagehandler.impl.ErrorPageHandlerImpl | fallbackErrorName |
com.adobe.acs.commons.httpcache.config.impl.HttpCacheConfigImpl | cacheInvalidationPathPatterns |
com.adobe.acs.commons.httpcache.config.impl.HttpCacheConfigImpl | blacklistedRequestUriPatterns |
com.adobe.acs.commons.httpcache.store.mem.impl.MemHttpCacheStoreImpl | maxSizeInMb |
In addition, the private field resource at Line 42 in the file 'acs-aem-commons/bundle/src/main/java/com/adobe/acs/commons/version/impl/EvolutionContextImpl.java ' on the master branch is only assigned and used in the method EvolutionContextImpl. Therefore, this field can be removed from the class, and become a local variable in the method EvolutionContextImpl. Besides, for method populateEvolutions, the field resource can be converted to an input parameter.This transformation will normally reduce memory usage and improve readability of your code.I will be happy if this transformation is helpful.
private Resource resource = null;// line 42 this field can be replaced by local variable
public EvolutionContextImpl(Resource resource, EvolutionConfig config) {
// Resource resource = resource.isResourceType("cq:Page") ? resource.getChild("jcr:content") : resource;
this.resource = resource.isResourceType("cq:Page") ? resource.getChild("jcr:content") : resource;
this.config = config;
// populateEvolutions(resource);
populateEvolutions();
}
private void populateEvolutions(Resource resource) {
try {
this.resolver = resource.getResourceResolver();
this.versionManager = resolver.adaptTo(Session.class).getWorkspace().getVersionManager();
this.history = versionManager.getVersionHistory(resource.getPath());
Iterator<Version> iter = history.getAllVersions();
while (iter.hasNext()) {
Version next = iter.next();
String versionPath = next.getFrozenNode().getPath();
Resource versionResource = resolver.resolve(versionPath);
versions.add(new EvolutionImpl(next, versionResource, config));
log.debug("Version={} added to EvolutionItem", next.getName());
}
} catch (UnsupportedRepositoryOperationException e1) {
log.warn("Could not find version for resource={}", resource.getPath());
} catch (Exception e) {
log.error("Could not find versions", e);
}
evolutionItems = new ArrayList<>(versions);
// evolutionItems.add(new CurrentEvolutionImpl(resource, this.config));
evolutionItems.add(new CurrentEvolutionImpl(this.resource, this.config));
}
Besides, there are other fields like this.I sorted them out and put them in the table below.
Location | Field |
---|---|
com.adobe.acs.commons.version.impl.EvolutionContextImpl | config |
com.adobe.acs.commons.version.impl.CurrentEvolutionImpl | config |
com.adobe.acs.commons.reports.models.ReferencesModel | resource |
com.adobe.acs.commons.util.impl.ComponentDisabler | disabledComponents |
com.adobe.acs.commons.twitter.impl.TwitterAdapterFactory | httpProxyHost |
com.adobe.acs.commons.twitter.impl.TwitterAdapterFactory | httpProxyPort |
com.adobe.acs.commons.twitter.impl.TwitterAdapterFactory | useSsl |
com.adobe.acs.commons.reports.models.ReportRunner | page |
com.adobe.acs.commons.wcm.comparisons.impl.lines.LinesGenerator | leftSpacer |
com.adobe.acs.commons.wcm.comparisons.impl.lines.LinesGenerator | left |
com.adobe.acs.commons.wcm.comparisons.impl.lines.LinesGenerator | rightSpacer |
com.adobe.acs.commons.wcm.comparisons.impl.lines.LinesGenerator | right |
Expected Behavior
Some fields can be replaced by local variable.
Actual Behavior
Use fields as local variables.
Steps to Reproduce
See attachment for details acs-aem-commons.md
Links
See attachment for details