Skip to content

Commit a9b3279

Browse files
authored
Merge pull request #210 from skapral/issue/208
[#208] Added excludePackage configuration parameter to atom-maven-plugin
2 parents 98e73fd + ad169ed commit a9b3279

File tree

5 files changed

+131
-9
lines changed

5 files changed

+131
-9
lines changed

atom-basis/src/main/java/com/pragmaticobjects/oo/atom/codegen/AtomizerMain.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.pragmaticobjects.oo.atom.codegen;
2626

2727
import com.pragmaticobjects.oo.atom.anno.NotAtom;
28+
import com.pragmaticobjects.oo.atom.codegen.cn.CnFromPath;
2829
import com.pragmaticobjects.oo.atom.codegen.cp.CpFromString;
2930
import com.pragmaticobjects.oo.atom.codegen.stage.AggroInstrumentationStage;
3031
import com.pragmaticobjects.oo.atom.codegen.stage.CopyAtomAnnotations;
@@ -64,6 +65,9 @@ public static final void main(String... args) throws Exception {
6465
System.getProperty("java.class.path")
6566
),
6667
workingDirectory,
68+
new CnFromPath(
69+
workingDirectory
70+
),
6771
stage,
6872
new CopyAtomAnnotations()
6973
).apply();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.pragmaticobjects.oo.atom.codegen.cn;
2+
3+
import io.vavr.collection.List;
4+
5+
/**
6+
* Class names list, excluding classes from certain packages
7+
*
8+
* @author Kapralov Sergey
9+
*/
10+
public class CnExcludingPackages implements ClassNames {
11+
private final ClassNames delegate;
12+
private final List<String> packages;
13+
14+
/**
15+
* Ctor.
16+
*
17+
* @param delegate Source class names
18+
* @param packages Packages to exclude
19+
*/
20+
public CnExcludingPackages(ClassNames delegate, List<String> packages) {
21+
this.delegate = delegate;
22+
this.packages = packages;
23+
}
24+
25+
/**
26+
* Ctor.
27+
*
28+
* @param delegate Source class names
29+
* @param packages Packages to exclude
30+
*/
31+
public CnExcludingPackages(ClassNames delegate, String... packages) {
32+
this(
33+
delegate,
34+
List.of(packages)
35+
);
36+
}
37+
38+
@Override
39+
public final List<String> classNames() {
40+
List<String> names = this.delegate.classNames();
41+
for(String pkg : packages) {
42+
names = names.filter(cn -> !cn.startsWith(pkg + "."));
43+
}
44+
return names;
45+
}
46+
}

atom-basis/src/main/java/com/pragmaticobjects/oo/atom/instrumentation/ApplyStages.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package com.pragmaticobjects.oo.atom.instrumentation;
2525

