Skip to content

Commit 3b0926b

Browse files
committed
Added publicClass attribute to SimpleJavaGenerator
1 parent 73c4dab commit 3b0926b

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Added publicClass attribute to SimpleJavaGenerator
13+
1014
## [8.4.9] - 2024-02-21
1115

1216
### Changed

fj-core/src/main/java/org/fugerit/java/core/javagen/SimpleJavaGenerator.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515

1616
public abstract class SimpleJavaGenerator extends BasicJavaGenerator {
1717

18+
/**
19+
* Default value for publicClass property, value : true
20+
*/
21+
public static final boolean DEFAULT_PUBLIC_CLASS = true;
22+
23+
protected SimpleJavaGenerator() {
24+
this.publicClass = DEFAULT_PUBLIC_CLASS;
25+
}
26+
1827
public static final String PROP_VERSION = "gen-version";
1928
public static final String PROP_AUTHOR = "gen-author";
2029
public static final String PROP_CLASS_COMMENT = "gen-class-comment";
@@ -48,6 +57,8 @@ public void init( File sourceFolder, String fullObjectBName, String javaStyle, P
4857
private Properties config;
4958

5059
private boolean noCustomComment;
60+
61+
private boolean publicClass;
5162

5263
public boolean isNoCustomComment() {
5364
return noCustomComment;
@@ -57,6 +68,14 @@ public void setNoCustomComment(boolean noCustomComment) {
5768
this.noCustomComment = noCustomComment;
5869
}
5970

71+
public boolean isPublicClass() {
72+
return publicClass;
73+
}
74+
75+
public void setPublicClass(boolean publicClass) {
76+
this.publicClass = publicClass;
77+
}
78+
6079
protected Properties getConfig() {
6180
return config;
6281
}
@@ -138,7 +157,7 @@ public void generate() throws IOException {
138157
impl+= " implements "+this.implementsInterface;
139158
}
140159
this.beforeClass();
141-
this.getWriter().println( "public "+this.getJavaStyle()+" "+this.getJavaName()+impl+" {" );
160+
this.getWriter().println( (this.isPublicClass() ? "public " : "") +this.getJavaStyle()+" "+this.getJavaName()+impl+" {" );
142161
this.getWriter().println();
143162
this.customPartWorker( CUSTOM_CODE_START , CUSTOM_CODE_END, "\t" );
144163
this.generateBody();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package test.org.fugerit.java.core.javagen;
2+
3+
import org.fugerit.java.core.cfg.ConfigException;
4+
import org.fugerit.java.core.javagen.GeneratorNameHelper;
5+
import org.fugerit.java.core.javagen.SimpleJavaGenerator;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.Serializable;
10+
import java.util.AbstractList;
11+
import java.util.Properties;
12+
13+
public class NoPublicClassJavaGenerator extends SimpleJavaGenerator {
14+
15+
public void init() throws ConfigException {
16+
this.setExtendsClass( AbstractList.class.getName() );
17+
this.setImplementsInterface( Serializable.class.getName() );
18+
this.setPublicClass( false );
19+
super.init( new File( "target/generator_test" ), "test.TestClass", STYLE_CLASS, new Properties() );
20+
}
21+
22+
@Override
23+
public void generateBody() throws IOException {
24+
this.addSerialVerUID();
25+
this.printlnWithTabs( 2, "//"+GeneratorNameHelper.toClassName( this.getPackageName() ) );
26+
this.printlnWithTabs( 2, "private String "+GeneratorNameHelper.toPropertyName( "test" ) );
27+
}
28+
29+
}

fj-core/src/test/java/test/org/fugerit/java/core/javagen/TestJavaGenerator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,20 @@ public void generate() {
2323
this.failEx(e);
2424
}
2525
}
26+
27+
@Test
28+
public void generateNoPublicClass() {
29+
try {
30+
NoPublicClassJavaGenerator generator = new NoPublicClassJavaGenerator();
31+
generator.init();
32+
generator.generate();
33+
log.info( "ok : {}", generator.getContent() );
34+
generator.write();
35+
generator.generate();
36+
Assert.assertNotNull(generator);
37+
} catch (Exception e) {
38+
this.failEx(e);
39+
}
40+
}
2641

2742
}

0 commit comments

Comments
 (0)