Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

75
LINES

< > BotCompany Repo | #1025811 // javaParser_makeAllPublic - also pretty-prints whole source [new JavaParser version]

JavaX fragment (include) [tags: use-pretranspiled]

Uses 1176K of libraries. Click here for Pure Java version (9384L/51K).

1  
!include once #1025810 // JavaParser
2  
!include once #1025812 // javaParseCompilationUnit
3  
4  
import com.github.javaparser.ast.NodeList;
5  
import com.github.javaparser.ast.body.MethodDeclaration;
6  
import com.github.javaparser.ast.body.EnumDeclaration;
7  
import com.github.javaparser.ast.stmt.LocalClassDeclarationStmt;
8  
import com.github.javaparser.printer.*;
9  
10  
static S javaParser_makeAllPublic(S src, O... _) {
11  
  CompilationUnit cu = javaParseCompilationUnit(src);
12  
  new PrettyPrinterConfiguration ppconf;
13  
  ppconf.setIndentSize(2);
14  
  ppconf.setIndentType(PrettyPrinterConfiguration.IndentType.SPACES);
15  
  ppconf.setPrintComments(false);
16  
  new javaParser_makeAllPublic_Visitor visitor;
17  
  visitor.notTopLevelClassDecl = boolOptPar notTopLevelClassDecl(_);
18  
  visitor.visit(cu, null);
19  
  ret cu.toString(ppconf);
20  
}
21  
22  
sclass javaParser_makeAllPublic_Visitor extends VoidVisitorAdapter {
23  
  bool notTopLevelClassDecl; // don't make top-level class declaration public
24  
  
25  
  delegate Modifier to com.github.javaparser.ast.
26  
  
27  
  void makePublic(NodeList<Modifier> modifiers) {
28  
    // XXX: does this work?
29  
    modifiers.remove(Modifier.privateModifier());
30  
    modifiers.remove(Modifier.protectedModifier());
31  
    if (!modifiers.contains(Modifier.publicModifier()))
32  
      modifiers.add(Modifier.publicModifier());
33  
  }
34  
35  
  public void visit(ClassOrInterfaceDeclaration n, O arg) {
36  
    Node parent = n.getParentNode().get();
37  
    if (!(parent instanceof LocalClassDeclarationStmt
38  
      || parent instanceof CompilationUnit && neq(n.getName().asString(), "main")))
39  
      if (notTopLevelClassDecl)
40  
        notTopLevelClassDecl = false;
41  
      else
42  
        makePublic(n.getModifiers());
43  
    super.visit(n, arg);
44  
  }
45  
  
46  
  @Override
47  
  public void visit(EnumDeclaration n, O arg) {
48  
    makePublic(n.getModifiers());
49  
    super.visit(n, arg);
50  
  }  
51  
  
52  
  public void visit(FieldDeclaration n, O arg) {
53  
    makePublic(n.getModifiers());
54  
    super.visit(n, arg);
55  
  }
56  
  
57  
  public void visit(ConstructorDeclaration n, O arg) {
58  
    Node parent = n.getParentNode().get();
59  
    if (!parent instanceof EnumDeclaration)
60  
      makePublic(n.getModifiers());
61  
    super.visit(n, arg);
62  
  }
63  
  
64  
  public void visit(MethodDeclaration n, O arg) {
65  
    NodeList<Modifier> m = n.getModifiers();
66  
    //print("Method found: " + n.getName() + " with modifiers: " + m + ", position: " + n.getRange()->begin);
67  
    if (!m.contains(Modifier.privateModifier())) {
68  
      makePublic(m);
69  
    } /*else
70  
      if (!m.contains(Modifier.staticModifier()) && !m.contains(Modifier.finalModifier()))
71  
        m.add(Modifier.finalModifier());*/
72  
        
73  
    super.visit(n, arg);
74  
  }
75  
}

Author comment

Began life as a copy of #1011234

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, wnsclhtenguj

No comments. add comment

Snippet ID: #1025811
Snippet name: javaParser_makeAllPublic - also pretty-prints whole source [new JavaParser version]
Eternal ID of this version: #1025811/16
Text MD5: f1c70b098ae7437683f8fae244005811
Transpilation MD5: f79ef96782cb84104e24210c198a80e6
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-16 21:25:30
Source code size: 2675 bytes / 75 lines
Pitched / IR pitched: No / No
Views / Downloads: 172 / 1550
Version history: 15 change(s)
Referenced in: [show references]