88import com .github .javaparser .ast .body .Parameter ;
99import com .github .javaparser .ast .body .VariableDeclarator ;
1010import com .github .javaparser .ast .expr .NameExpr ;
11+ import com .github .javaparser .ast .nodeTypes .NodeWithSimpleName ;
1112import com .github .javaparser .ast .visitor .GenericVisitor ;
1213import com .github .javaparser .ast .visitor .VoidVisitor ;
1314import com .github .javaparser .resolution .declarations .ResolvedValueDeclaration ;
1920import java .io .IOException ;
2021import java .nio .file .Paths ;
2122import java .util .*;
23+ import java .util .regex .Matcher ;
24+ import java .util .regex .Pattern ;
2225
2326public class SourceNaming {
2427
@@ -30,17 +33,20 @@ public static void naming(CompilationUnit cu, List<SourceProblem> list) {
3033 ResolvedValueDeclaration rne = ne .resolve ();
3134
3235 Node node = null ;
33- if (rne instanceof JavaParserSymbolDeclaration ) {
36+ if (rne instanceof JavaParserSymbolDeclaration ) {
3437 node = ((JavaParserSymbolDeclaration )rne ).getWrappedNode ();
3538 }
3639 else if (rne instanceof JavaParserFieldDeclaration ) {
37- node = ((JavaParserFieldDeclaration )rne ).getWrappedNode ();
40+ FieldDeclaration field = ((JavaParserFieldDeclaration )rne ).getWrappedNode ();
41+ node = field .getVariables ().stream ()
42+ .filter (d -> d .getName ().equals (ne .getName ()))
43+ .findFirst ().orElseThrow (() -> new RuntimeException ("cannot find field" ));
3844 }
3945 else if (rne instanceof JavaParserParameterDeclaration ) {
4046 node = ((JavaParserParameterDeclaration )rne ).getWrappedNode ();
4147 }
4248
43- if (symbolMap .containsKey (node )) {
49+ if (symbolMap .containsKey (node )) {
4450 symbolMap .get (node ).add (ne );
4551 }
4652 else {
@@ -52,38 +58,61 @@ else if (rne instanceof JavaParserParameterDeclaration) {
5258 });
5359
5460 symbolMap .forEach ((k , v ) -> {
55- if (k instanceof FieldDeclaration ) {
56- // TODO
57- }
58- else if (k instanceof Parameter ) {
59- // TODO
60- }
61- else if (k instanceof VariableDeclarator ){
62- // local vars
63- if (k .getChildNodes ().size () >= 2 ) {
64- Node child = k .getChildNodes ().get (1 );
61+ if (k .getChildNodes ().size () >= 2 ) {
62+ Node child = k .getChildNodes ().get (1 );
6563
66- fixNameToLower (child .toString ()).ifPresent (name -> {
67- ((VariableDeclarator )k ).setName (name );
64+ fixNameToLower (child .toString ()).ifPresent (name -> {
65+ if (symbolMap .entrySet ().stream ().noneMatch (x -> // filter out all of node with conflict
66+ ((NodeWithSimpleName )x .getKey ()).getName ().toString ().equals (name )
67+ )) {
68+ ((NodeWithSimpleName ) k ).setName (name );
6869
6970 for (Node n : v ) {
70- ((NameExpr )n ).setName (name );
71+ ((NameExpr ) n ).setName (name );
7172 }
7273
7374 TokenRange tokenRange = k .getTokenRange ().orElseThrow (() -> new RuntimeException ("cannot get range" ));
74- list .add (new SourceProblem ("s5.2.7-local-variable-names" , tokenRange ));
75- });
76- }
75+
76+ if (k instanceof VariableDeclarator ) {
77+ Node p = k .getParentNode ().orElse (null );
78+ if (p instanceof FieldDeclaration ) {
79+ list .add (new SourceProblem ("s5.2.5-non-constant-field-names" , tokenRange ));
80+ } else {
81+ list .add (new SourceProblem ("s5.2.7-local-variable-names" , tokenRange ));
82+ }
83+
84+ } else if (k instanceof Parameter ) {
85+ list .add (new SourceProblem ("s5.2.6-parameter-names" , tokenRange ));
86+ }
87+ }
88+ });
7789 }
7890 });
7991 }
8092
8193 private static Optional <String > fixNameToLower (String name ) {
82- if (name .charAt (0 ) >= 'A' && name .charAt (0 ) <= 'Z' ) {
83- return Optional .of (name .substring (0 , 1 ).toLowerCase () + name .substring (1 ));
94+ StringBuffer result = new StringBuffer (name );
95+
96+ // first char
97+ if (result .charAt (0 ) >= 'A' && result .charAt (0 ) <= 'Z' ) {
98+ result .setCharAt (0 , Character .toLowerCase (result .charAt (0 )));
99+ }
100+
101+ // underline to camel
102+ Matcher m = Pattern .compile ("_[a-z]" ).matcher (result );
103+ StringBuffer sb = new StringBuffer ();
104+ while (m .find ()) {
105+ m .appendReplacement (sb , m .group ().replace ("_" ,"" ).toUpperCase ());
84106 }
107+ m .appendTail (sb );
108+ result = sb ;
85109
86- else return Optional .empty ();
110+ if (result .toString ().equals (name )) {
111+ return Optional .empty ();
112+ }
113+ else {
114+ return Optional .of (result .toString ());
115+ }
87116 }
88117
89118 public static void main (String [] args ) throws IOException , SourceException {
0 commit comments