Skip to content

Commit 22d3737

Browse files
committed
refactoring and added tests for maven plugin
1 parent 46c02cd commit 22d3737

File tree

15 files changed

+264
-55
lines changed

15 files changed

+264
-55
lines changed

changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
- __reworked mapping system, removed hacks to instantiate classes, now only objects allowed__
33
- __minimal JDK version is increaded to 1.8+__
44
- __minimal Android API is increased to 3.0+__
5-
- added creation of `makeFIELD()` method for structure types in Java6 converted classes
5+
- added creation of `makeFIELD()` method for structure types in Java converted classes
66
- refactoring
77

88
1.4.1

jbbp-plugins/jbbp-gradle/src/main/java/com/igormaznitsa/jbbp/plugin/gradle/JBBPExtension.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import com.igormaznitsa.jbbp.plugin.common.converters.ParserFlags;
44
import com.igormaznitsa.jbbp.plugin.common.converters.Target;
5-
import org.gradle.api.Project;
6-
import org.gradle.api.file.ConfigurableFileTree;
7-
8-
import javax.annotation.Nonnull;
95
import java.io.File;
106
import java.util.HashMap;
117
import java.util.HashSet;
128
import java.util.Map;
139
import java.util.Set;
10+
import javax.annotation.Nonnull;
11+
import org.gradle.api.Project;
12+
import org.gradle.api.file.ConfigurableFileTree;
1413

1514
/**
1615
* JBBP extension settings.
@@ -137,7 +136,7 @@ public class JBBPExtension {
137136
/**
138137
* Generate methods newInstance in generated classes.
139138
*/
140-
public boolean genNewInstance;
139+
public boolean addNewInstanceMethods;
141140

