|
| 1 | +package org.hacksource.core; |
| 2 | + |
| 3 | +import com.github.javaparser.TokenRange; |
| 4 | +import com.github.javaparser.ast.CompilationUnit; |
| 5 | +import com.github.javaparser.ast.Node; |
| 6 | + |
| 7 | +import com.github.javaparser.ast.body.FieldDeclaration; |
| 8 | +import com.github.javaparser.ast.body.Parameter; |
| 9 | +import com.github.javaparser.ast.body.VariableDeclarator; |
| 10 | +import com.github.javaparser.ast.expr.NameExpr; |
| 11 | +import com.github.javaparser.ast.visitor.GenericVisitor; |
| 12 | +import com.github.javaparser.ast.visitor.VoidVisitor; |
| 13 | +import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration; |
| 14 | +import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserFieldDeclaration; |
| 15 | +import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserSymbolDeclaration; |
| 16 | +import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserParameterDeclaration; |
| 17 | + |
| 18 | +import java.io.FileWriter; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Paths; |
| 21 | +import java.util.*; |
| 22 | + |
| 23 | +public class SourceNaming { |
| 24 | + |
| 25 | + public static void naming(CompilationUnit cu, List<SourceProblem> list) { |
| 26 | + |
| 27 | + Map<Node, List<Node>> symbolMap = new HashMap<>(); |
| 28 | + |
| 29 | + cu.findAll(NameExpr.class).forEach(ne -> { |
| 30 | + ResolvedValueDeclaration rne = ne.resolve(); |
| 31 | + |
| 32 | + Node node = null; |
| 33 | + if(rne instanceof JavaParserSymbolDeclaration) { |
| 34 | + node = ((JavaParserSymbolDeclaration)rne).getWrappedNode(); |
| 35 | + } |
| 36 | + else if (rne instanceof JavaParserFieldDeclaration) { |
| 37 | + node = ((JavaParserFieldDeclaration)rne).getWrappedNode(); |
| 38 | + } |
| 39 | + else if (rne instanceof JavaParserParameterDeclaration) { |
| 40 | + node = ((JavaParserParameterDeclaration)rne).getWrappedNode(); |
| 41 | + } |
| 42 | + |
| 43 | + if(symbolMap.containsKey(node)) { |
| 44 | + symbolMap.get(node).add(ne); |
| 45 | + } |
| 46 | + else { |
| 47 | + List<Node> newList = new ArrayList<>(); |
| 48 | + newList.add(ne); |
| 49 | + symbolMap.put(node, newList); |
| 50 | + } |
| 51 | + |
| 52 | + }); |
| 53 | + |
| 54 | + 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); |
| 65 | + |
| 66 | + fixNameToLower(child.toString()).ifPresent(name -> { |
| 67 | + ((VariableDeclarator)k).setName(name); |
| 68 | + |
| 69 | + for (Node n : v) { |
| 70 | + ((NameExpr)n).setName(name); |
| 71 | + } |
| 72 | + |
| 73 | + TokenRange tokenRange = k.getTokenRange().orElseThrow(() -> new RuntimeException("cannot get range")); |
| 74 | + list.add(new SourceProblem("s5.2.7-local-variable-names", tokenRange)); |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + 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)); |
| 84 | + } |
| 85 | + |
| 86 | + else return Optional.empty(); |
| 87 | + } |
| 88 | + |
| 89 | + public static void main(String[] args) throws IOException, SourceException { |
| 90 | + String path = SourceFormat.class.getResource("/example2.java").getPath(); |
| 91 | + path = path.substring(1); |
| 92 | + |
| 93 | + List<SourceProblem> problems = new ArrayList<>(); |
| 94 | + CompilationUnit cu = SourceParser.parseFile(Paths.get(path)); |
| 95 | + naming(cu, problems); |
| 96 | + |
| 97 | + problems.forEach(p -> System.out.println(p)); |
| 98 | + |
| 99 | + FileWriter fileWriter = new FileWriter("generated/example2.java"); |
| 100 | + fileWriter.write(cu.toString()); |
| 101 | + fileWriter.close(); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments