Modules Flashcards

1
Q

What types of modules are there?

A

Named, unnamed, automatic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are named modules?

A

They have module-info.java file.
They are on modules path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are unnamed modules?

A

They are on classpath.
They ignore module-info.java.
Modules cannot import them, they don’t export anything.
They can read everything from classpath and other modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are automatic modules?

A

They don’t have module-info.java file.
They are on modules path.
Name of the module is determined by Java based on the name of the module’s jar file.
All packages are public.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is typical workflow of defining modules?

A

Definition
Compiling
Running or packing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are modules defined?

A

They are based on file structure. Inside src folder we define a folder and name it something. It can be package. Inside that folder or package we create “module-info.java” file. Everything that is inside that folder is now module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Do modules need to have main method?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can module-info.java file be empty?

A

No, it must at least have definition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we define module in module-info.java

A

module full.name.of.the.module {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are modules compiled?

A

javac -d out –module-source-path ./path/to/FirstClass.java ./path/to/SecondClass.java ./path/to/module-info.java

javac -d out -p ./path/to/FirstClass.java ./path/to/SecondClass.java ./path/to/module-info.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do we run module?

A

It must have main class if we want to run it separately.

java –module-path out –module module.name/path.to.Main
java –p out -m module.name/path.to.Main

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we pack module?

A

jar -cvf /path/to/module.name.jar -C out/module.name .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Jmod?

A

Command to work with jmod files.
It is made for native libs and for things that don’t go into jars.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is jlink?

A

It is a tool that creates smallest possible runnable module, leaving only dependencies module needs to run.

jdeps -summary module.name.jar
jdeps –jdk-internals module.name.jar
jdeps module.name.jar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do we list available modules?

A

java -p mods –list-modules
java –list-modules

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do we show module resolution?

A

Showing resolution means to get information what modules are needed or used with some module. We do it as a part of module startup.

java –show-module-resolution -p path/to/module -m module.name/path.to.Main

17
Q

How do we describe module with CLI?

A

java -p path/to/module –describe-module module.name
jar –file path/to/module.jar –describe-module

18
Q

How can we do migration to modules?

A

Bottom-up method
Top-down method
Splitting big projects into module

19
Q

What is Bottom-up method of migration?

A

Process where:
- main project stays as unnamed module until all other dependencies become module
- dependencies are converted by adding module-info.java

20
Q

What is Top-down method of migration?

A

Process where:
- main project is put onto module path and becomes auto. module, and then it is turned into named module
- dependencies are turned into auto. modules and then to named modules

21
Q

Splitting big projects into module?

A

We split packages into groups and then we determine relationships

22
Q

How do we implement services trough modules?

A
  1. Defining service interface
  2. Defining service provider
  3. Defining service locator
  4. Defining service consumer
23
Q

How do we define service interface in modules?

A

We create module interface with interface methods in certain package.
We define module file in that package, which definition must contain:
“exports full.package.path”

24
Q

How do we define service provider?

A

Service provider implements service interface.
We create class in certain package, where we implement service interface defined before.
We create module file in that package, where we must define:
requires full.path.to.interface.module
provides full.path.to.interface.module.InterfaceName with full.path.to.interface.implementation.ClassName

25
Q

How do we define service locator?

A

We create class in certain package, where we use ServiceLoader.load() or ServiceLoader.stream():

public void useService() {
    ServiceLoader<MyService> loader = ServiceLoader.load(MyService.class);
    for (MyService service : loader) {
        service.doSomething();
    }
}

In same package we define module file, where we must define:
requires full.path.to.interface.implementation.ClassName
uses full.path.to.interface.module

26
Q

How do we define service consumer?

A

We can create class in some package, where we instantiate service locator and run wanted methods.
In same package we define module file, where we must define:
requires full.path.to.service.locator

27
Q

What does “requires” keyword mean?

A

It is used in module-info files to define dependecy that certaing module needs

28
Q

What does “requires transitive” keywords mean?

A

If some module B requires another module C, that module C will be required by every module that wants to require B. B can use “requires transitive” to enable every other module to implicitly extend some module.

29
Q

What does “exports” keyword mean?

A

It is used to define what package from module will be available to other modules, but only public methods.

30
Q

What does “exports … to” keyword mean?

A

It is used to define what package from module will be available to other specific module, but only public methods.

31
Q

What does “opens” keyword mean?

A

It is used to define what package methods and variables from module will be available to other modules via reflection.

32
Q

What does “opens .. to “ keyword mean?

A

It is used to define what package methods and variables from module will be available to other specific module via reflection.