142141
/**
143142
* Generate Bin annotations for fields

jbbp-plugins/jbbp-gradle/src/main/java/com/igormaznitsa/jbbp/plugin/gradle/JBBPGenerateTask.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.igormaznitsa.jbbp.plugin.gradle;
22

3+
import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_STRING_EMPTY;
4+
5+
36
import com.igormaznitsa.jbbp.JBBPCustomFieldTypeProcessor;
47
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
58
import com.igormaznitsa.jbbp.compiler.tokenizer.JBBPFieldTypeParameterContainer;
@@ -13,23 +16,20 @@
1316
import com.igormaznitsa.meta.annotation.MustNotContainNull;
1417
import com.igormaznitsa.meta.common.utils.Assertions;
1518
import com.igormaznitsa.meta.common.utils.GetUtils;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.util.HashSet;
22+
import java.util.Locale;
23+
import java.util.Set;
24+
import javax.annotation.Nonnull;
25+
import javax.annotation.Nullable;
1626
import org.gradle.api.GradleException;
1727
import org.gradle.api.plugins.JavaPlugin;
1828
import org.gradle.api.plugins.JavaPluginConvention;
1929
import org.gradle.api.tasks.Input;
2030
import org.gradle.api.tasks.Optional;
2131
import org.gradle.api.tasks.SourceSet;
2232

23-
import javax.annotation.Nonnull;
24-
import javax.annotation.Nullable;
25-
import java.io.File;
26-
import java.io.IOException;
27-
import java.util.HashSet;
28-
import java.util.Locale;
29-
import java.util.Set;
30-
31-
import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_STRING_EMPTY;
32-
3333
/**
3434
* Task to translate found JBBP scripts in source files.
3535
*
@@ -99,7 +99,7 @@ public JBBPAbstractField readCustomFieldType(@Nonnull final JBBPBitInputStream i
9999
.setEncodingOut(CommonUtils.ensureEncodingName(ext.outEncoding))
100100
.setCustomFieldTypeProcessor(customFieldProcessor)
101101
.setSuperClass(ext.superClass)
102-
.setGenNewInstance(ext.genNewInstance)
102+
.setAddNewInstanceMethods(ext.addNewInstanceMethods)
103103
.setAddBinAnnotations(ext.addBinAnnotations)
104104
.setClassImplements(ext.interfaces)
105105
.setSubClassInterfaces(ext.mapSubClassInterfaces)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.igormaznitsa</groupId>
7+
<artifactId>jbbp-maven-plugin-tests</artifactId>
8+
<version>0.0.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>jbbp-mvn-test-genannotations-nonstatic</artifactId>
12+
<packaging>jar</packaging>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>${project.groupId}</groupId>
18+
<artifactId>jbbp-maven-plugin</artifactId>
19+
<version>${jbbp.plugin.version}</version>
20+
<executions>
21+
<execution>
22+
<goals>
23+
<goal>generate</goal>
24+
</goals>
25+
</execution>
26+
</executions>
27+
<configuration>
28+
<headComment>Generated with annotations</headComment>
29+
<addBinAnnotations>true</addBinAnnotations>
30+
<addGettersSetters>true</addGettersSetters>
31+
<addNewInstanceMethods>true</addNewInstanceMethods>
32+
<doInnerClassesNonStatic>true</doInnerClassesNonStatic>
33+
</configuration>
34+
</plugin>
35+
</plugins>
36+
</build>
37+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ubyte len;
2+
some1 {
3+
bit:4 [len] someField;
4+
ubyte len;
5+
some2 {
6+
byte [len] field;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2019 Igor Maznitsa.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.igormaznitsa.mvn.test.jbbp;
18+
19+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
22+
23+
import com.igormaznitsa.jbbp.JBBPParser;
24+
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
25+
import java.io.ByteArrayInputStream;
26+
import java.io.IOException;
27+
import org.junit.jupiter.api.Test;
28+
29+
class GenAnnotationsNonStaticTest {
30+
31+
@Test
32+
void testReadWrite() throws IOException {
33+
final byte[] testData = new byte[] {4, (byte) 0x12, (byte) 0x34, 3, 5, 6, 7};
34+
35+
final GenAnnotationsNonStatic result = new GenAnnotationsNonStatic().read(new JBBPBitInputStream(new ByteArrayInputStream(testData)));
36+
assertEquals(4, result.getLEN());
37+
assertEquals(3, result.getSOME1().getSOME2().getFIELD().length);
38+
39+
final String script = "ubyte len;"
40+
+ "some1 {"
41+
+ " bit:4 [len] someField;"
42+
+ " ubyte len;"
43+
+ " some2 {"
44+
+ " byte [len] field;"
45+
+ " }"
46+
+ "}";
47+
48+
final GenAnnotationsNonStatic instance = JBBPParser.prepare(script).parse(testData).mapTo(new GenAnnotationsNonStatic());
49+
assertEquals(result.getLEN(), instance.getLEN());
50+
assertEquals(result.getSOME1().getLEN(), instance.getSOME1().getLEN());
51+
assertArrayEquals(result.getSOME1().getSOMEFIELD(), instance.getSOME1().getSOMEFIELD());
52+
assertArrayEquals(result.getSOME1().getSOME2().getFIELD(), instance.getSOME1().getSOME2().getFIELD());
53+
}
54+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.igormaznitsa</groupId>
7+
<artifactId>jbbp-maven-plugin-tests</artifactId>
8+
<version>0.0.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>jbbp-mvn-test-genannotations</artifactId>
12+
<packaging>jar</packaging>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>${project.groupId}</groupId>
18+
<artifactId>jbbp-maven-plugin</artifactId>
19+
<version>${jbbp.plugin.version}</version>
20+
<executions>
21+
<execution>
22+
<goals>
23+
<goal>generate</goal>
24+
</goals>
25+
</execution>
26+
</executions>
27+
<configuration>
28+
<headComment>Generated with annotations</headComment>
29+
<addBinAnnotations>true</addBinAnnotations>
30+
<addGettersSetters>true</addGettersSetters>
31+
<addNewInstanceMethods>true</addNewInstanceMethods>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ubyte len;
2+
some1 {
3+
bit:4 [len] someField;
4+
ubyte len;
5+
some2 {
6+
byte [len] field;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2019 Igor Maznitsa.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.igormaznitsa.mvn.test.jbbp;
18+
19+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
22+
23+
import com.igormaznitsa.jbbp.JBBPParser;
24+
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
25+
import java.io.ByteArrayInputStream;
26+
import java.io.IOException;
27+
import org.junit.jupiter.api.Test;
28+
29+
class GenAnnotationsTest {
30+
31+
@Test
32+
void testReadWrite() throws IOException {
33+
final byte[] testData = new byte[] {4, (byte) 0x12, (byte) 0x34, 3, 5, 6, 7};
34+
35+
final GenAnnotations result = new GenAnnotations().read(new JBBPBitInputStream(new ByteArrayInputStream(testData)));
36+
assertEquals(4, result.getLEN());
37+
assertEquals(3, result.getSOME1().getSOME2().getFIELD().length);
38+
39+
final String script = "ubyte len;"
40+
+ "some1 {"
41+
+ " bit:4 [len] someField;"
42+
+ " ubyte len;"
43+
+ " some2 {"
44+
+ " byte [len] field;"
45+
+ " }"
46+
+ "}";
47+
48+
final GenAnnotations instance = JBBPParser.prepare(script).parse(testData).mapTo(new GenAnnotations());
49+
assertEquals(result.getLEN(), instance.getLEN());
50+
assertEquals(result.getSOME1().getLEN(), instance.getSOME1().getLEN());
51+
assertArrayEquals(result.getSOME1().getSOMEFIELD(), instance.getSOME1().getSOMEFIELD());
52+
assertArrayEquals(result.getSOME1().getSOME2().getFIELD(), instance.getSOME1().getSOME2().getFIELD());
53+
}
54+
}

jbbp-plugins/jbbp-maven/jbbp-maven-plugin/src/main/java/com/igormaznitsa/jbbp/plugin/mvn/JBBPGenerateMojo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@ public JBBPAbstractField readCustomFieldType(@Nonnull final JBBPBitInputStream i
378378
.setSubClassSuperclasses(this.getMapStructToSuperclasses())
379379
.setAddGettersSetters(this.getAddGettersSetters())
380380
.setDoAbstract(this.isDoAbstract())
381-
.setDisableGenerateFields(this.isDisableGenerateFields());
381+
.setDisableGenerateFields(this.isDisableGenerateFields())
382+
.setAddBinAnnotations(this.isAddBinAnnotations())
383+
.setAddNewInstanceMethods(this.isAddNewInstanceMethods());
382384

383385
for (final File aScript : foundJBBPScripts) {
384386
parameters.setScriptFile(aScript).assertAllOk();

0 commit comments

Comments
 (0)