2626
import com.pragmaticobjects.oo.atom.codegen.cn.ClassNames;
27-
import com.pragmaticobjects.oo.atom.codegen.cn.CnFromPath;
2827
import com.pragmaticobjects.oo.atom.codegen.cp.ClassPath;
2928
import com.pragmaticobjects.oo.atom.codegen.cp.CpCombined;
3029
import com.pragmaticobjects.oo.atom.codegen.cp.CpExplicit;
@@ -42,18 +41,22 @@ public class ApplyStages implements Instrumentation {
4241
private final ClassPath classPath;
4342
private final Path workingDirectory;
4443
private final List<Stage> stages;
44+
private final ClassNames classNames;
45+
4546

4647
/**
4748
* Ctor.
4849
*
4950
* @param classPath {@link ClassPath}
5051
* @param workingDirectory Working directory - root of the location where instrumented classes located.
5152
* @param stages Stages to apply.
53+
* @param classNames Class names
5254
*/
53-
public ApplyStages(final ClassPath classPath, final Path workingDirectory, final List<Stage> stages) {
55+
public ApplyStages(ClassPath classPath, Path workingDirectory, ClassNames classNames, List<Stage> stages) {
5456
this.classPath = classPath;
5557
this.workingDirectory = workingDirectory;
5658
this.stages = stages;
59+
this.classNames = classNames;
5760
}
5861

5962
/**
@@ -62,11 +65,13 @@ public ApplyStages(final ClassPath classPath, final Path workingDirectory, final
6265
* @param classPath {@link ClassPath}
6366
* @param workingDirectory Working directory - root of the location where instrumented classes located.
6467
* @param stages Stages to apply.
68+
* @param classNames Class names
6569
*/
66-
public ApplyStages(final ClassPath classPath, final Path workingDirectory, final Stage... stages) {
70+
public ApplyStages(final ClassPath classPath, final Path workingDirectory, ClassNames classNames, final Stage... stages) {
6771
this(
6872
classPath,
6973
workingDirectory,
74+
classNames,
7075
List.of(stages)
7176
);
7277
}
@@ -79,9 +84,6 @@ public final void apply() {
7984
workingDirectory
8085
)
8186
);
82-
final ClassNames classNames = new CnFromPath(
83-
workingDirectory
84-
);
8587
for(Stage stage : stages) {
8688
stage.apply(classPath, classNames, workingDirectory);
8789
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.pragmaticobjects.oo.atom.codegen.cn;
2+
3+
import com.pragmaticobjects.oo.atom.tests.TestCase;
4+
import com.pragmaticobjects.oo.atom.tests.TestsSuite;
5+
6+
/**
7+
* Tests suite for {@link CnExcludingPackages}
8+
*
9+
* @author Kapralov Sergey
10+
*/
11+
class CnExcludingPackagesTest extends TestsSuite {
12+
/**
13+
* Ctor.
14+
*/
15+
public CnExcludingPackagesTest() {
16+
super(
17+
new TestCase(
18+
"Excluding nothing",
19+
new AssertClassNamesContainCertainNames(
20+
new CnExcludingPackages(
21+
new CnExplicit(
22+
"com.package1.Foo",
23+
"com.package2.Bar"
24+
)
25+
),
26+
"com.package1.Foo",
27+
"com.package2.Bar"
28+
)
29+
),
30+
new TestCase(
31+
"Excluding a single package",
32+
new AssertClassNamesContainCertainNames(
33+
new CnExcludingPackages(
34+
new CnExplicit(
35+
"com.package1.Foo",
36+
"com.package2.Bar"
37+
),
38+
"com.package1"
39+
),
40+
"com.package2.Bar"
41+
)
42+
),
43+
new TestCase(
44+
"Excluding non-existing package",
45+
new AssertClassNamesContainCertainNames(
46+
new CnExcludingPackages(
47+
new CnExplicit(
48+
"com.package1.Foo",
49+
"com.package2.Bar"
50+
),
51+
"com.package"
52+
),
53+
"com.package1.Foo",
54+
"com.package2.Bar"
55+
)
56+
)
57+
);
58+
}
59+
}

atom-maven-plugin/src/main/java/com/pragmaticobjects/oo/atom/maven/BaseMojo.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.pragmaticobjects.oo.atom.maven;
22

3+
import com.pragmaticobjects.oo.atom.codegen.cn.CnExcludingPackages;
4+
import com.pragmaticobjects.oo.atom.codegen.cn.CnFromPath;
35
import com.pragmaticobjects.oo.atom.codegen.cp.ClassPath;
46
import com.pragmaticobjects.oo.atom.codegen.cp.CpFromString;
57
import com.pragmaticobjects.oo.atom.codegen.stage.Stage;
@@ -13,6 +15,7 @@
1315

1416
import java.io.File;
1517
import java.nio.file.Path;
18+
import java.util.Optional;
1619
import java.util.stream.Collectors;
1720

1821
/**
@@ -25,6 +28,8 @@ public abstract class BaseMojo extends AbstractMojo {
2528
private MavenProject project;
2629
@Parameter(defaultValue = "false", required = true, readonly = true)
2730
private boolean instrumentPomProjects;
31+
@Parameter
32+
private String[] excludePackages;
2833

2934
protected final void doInstrumentation(Stage stage, Path workingDirectory) throws MojoExecutionException, MojoFailureException {
3035
if(!project.getPackaging().equals("pom") || instrumentPomProjects) {
@@ -34,9 +39,15 @@ protected final void doInstrumentation(Stage stage, Path workingDirectory) throw
3439
.collect(Collectors.joining(":"));
3540
ClassPath cp = new CpFromString(classPath);
3641
new ApplyStages(
37-
cp,
38-
workingDirectory,
39-
stage
42+
cp,
43+
workingDirectory,
44+
new CnExcludingPackages(
45+
new CnFromPath(
46+
workingDirectory
47+
),
48+
Optional.ofNullable(excludePackages).orElse(new String[] {})
49+
),
50+
stage
4051
).apply();
4152
}
4253
}

0 commit comments

Comments
 (0)