Skip to content

Commit

Permalink
[JENKINS-49839] correct annotation check (#3321)
Browse files Browse the repository at this point in the history
- also correct a bad annotation in Run
  • Loading branch information
Wadeck authored and daniel-beck committed Apr 28, 2018
1 parent c7bc103 commit 79ec930
Show file tree
Hide file tree
Showing 27 changed files with 91 additions and 69 deletions.
18 changes: 16 additions & 2 deletions core/src/main/java/hudson/ExtensionList.java
Expand Up @@ -145,15 +145,29 @@ public void addListener(@Nonnull ExtensionListListener listener) {
* Looks for the extension instance of the given type (subclasses excluded),
* or return null.
*/
public @CheckForNull <U extends T> U get(Class<U> type) {
public @CheckForNull <U extends T> U get(@Nonnull Class<U> type) {
for (T ext : this)
if(ext.getClass()==type)
return type.cast(ext);
return null;
}

/**
* Looks for the extension instance of the given type (subclasses excluded),
* or throws an IllegalStateException.
*
* Meant to simplify call inside @Extension annotated class to retrieve their own instance.
*/
public @Nonnull <U extends T> U getInstance(@Nonnull Class<U> type) throws IllegalStateException {
for (T ext : this)
if(ext.getClass()==type)
return type.cast(ext);

throw new IllegalStateException("The class " + type.getName() + " was not found, potentially not yet loaded");
}

@Override
public Iterator<T> iterator() {
public @Nonnull Iterator<T> iterator() {
// we need to intercept mutation, so for now don't allow Iterator.remove
return new AdaptedIterator<ExtensionComponent<T>,T>(Iterators.readOnly(ensureLoaded().iterator())) {
protected T adapt(ExtensionComponent<T> item) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/Descriptor.java
Expand Up @@ -821,7 +821,7 @@ public String getGlobalConfigPage() {
*
* @since 2.0, used to be in {@link GlobalConfiguration} before that.
*/
public GlobalConfigurationCategory getCategory() {
public @Nonnull GlobalConfigurationCategory getCategory() {
return GlobalConfigurationCategory.get(GlobalConfigurationCategory.Unclassified.class);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/Run.java
Expand Up @@ -1133,7 +1133,7 @@ public SerializableArtifactList call() throws IOException {

private static int addArtifacts(@Nonnull VirtualFile dir,
@Nonnull String path, @Nonnull String pathHref,
@Nonnull SerializableArtifactList r, @Nonnull SerializableArtifact parent, int upTo) throws IOException {
@Nonnull SerializableArtifactList r, @CheckForNull SerializableArtifact parent, int upTo) throws IOException {
VirtualFile[] kids = dir.list();
Arrays.sort(kids);

Expand Down
Expand Up @@ -31,6 +31,8 @@
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.StaplerRequest;

import javax.annotation.Nonnull;

/**
* Show the crumb configuration to the system config page.
*
Expand All @@ -39,14 +41,14 @@
@Extension(ordinal=195) @Symbol("crumb") // immediately after the security setting
public class GlobalCrumbIssuerConfiguration extends GlobalConfiguration {
@Override
public GlobalConfigurationCategory getCategory() {
public @Nonnull GlobalConfigurationCategory getCategory() {
return GlobalConfigurationCategory.get(GlobalConfigurationCategory.Security.class);
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
// for compatibility reasons, the actual value is stored in Jenkins
Jenkins j = Jenkins.getInstance();
Jenkins j = Jenkins.get();
if (json.has("csrf")) {
JSONObject csrf = json.getJSONObject("csrf");
j.setCrumbIssuer(CrumbIssuer.all().newInstanceFromRadioList(csrf, "issuer"));
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/hudson/tools/ToolDescriptor.java
Expand Up @@ -44,6 +44,8 @@
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import javax.annotation.Nonnull;

/**
* {@link Descriptor} for {@link ToolInstallation}.
*
Expand Down Expand Up @@ -109,12 +111,12 @@ public void setInstallations(T... installations) {
* Lists up {@link ToolPropertyDescriptor}s that are applicable to this {@link ToolInstallation}.
*/
public List<ToolPropertyDescriptor> getPropertyDescriptors() {
return PropertyDescriptor.<ToolPropertyDescriptor, ToolInstallation>for_(ToolProperty.all(), clazz);
return PropertyDescriptor.for_(ToolProperty.all(), clazz);
}


@Override
public GlobalConfigurationCategory getCategory() {
public @Nonnull GlobalConfigurationCategory getCategory() {
return GlobalConfigurationCategory.get(ToolConfigurationCategory.class);
}

Expand Down
Expand Up @@ -41,7 +41,7 @@ public class GlobalDefaultViewConfiguration extends GlobalConfiguration {
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
// for compatibility reasons, the actual value is stored in Jenkins
Jenkins j = Jenkins.getInstance();
Jenkins j = Jenkins.get();
if (json.has("primaryView")) {
final String viewName = json.getString("primaryView");
final View newPrimaryView = j.getView(viewName);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/views/MyViewsTabBar.java
Expand Up @@ -100,13 +100,13 @@ public int compare(View o1, View o2) {
@Extension(ordinal=305) @Symbol("myView")
public static class GlobalConfigurationImpl extends GlobalConfiguration {
public MyViewsTabBar getMyViewsTabBar() {
return Jenkins.getInstance().getMyViewsTabBar();
return Jenkins.get().getMyViewsTabBar();
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
// for compatibility reasons, the actual value is stored in Jenkins
Jenkins j = Jenkins.getInstance();
Jenkins j = Jenkins.get();

if (json.has("myViewsTabBar")) {
j.setMyViewsTabBar(req.bindJSON(MyViewsTabBar.class,json.getJSONObject("myViewsTabBar")));
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/views/ViewsTabBar.java
Expand Up @@ -102,13 +102,13 @@ public int compare(View o1, View o2) {
@Extension(ordinal=310) @Symbol("viewsTabBar")
public static class GlobalConfigurationImpl extends GlobalConfiguration {
public ViewsTabBar getViewsTabBar() {
return Jenkins.getInstance().getViewsTabBar();
return Jenkins.get().getViewsTabBar();
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
// for compatibility reasons, the actual value is stored in Jenkins
Jenkins j = Jenkins.getInstance();
Jenkins j = Jenkins.get();

if (json.has("viewsTabBar")) {
j.setViewsTabBar(req.bindJSON(ViewsTabBar.class,json.getJSONObject("viewsTabBar")));
Expand Down
9 changes: 2 additions & 7 deletions core/src/main/java/jenkins/CLI.java
Expand Up @@ -34,12 +34,7 @@ public class CLI extends GlobalConfiguration {

@Nonnull
public static CLI get() {
CLI instance = GlobalConfiguration.all().get(CLI.class);
if (instance == null) {
// should not happen
return new CLI();
}
return instance;
return GlobalConfiguration.all().getInstance(CLI.class);
}

private boolean enabled = true; // historical default, but overridden in SetupWizard
Expand All @@ -49,7 +44,7 @@ public CLI() {
}

@Override
public GlobalConfigurationCategory getCategory() {
public @Nonnull GlobalConfigurationCategory getCategory() {
return GlobalConfigurationCategory.get(GlobalConfigurationCategory.Security.class);
}

Expand Down
Expand Up @@ -31,15 +31,17 @@
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.StaplerRequest;

import javax.annotation.Nonnull;

/**
* List of configured {@link ArtifactManagerFactory}s.
* @since 1.532
*/
@Extension @Symbol("artifactManager")
public class ArtifactManagerConfiguration extends GlobalConfiguration {

public static ArtifactManagerConfiguration get() {
return Jenkins.getInstance().getInjector().getInstance(ArtifactManagerConfiguration.class);
public static @Nonnull ArtifactManagerConfiguration get() {
return GlobalConfiguration.all().getInstance(ArtifactManagerConfiguration.class);
}

private final DescribableList<ArtifactManagerFactory,ArtifactManagerFactoryDescriptor> artifactManagerFactories = new DescribableList<ArtifactManagerFactory,ArtifactManagerFactoryDescriptor>(this);
Expand Down
16 changes: 9 additions & 7 deletions core/src/main/java/jenkins/model/DownloadSettings.java
Expand Up @@ -42,6 +42,8 @@
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpResponse;

import javax.annotation.Nonnull;

/**
* Lets user configure how metadata files should be downloaded.
* @see UpdateSite
Expand All @@ -51,8 +53,8 @@
@Extension @Symbol("downloadSettings")
public final class DownloadSettings extends GlobalConfiguration {

public static DownloadSettings get() {
return Jenkins.getInstance().getInjector().getInstance(DownloadSettings.class);
public static @Nonnull DownloadSettings get() {
return GlobalConfiguration.all().getInstance(DownloadSettings.class);
}

private boolean useBrowser = false;
Expand All @@ -70,19 +72,19 @@ public void setUseBrowser(boolean useBrowser) {
save();
}

@Override public GlobalConfigurationCategory getCategory() {
@Override public @Nonnull GlobalConfigurationCategory getCategory() {
return GlobalConfigurationCategory.get(GlobalConfigurationCategory.Security.class);
}

public static boolean usePostBack() {
return get().isUseBrowser() && Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER);
return get().isUseBrowser() && Jenkins.get().hasPermission(Jenkins.ADMINISTER);
}

public static void checkPostBackAccess() throws AccessDeniedException {
if (!get().isUseBrowser()) {
throw new AccessDeniedException("browser-based download disabled");
}
Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
}

@Extension @Symbol("updateCenterCheck")
Expand All @@ -106,7 +108,7 @@ public DailyCheck() {
return;
}
boolean due = false;
for (UpdateSite site : Jenkins.getInstance().getUpdateCenter().getSites()) {
for (UpdateSite site : Jenkins.get().getUpdateCenter().getSites()) {
if (site.isDue()) {
due = true;
break;
Expand All @@ -128,7 +130,7 @@ public DailyCheck() {
return;
}
// This checks updates of the update sites and downloadables.
HttpResponse rsp = Jenkins.getInstance().getPluginManager().doCheckUpdatesServer();
HttpResponse rsp = Jenkins.get().getPluginManager().doCheckUpdatesServer();
if (rsp instanceof FormValidation) {
listener.error(((FormValidation) rsp).renderHtml());
}
Expand Down
Expand Up @@ -21,7 +21,7 @@ public class GlobalCloudConfiguration extends GlobalConfiguration {
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
try {
Jenkins.getInstance().clouds.rebuildHetero(req,json, Cloud.all(), "cloud");
Jenkins.get().clouds.rebuildHetero(req,json, Cloud.all(), "cloud");
return true;
} catch (IOException e) {
throw new FormException(e,"clouds");
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/jenkins/model/GlobalConfiguration.java
Expand Up @@ -7,6 +7,8 @@
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;

import javax.annotation.Nonnull;

/**
* Convenient base class for extensions that contributes to the system configuration page but nothing
* else, or to manage the global configuration of a plugin implementing several extension points.
Expand Down Expand Up @@ -69,8 +71,8 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
/**
* Returns all the registered {@link GlobalConfiguration} descriptors.
*/
public static ExtensionList<GlobalConfiguration> all() {
return Jenkins.getInstance().<GlobalConfiguration,GlobalConfiguration>getDescriptorList(GlobalConfiguration.class);
public static @Nonnull ExtensionList<GlobalConfiguration> all() {
return Jenkins.get().getDescriptorList(GlobalConfiguration.class);
// pointless type parameters help work around bugs in javac in earlier versions http://codepad.org/m1bbFRrH
}
}
Expand Up @@ -4,10 +4,10 @@
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.ModelObject;
import hudson.security.*;
import hudson.security.Messages;
import org.jenkinsci.Symbol;

import javax.annotation.Nonnull;

/**
* Grouping of related {@link GlobalConfiguration}s.
*
Expand Down Expand Up @@ -41,8 +41,12 @@ public static ExtensionList<GlobalConfigurationCategory> all() {
return ExtensionList.lookup(GlobalConfigurationCategory.class);
}

public static <T extends GlobalConfigurationCategory> T get(Class<T> type) {
return all().get(type);
public static @Nonnull <T extends GlobalConfigurationCategory> T get(Class<T> type) {
T category = all().get(type);
if(category == null){
throw new AssertionError("Category not found. It seems the " + type + " is not annotated with @Extension and so not registered");
}
return category;
}

/**
Expand Down
Expand Up @@ -19,7 +19,7 @@ public class GlobalNodePropertiesConfiguration extends GlobalConfiguration {
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
try {
Jenkins j = Jenkins.getInstance();
Jenkins j = Jenkins.get();
JSONObject np = json.getJSONObject("globalNodeProperties");
if (!np.isNullObject()) {
j.getGlobalNodeProperties().rebuild(req, np, NodeProperty.for_(j));
Expand Down
Expand Up @@ -24,7 +24,7 @@ public class GlobalPluginConfiguration extends GlobalConfiguration {
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
try {
for( JSONObject o : StructuredForm.toList(json, "plugin"))
Jenkins.getInstance().pluginManager.getPlugin(o.getString("name")).getPlugin().configure(req, o);
Jenkins.get().pluginManager.getPlugin(o.getString("name")).getPlugin().configure(req, o);
return true;
} catch (IOException | ServletException e) {
throw new FormException(e,"plugin");
Expand Down
Expand Up @@ -41,13 +41,13 @@ public class GlobalProjectNamingStrategyConfiguration extends GlobalConfiguratio
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws hudson.model.Descriptor.FormException {
// for compatibility reasons, the actual value is stored in Jenkins
Jenkins j = Jenkins.getInstance();
Jenkins j = Jenkins.get();
final JSONObject optJSONObject = json.optJSONObject("useProjectNamingStrategy");
if (optJSONObject != null) {
final JSONObject strategyObject = optJSONObject.getJSONObject("namingStrategy");
final String className = strategyObject.getString("$class");
try {
Class clazz = Class.forName(className, true, Jenkins.getInstance().getPluginManager().uberClassLoader);
Class clazz = Class.forName(className, true, j.getPluginManager().uberClassLoader);
final ProjectNamingStrategy strategy = (ProjectNamingStrategy) req.bindJSON(clazz, strategyObject);
j.setProjectNamingStrategy(strategy);
} catch (ClassNotFoundException e) {
Expand Down
Expand Up @@ -38,7 +38,7 @@
@Extension(ordinal=400) @Symbol("quietPeriod")
public class GlobalQuietPeriodConfiguration extends GlobalConfiguration {
public int getQuietPeriod() {
return Jenkins.getInstance().getQuietPeriod();
return Jenkins.get().getQuietPeriod();
}

@Override
Expand All @@ -51,7 +51,7 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
}
try {
// for compatibility reasons, this value is stored in Jenkins
Jenkins.getInstance().setQuietPeriod(i);
Jenkins.get().setQuietPeriod(i);
return true;
} catch (IOException e) {
throw new FormException(e,"quietPeriod");
Expand Down
Expand Up @@ -39,14 +39,14 @@
@Extension(ordinal=395) @Symbol("scmRetryCount")
public class GlobalSCMRetryCountConfiguration extends GlobalConfiguration {
public int getScmCheckoutRetryCount() {
return Jenkins.getInstance().getScmCheckoutRetryCount();
return Jenkins.get().getScmCheckoutRetryCount();
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
try {
// for compatibility reasons, this value is stored in Jenkins
Jenkins.getInstance().setScmCheckoutRetryCount(json.getInt("scmCheckoutRetryCount"));
Jenkins.get().setScmCheckoutRetryCount(json.getInt("scmCheckoutRetryCount"));
return true;
} catch (IOException e) {
throw new FormException(e,"quietPeriod");
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -2539,7 +2539,7 @@ public Lifecycle getLifecycle() {
*
* @since 1.433
*/
public Injector getInjector() {
public @CheckForNull Injector getInjector() {
return lookup(Injector.class);
}

Expand Down Expand Up @@ -2576,7 +2576,7 @@ public ExtensionList getExtensionList(String extensionType) throws ClassNotFound
* Can be an empty list but never null.
*/
@SuppressWarnings({"unchecked"})
public <T extends Describable<T>,D extends Descriptor<T>> DescriptorExtensionList<T,D> getDescriptorList(Class<T> type) {
public @Nonnull <T extends Describable<T>,D extends Descriptor<T>> DescriptorExtensionList<T,D> getDescriptorList(Class<T> type) {
return descriptorLists.computeIfAbsent(type, key -> DescriptorExtensionList.createDescriptorList(this, key));
}

Expand Down

0 comments on commit 79ec930

Please sign in to comment.