Skip to content

Commit

Permalink
Update Iterator hasNext in ServiceLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
taoliult committed Nov 1, 2022
1 parent db8d413 commit 50fe664
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions src/java.base/share/classes/java/util/ServiceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -726,33 +726,9 @@ public Class<? extends S> type() {
@Override
public S get() {
if (factoryMethod != null) {
// If the specified class extends java.security.Provider
// && the restricted security mode is enabled.
if (java.security.Provider.class
.isAssignableFrom(factoryMethod.getDeclaringClass())
&& RestrictedSecurityConfigurator.isEnabled()) {
// load provider if it is allowed in restricted security mode.
if (RestrictedSecurityProperties.getInstance()
.isProviderAllowed(factoryMethod.getDeclaringClass().getName())) {
return invokeFactoryMethod();
}
// Do nothing (return provider = null) if it is not allowed.
return null;
} else {
return invokeFactoryMethod();
}
return invokeFactoryMethod();
} else {
if (java.security.Provider.class
.isAssignableFrom(ctor.getDeclaringClass())
&& RestrictedSecurityConfigurator.isEnabled()) {
if (RestrictedSecurityProperties.getInstance()
.isProviderAllowed(ctor.getDeclaringClass().getName())) {
return newInstance();
}
return null;
} else {
return newInstance();
}
return newInstance();
}
}

Expand Down Expand Up @@ -917,6 +893,16 @@ private Provider<S> loadProvider(ServiceProvider provider) {

@SuppressWarnings("unchecked")
Class<? extends S> type = (Class<? extends S>) returnType;
// If the restricted security mode is enabled
// && the specified class extends java.security.Provider
// && the provider is NOT allowed in restricted security mode.
if (RestrictedSecurityConfigurator.isEnabled()
&& java.security.Provider.class.isAssignableFrom(clazz)
&& !RestrictedSecurityProperties.getInstance()
.isProviderAllowed(clazz.getName())) {
// Then skip it.
return null;
}
return new ProviderImpl<S>(service, type, factoryMethod, acc);
}
}
Expand All @@ -929,7 +915,17 @@ private Provider<S> loadProvider(ServiceProvider provider) {
@SuppressWarnings("unchecked")
Class<? extends S> type = (Class<? extends S>) clazz;
@SuppressWarnings("unchecked")
Constructor<? extends S> ctor = (Constructor<? extends S> ) getConstructor(clazz);
Constructor<? extends S> ctor = (Constructor<? extends S>) getConstructor(clazz);
// If the restricted security mode is enabled
// && the specified class extends java.security.Provider
// && the provider is NOT allowed in restricted security mode.
if (RestrictedSecurityConfigurator.isEnabled()
&& java.security.Provider.class.isAssignableFrom(clazz)
&& !RestrictedSecurityProperties.getInstance()
.isProviderAllowed(clazz.getName())) {
// Then skip it.
return null;
}
return new ProviderImpl<S>(service, type, ctor, acc);
}

Expand Down Expand Up @@ -1264,6 +1260,16 @@ private boolean hasNextService() {
Class<? extends S> type = (Class<? extends S>) clazz;
Constructor<? extends S> ctor
= (Constructor<? extends S>)getConstructor(clazz);
// If the restricted security mode is enabled
// && the specified class extends java.security.Provider
// && the provider is NOT allowed in restricted security mode.
if (RestrictedSecurityConfigurator.isEnabled()
&& java.security.Provider.class.isAssignableFrom(clazz)
&& !RestrictedSecurityProperties.getInstance()
.isProviderAllowed(clazz.getName())) {
// Then skip it.
continue;
}
ProviderImpl<S> p = new ProviderImpl<S>(service, type, ctor, acc);
nextProvider = (ProviderImpl<T>) p;
} else {
Expand Down

0 comments on commit 50fe664

Please sign in to comment.