6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /**
25 * @test
26 * @library /test/lib
27 * @modules jdk.compiler
28 * @build jdk.test.lib.compiler.CompilerUtils
29 * @run testng/othervm BadProvidersTest
30 * @summary Basic test of ServiceLoader with bad provider and bad provider
31 * factories deployed on the module path
32 */
33
34 import java.lang.classfile.ClassFile;
35 import java.lang.constant.ClassDesc;
36 import java.lang.constant.MethodTypeDesc;
37 import java.lang.module.Configuration;
38 import java.lang.module.ModuleFinder;
39 import java.lang.reflect.AccessFlag;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.nio.file.StandardCopyOption;
44 import java.util.List;
45 import java.util.ServiceConfigurationError;
198 Path classFile = Paths.get("p", "Provider.class");
199 Files.copy(output.resolve(classFile),
200 mods.resolve(TEST2_MODULE).resolve(classFile),
201 StandardCopyOption.REPLACE_EXISTING);
202
203 // load providers and instantiate each one
204 loadProviders(mods, TEST2_MODULE).forEach(Provider::get);
205 }
206
207
208 /**
209 * Test a service provider that defines more than one no-args
210 * public static "provider" method.
211 */
212 @Test(expectedExceptions = ServiceConfigurationError.class)
213 public void testWithTwoFactoryMethods() throws Exception {
214 Path mods = compileTest(TEST1_MODULE);
215
216 var bytes = ClassFile.of().build(ClassDesc.of("p", "ProviderFactory"), clb -> {
217 clb.withSuperclass(CD_Object);
218 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
219
220 var providerFactory$1 = ClassDesc.of("p", "ProviderFactory$1");
221
222 // public static p.Service provider()
223 clb.withMethodBody("provider", MethodTypeDesc.of(ClassDesc.of("p", "Service")),
224 ACC_PUBLIC | ACC_STATIC, cob -> {
225 cob.new_(providerFactory$1);
226 cob.dup();
227 cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
228 cob.areturn();
229 });
230
231 // public static p.ProviderFactory$1 provider()
232 clb.withMethodBody("provider", MethodTypeDesc.of(providerFactory$1),
233 ACC_PUBLIC | ACC_STATIC, cob -> {
234 cob.new_(providerFactory$1);
235 cob.dup();
236 cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
237 cob.areturn();
238 });
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /**
25 * @test
26 * @enablePreview
27 * @library /test/lib
28 * @modules jdk.compiler
29 * @build jdk.test.lib.compiler.CompilerUtils
30 * @run testng/othervm BadProvidersTest
31 * @summary Basic test of ServiceLoader with bad provider and bad provider
32 * factories deployed on the module path
33 */
34
35 import java.lang.classfile.ClassFile;
36 import java.lang.constant.ClassDesc;
37 import java.lang.constant.MethodTypeDesc;
38 import java.lang.module.Configuration;
39 import java.lang.module.ModuleFinder;
40 import java.lang.reflect.AccessFlag;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.nio.file.StandardCopyOption;
45 import java.util.List;
46 import java.util.ServiceConfigurationError;
199 Path classFile = Paths.get("p", "Provider.class");
200 Files.copy(output.resolve(classFile),
201 mods.resolve(TEST2_MODULE).resolve(classFile),
202 StandardCopyOption.REPLACE_EXISTING);
203
204 // load providers and instantiate each one
205 loadProviders(mods, TEST2_MODULE).forEach(Provider::get);
206 }
207
208
209 /**
210 * Test a service provider that defines more than one no-args
211 * public static "provider" method.
212 */
213 @Test(expectedExceptions = ServiceConfigurationError.class)
214 public void testWithTwoFactoryMethods() throws Exception {
215 Path mods = compileTest(TEST1_MODULE);
216
217 var bytes = ClassFile.of().build(ClassDesc.of("p", "ProviderFactory"), clb -> {
218 clb.withSuperclass(CD_Object);
219 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
220
221 var providerFactory$1 = ClassDesc.of("p", "ProviderFactory$1");
222
223 // public static p.Service provider()
224 clb.withMethodBody("provider", MethodTypeDesc.of(ClassDesc.of("p", "Service")),
225 ACC_PUBLIC | ACC_STATIC, cob -> {
226 cob.new_(providerFactory$1);
227 cob.dup();
228 cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
229 cob.areturn();
230 });
231
232 // public static p.ProviderFactory$1 provider()
233 clb.withMethodBody("provider", MethodTypeDesc.of(providerFactory$1),
234 ACC_PUBLIC | ACC_STATIC, cob -> {
235 cob.new_(providerFactory$1);
236 cob.dup();
237 cob.invokespecial(providerFactory$1, INIT_NAME, MTD_void);
238 cob.areturn();
239 });
|