Skip to content

Commit

Permalink
Merge pull request #5313 from basil/reduceGuava
Browse files Browse the repository at this point in the history
Use native Java functionality rather than Guava's Lists#newArrayList, Maps#newHashMap, and Sets#newHashSet where possible
  • Loading branch information
oleg-nenashev committed Mar 1, 2021
2 parents e293968 + 7fb2511 commit 76bb81d
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 29 deletions.
5 changes: 2 additions & 3 deletions core/src/main/java/hudson/ClassicPluginStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package hudson;

import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Plugin.DummyImpl;
import hudson.PluginWrapper.Dependency;
Expand Down Expand Up @@ -343,7 +342,7 @@ public <T> List<ExtensionComponent<T>> findComponents(Class<T> type, Hudson huds
finder.scout(type, hudson);
}

List<ExtensionComponent<T>> r = Lists.newArrayList();
List<ExtensionComponent<T>> r = new ArrayList<>();
for (ExtensionFinder finder : finders) {
try {
r.addAll(finder.find(type, hudson));
Expand All @@ -354,7 +353,7 @@ public <T> List<ExtensionComponent<T>> findComponents(Class<T> type, Hudson huds
}
}

List<ExtensionComponent<T>> filtered = Lists.newArrayList();
List<ExtensionComponent<T>> filtered = new ArrayList<>();
for (ExtensionComponent<T> e : r) {
if (ExtensionFilter.isAllowed(type,e))
filtered.add(e);
Expand Down
14 changes: 6 additions & 8 deletions core/src/main/java/hudson/ExtensionFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.AbstractModule;
import com.google.inject.Binding;
import com.google.inject.Guice;
Expand Down Expand Up @@ -259,7 +257,7 @@ public static class GuiceFinder extends ExtensionFinder {
/**
* Map from {@link GuiceExtensionAnnotation#annotationType} to {@link GuiceExtensionAnnotation}
*/
private Map<Class<? extends Annotation>,GuiceExtensionAnnotation<?>> extensionAnnotations = Maps.newHashMap();
private Map<Class<? extends Annotation>,GuiceExtensionAnnotation<?>> extensionAnnotations = new HashMap<>();

public GuiceFinder() {
refreshExtensionAnnotations();
Expand Down Expand Up @@ -307,7 +305,7 @@ private void refreshExtensionAnnotations() {
}

private ImmutableList<IndexItem<?, Object>> loadSezpozIndices(ClassLoader classLoader) {
List<IndexItem<?,Object>> indices = Lists.newArrayList();
List<IndexItem<?,Object>> indices = new ArrayList<>();
for (GuiceExtensionAnnotation<?> gea : extensionAnnotations.values()) {
Iterables.addAll(indices, Index.load(gea.annotationType, Object.class, classLoader));
}
Expand All @@ -330,7 +328,7 @@ public Injector getContainer() {
public synchronized ExtensionComponentSet refresh() throws ExtensionRefreshException {
refreshExtensionAnnotations();
// figure out newly discovered sezpoz components
List<IndexItem<?, Object>> delta = Lists.newArrayList();
List<IndexItem<?, Object>> delta = new ArrayList<>();
for (Class<? extends Annotation> annotationType : extensionAnnotations.keySet()) {
delta.addAll(Sezpoz.listDelta(annotationType,sezpozIndex));
}
Expand All @@ -346,7 +344,7 @@ public synchronized ExtensionComponentSet refresh() throws ExtensionRefreshExcep
try {
final Injector child = container.createChildInjector(modules);
container = child;
List<IndexItem<?, Object>> l = Lists.newArrayList(sezpozIndex);
List<IndexItem<?, Object>> l = new ArrayList<>(sezpozIndex);
l.addAll(deltaExtensions.getLoadedIndex());
sezpozIndex = l;

Expand Down Expand Up @@ -661,7 +659,7 @@ public synchronized ExtensionComponentSet refresh() {

final List<IndexItem<Extension, Object>> delta = listDelta(Extension.class,old);

List<IndexItem<Extension,Object>> r = Lists.newArrayList(old);
List<IndexItem<Extension,Object>> r = new ArrayList<>(old);
r.addAll(delta);
indices = ImmutableList.copyOf(r);

Expand All @@ -675,7 +673,7 @@ public <T> Collection<ExtensionComponent<T>> find(Class<T> type) {

static <T extends Annotation> List<IndexItem<T,Object>> listDelta(Class<T> annotationType, List<? extends IndexItem<?,Object>> old) {
// list up newly discovered components
final List<IndexItem<T,Object>> delta = Lists.newArrayList();
final List<IndexItem<T,Object>> delta = new ArrayList<>();
ClassLoader cl = Jenkins.get().getPluginManager().uberClassLoader;
for (IndexItem<T,Object> ii : Index.load(annotationType, Object.class, cl)) {
if (!old.contains(ii)) {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/ExtensionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package hudson;

import com.google.common.collect.Lists;
import hudson.init.InitMilestone;
import hudson.model.Hudson;
import jenkins.ExtensionComponentSet;
Expand Down Expand Up @@ -341,7 +340,7 @@ public void refresh(ExtensionComponentSet delta) {

Collection<ExtensionComponent<T>> found = load(delta);
if (!found.isEmpty()) {
List<ExtensionComponent<T>> l = Lists.newArrayList(extensions);
List<ExtensionComponent<T>> l = new ArrayList<>(extensions);
l.addAll(found);
extensions = sort(l);
fireOnChangeListeners = true;
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/ProxyConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package hudson;

import com.google.common.collect.Lists;
import com.thoughtworks.xstream.XStream;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
Expand All @@ -45,6 +44,7 @@
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -215,7 +215,7 @@ public String getNoProxyHost() {
public static List<Pattern> getNoProxyHostPatterns(String noProxyHost) {
if (noProxyHost==null) return Collections.emptyList();

List<Pattern> r = Lists.newArrayList();
List<Pattern> r = new ArrayList<>();
for (String s : noProxyHost.split("[ \t\n,|]+")) {
if (s.length()==0) continue;
r.add(Pattern.compile(s.replace(".", "\\.").replace("*", ".*")));
Expand Down
6 changes: 2 additions & 4 deletions core/src/main/java/hudson/model/ParametersAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.common.collect.Lists;
import static com.google.common.collect.Sets.newHashSet;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.util.SystemProperties;
Expand Down Expand Up @@ -242,8 +240,8 @@ public ParametersAction createUpdated(Collection<? extends ParameterValue> overr
parametersAction.safeParameters = this.safeParameters;
return parametersAction;
}
List<ParameterValue> combinedParameters = Lists.newArrayList(overrides);
Set<String> names = newHashSet();
List<ParameterValue> combinedParameters = new ArrayList<>(overrides);
Set<String> names = new HashSet<>();

for(ParameterValue v : overrides) {
if (v == null) continue;
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/jenkins/ExtensionComponentSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
*/
package jenkins;

import com.google.common.collect.Lists;
import hudson.ExtensionComponent;
import hudson.ExtensionFinder;
import hudson.ExtensionPoint;
import hudson.model.Descriptor;
import hudson.model.Hudson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -66,7 +66,7 @@ public final ExtensionComponentSet filtered() {
return new ExtensionComponentSet() {
@Override
public <T> Collection<ExtensionComponent<T>> find(Class<T> type) {
List<ExtensionComponent<T>> a = Lists.newArrayList();
List<ExtensionComponent<T>> a = new ArrayList<>();
for (ExtensionComponent<T> c : base.find(type)) {
if (ExtensionFilter.isAllowed(type,c))
a.add(c);
Expand All @@ -93,7 +93,7 @@ public static ExtensionComponentSet union(final Collection<? extends ExtensionCo
return new ExtensionComponentSet() {
@Override
public <T> Collection<ExtensionComponent<T>> find(Class<T> type) {
List<ExtensionComponent<T>> r = Lists.newArrayList();
List<ExtensionComponent<T>> r = new ArrayList<>();
for (ExtensionComponentSet d : base)
r.addAll(d.find(type));
return r;
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/jenkins/model/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.thoughtworks.xstream.XStream;
Expand Down Expand Up @@ -2733,14 +2732,14 @@ public void refreshExtensions() throws ExtensionRefreshException {
throw new ExtensionRefreshException(ef+" doesn't support refresh");
}

List<ExtensionComponentSet> fragments = Lists.newArrayList();
List<ExtensionComponentSet> fragments = new ArrayList<>();
for (ExtensionFinder ef : finders) {
fragments.add(ef.refresh());
}
ExtensionComponentSet delta = ExtensionComponentSet.union(fragments).filtered();

// if we find a new ExtensionFinder, we need it to list up all the extension points as well
List<ExtensionComponent<ExtensionFinder>> newFinders = Lists.newArrayList(delta.find(ExtensionFinder.class));
List<ExtensionComponent<ExtensionFinder>> newFinders = new ArrayList<>(delta.find(ExtensionFinder.class));
while (!newFinders.isEmpty()) {
ExtensionFinder f = newFinders.remove(newFinders.size()-1).getInstance();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package jenkins.scm;

import com.google.common.collect.Lists;
import hudson.DescriptorExtensionList;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import jenkins.model.Jenkins;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -34,7 +34,7 @@ public static DescriptorExtensionList<SCMCheckoutStrategy,SCMCheckoutStrategyDes
}

public static List<SCMCheckoutStrategyDescriptor> _for(AbstractProject p) {
List<SCMCheckoutStrategyDescriptor> r = Lists.newArrayList();
List<SCMCheckoutStrategyDescriptor> r = new ArrayList<>();
for (SCMCheckoutStrategyDescriptor d : all()) {
if (d.isApplicable(p))
r.add(d);
Expand Down
5 changes: 3 additions & 2 deletions core/src/test/java/hudson/EnvVarsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
import java.util.List;
import java.util.TreeMap;

import com.google.common.collect.Sets;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -141,7 +142,7 @@ public void overrideOrderCalculatorCyclic() {
OverrideOrderCalculator calc = new OverrideOrderCalculator(env, overrides);
List<String> order = calc.getOrderedVariableNames();
assertEquals(Arrays.asList("B", "A", "C"), order.subList(0, 3));
assertEquals(Sets.newHashSet("E", "D"), new HashSet<>(order.subList(3, order.size())));
assertThat(new HashSet<>(order.subList(3, order.size())), containsInAnyOrder("E", "D"));
}

@Test
Expand Down

0 comments on commit 76bb81d

Please sign in to comment.