Skip to content

Commit 0ab5f45

Browse files
committed
add SourceFileParser, TODO: use it :) (#29)
1 parent 046ca48 commit 0ab5f45

File tree

13 files changed

+353
-34
lines changed

13 files changed

+353
-34
lines changed

code-assert/src/main/java/guru/nidi/codeassert/junit/PredefConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public static CollectorTemplate<Ignore> minimalPmdIgnore() {
3737
.ignore("MethodArgumentCouldBeFinal", "AvoidFieldNameMatchingMethodName",
3838
"CommentDefaultAccessModifier", "AbstractNaming", "AvoidFieldNameMatchingTypeName",
3939
"UncommentedEmptyConstructor", "UseStringBufferForStringAppends",
40-
"UncommentedEmptyMethodBody", "EmptyMethodInAbstractClassShouldBeAbstract"))
40+
"UncommentedEmptyMethodBody", "EmptyMethodInAbstractClassShouldBeAbstract",
41+
"InefficientEmptyStringCheck"))
4142
.because("it's equals", In.methods("equals")
4243
.ignore("NPathComplexity", "ModifiedCyclomaticComplexity", "StdCyclomaticComplexity",
4344
"CyclomaticComplexity", "ConfusingTernary"))

code-assert/src/main/java/guru/nidi/codeassert/model/ClassFileParser.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,16 @@ class ClassFileParser {
3333
private static final int JAVA_MAGIC = 0xCAFEBABE;
3434

3535
private ConstantPool constantPool;
36-
private CountingInputStream counter;
3736
private DataInputStream in;
3837

39-
public CodeClass parse(File file, Model model) throws IOException {
38+
CodeClass parse(File file, Model model) throws IOException {
4039
try (final InputStream in = new FileInputStream(file)) {
4140
return parse(in, model);
4241
}
4342
}
4443

45-
public CodeClass parse(InputStream is, Model model) throws IOException {
46-
counter = new CountingInputStream(is);
44+
CodeClass parse(InputStream is, Model model) throws IOException {
45+
final CountingInputStream counter = new CountingInputStream(is);
4746
in = new DataInputStream(counter);
4847

4948
parseMagic();
@@ -70,7 +69,7 @@ public CodeClass parse(InputStream is, Model model) throws IOException {
7069
.addMethodRefs(methods)
7170
.addAttributeRefs(attributes)
7271
.addPackageInfo(model, className)
73-
.addSizes(counter.getCount(), methods)
72+
.addCodeSizes(counter.getCount(), methods)
7473
.clazz;
7574
}
7675

code-assert/src/main/java/guru/nidi/codeassert/model/CodeClass.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class CodeClass extends UsingElement<CodeClass> {
3939
int codeSize;
4040
int totalSize;
4141
boolean concrete;
42+
int codeLines;
43+
int commentLines;
44+
int emptyLines;
45+
int totalLines;
4246

4347
CodeClass(String name, CodePackage pack) {
4448
this.name = name;
@@ -85,6 +89,22 @@ public boolean isConcrete() {
8589
return concrete;
8690
}
8791

92+
public int getCodeLines() {
93+
return codeLines;
94+
}
95+
96+
public int getCommentLines() {
97+
return commentLines;
98+
}
99+
100+
public int getEmptyLines() {
101+
return emptyLines;
102+
}
103+
104+
public int getTotalLines() {
105+
return totalLines;
106+
}
107+
88108
@Override
89109
public CodeClass self() {
90110
return this;

code-assert/src/main/java/guru/nidi/codeassert/model/CodeClassBuilder.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ class CodeClassBuilder {
2929
private final Model model;
3030
private final ConstantPool constantPool;
3131

32-
CodeClassBuilder(String className, Model model, ConstantPool constantPool) {
32+
private CodeClassBuilder(CodeClass clazz, Model model, ConstantPool constantPool) {
33+
this.clazz = clazz;
3334
this.model = model;
34-
this.clazz = model.getOrCreateClass(className);
3535
this.constantPool = constantPool;
3636
}
3737

38+
CodeClassBuilder(String className, Model model, ConstantPool constantPool) {
39+
this(model.getOrCreateClass(className), model, constantPool);
40+
}
41+
42+
CodeClassBuilder(CodeClass clazz) {
43+
this(clazz, null, null);
44+
}
45+
3846
public CodeClassBuilder addSuperClass(String className) {
3947
addImport(className);
4048
return this;
@@ -97,7 +105,7 @@ public CodeClassBuilder addPackageInfo(Model model, String className) {
97105
return this;
98106
}
99107

100-
public CodeClassBuilder addSizes(int totalSize, List<MemberInfo> methods) {
108+
public CodeClassBuilder addCodeSizes(int totalSize, List<MemberInfo> methods) {
101109
int codeSize = 0;
102110
for (final MemberInfo method : methods) {
103111
codeSize += method.codeSize;
@@ -107,6 +115,14 @@ public CodeClassBuilder addSizes(int totalSize, List<MemberInfo> methods) {
107115
return this;
108116
}
109117

118+
public CodeClassBuilder addSourceSizes(int codeLines, int commentLines, int emptyLines, int totalLines) {
119+
clazz.codeLines = codeLines;
120+
clazz.commentLines = commentLines;
121+
clazz.emptyLines = emptyLines;
122+
clazz.totalLines = totalLines;
123+
return this;
124+
}
125+
110126
private void addMemberAnnotationRefs(List<MemberInfo> infos) throws IOException {
111127
for (final MemberInfo info : infos) {
112128
if (info.annotations != null) {

code-assert/src/main/java/guru/nidi/codeassert/model/Constant.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.io.IOException;
2020

2121
final class Constant {
22-
public static final int
22+
static final int
2323
UTF8 = 1,
2424
UNICODE = 2,
2525
INTEGER = 3,
@@ -41,7 +41,7 @@ final class Constant {
4141
final int typeIndex;
4242
final Object value;
4343

44-
public static Constant fromData(DataInputStream in) throws IOException {
44+
static Constant fromData(DataInputStream in) throws IOException {
4545
final byte tag = in.readByte();
4646
switch (tag) {
4747
case CLASS:
@@ -90,7 +90,7 @@ private Constant(byte tag, int nameIndex, int typeIndex, Object value) {
9090
this.value = value;
9191
}
9292

93-
public boolean isBig() {
93+
boolean isBig() {
9494
return tag == DOUBLE || tag == LONG;
9595
}
9696
}

code-assert/src/main/java/guru/nidi/codeassert/model/Model.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,55 @@
2222
import java.util.jar.JarInputStream;
2323
import java.util.zip.ZipEntry;
2424

25+
import static java.util.Arrays.asList;
26+
2527
public class Model {
2628
public static final String UNNAMED_PACKAGE = "<Unnamed Package>";
2729

2830
final Map<String, CodePackage> packages = new HashMap<>();
2931
final Map<String, CodeClass> classes = new HashMap<>();
3032

3133
public static Model from(File... files) {
32-
return from(Arrays.asList(files));
34+
return from(asList(files));
3335
}
3436

3537
public static Model from(List<File> files) {
38+
return new Model().and(files);
39+
}
40+
41+
public Model and(File... files) {
42+
return and(asList(files));
43+
}
44+
45+
public Model and(List<File> files) {
3646
try {
37-
final Model model = new Model();
38-
final ClassFileParser parser = new ClassFileParser();
47+
final ClassFileParser classParser = new ClassFileParser();
3948
for (final File file : files) {
4049
try (final InputStream in = new FileInputStream(file)) {
41-
add(parser, model, file.getName(), in);
50+
add(classParser, file.getName(), in);
4251
}
4352
}
44-
return model;
53+
return this;
4554
} catch (IOException e) {
4655
throw new AnalyzerException("Problem creating a Model", e);
4756
}
4857
}
4958

50-
private static void add(ClassFileParser parser, Model model, String name, InputStream in) throws IOException {
59+
private void add(ClassFileParser parser, String name, InputStream in) throws IOException {
5160
if (name.endsWith(".jar") || name.endsWith(".zip") || name.endsWith(".war") || name.endsWith(".ear")) {
5261
final JarInputStream jar = new JarInputStream(in);
5362
ZipEntry entry;
5463
while ((entry = jar.getNextEntry()) != null) {
5564
try {
5665
if (!entry.isDirectory()) {
57-
add(parser, model, entry.getName(), jar);
66+
add(parser, entry.getName(), jar);
5867
}
5968
} finally {
6069
jar.closeEntry();
6170
}
6271
}
6372
} else if (name.endsWith(".class")) {
64-
parser.parse(in, model);
73+
parser.parse(in, this);
6574
}
6675
}
6776

0 commit comments

Comments
 (0)