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

56
LINES

< > BotCompany Repo | #1011234 - javaParser_makeAllPublic - also pretty-prints whole source

JavaX fragment (include)

import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.stmt.LocalClassDeclarationStmt;
import com.github.javaparser.printer.*;

static S javaParser_makeAllPublic(S src, O... _) {
  CompilationUnit cu = javaParseCompilationUnit(src);
  new PrettyPrinterConfiguration ppconf;
  ppconf.setIndent("  ");
  ppconf.setPrintComments(false);
  new javaParser_makeAllPublic_Visitor visitor;
  visitor.notTopLevelClassDecl = boolOptPar notTopLevelClassDecl(_);
  visitor.visit(cu, null);
  ret cu.toString(ppconf);
}

sclass javaParser_makeAllPublic_Visitor extends VoidVisitorAdapter {
  bool notTopLevelClassDecl; // don't make top-level class declaration public
  
  void makePublic(Set<com.github.javaparser.ast.Modifier> modifiers) {
    modifiers.remove(com.github.javaparser.ast.Modifier.PRIVATE);
    modifiers.remove(com.github.javaparser.ast.Modifier.PROTECTED);
    modifiers.add(com.github.javaparser.ast.Modifier.PUBLIC);
  }

  public void visit(ClassOrInterfaceDeclaration n, O arg) {
    Node parent = n.getParentNode().get();
    if (!(parent instanceof LocalClassDeclarationStmt
      || parent instanceof CompilationUnit && neq(n.getName().asString(), "main")))
      if (notTopLevelClassDecl)
        notTopLevelClassDecl = false;
      else
        makePublic(n.getModifiers());
    super.visit(n, arg);
  }
  
  public void visit(FieldDeclaration n, O arg) {
    makePublic(n.getModifiers());
    super.visit(n, arg);
  }
  
  public void visit(ConstructorDeclaration n, O arg) {
    Node parent = n.getParentNode().get();
    if (!parent instanceof EnumDeclaration)
      makePublic(n.getModifiers());
    super.visit(n, arg);
  }
  
  public void visit(MethodDeclaration n, O arg) {
    EnumSet<com.github.javaparser.ast.Modifier> m = n.getModifiers();
    //print("Method found: " + n.getName() + " with modifiers: " + m + ", position: " + n.getRange()->begin);
    if (m.contains(com.github.javaparser.ast.Modifier.PRIVATE) && !m.contains(com.github.javaparser.ast.Modifier.STATIC)) m.add(com.github.javaparser.ast.Modifier.FINAL);
    makePublic(m);
    super.visit(n, arg);
  }
}

Author comment

Began life as a copy of #1011232

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1011234
Snippet name: javaParser_makeAllPublic - also pretty-prints whole source
Eternal ID of this version: #1011234/12
Text MD5: d33814999dfe1f62b05348066048767c
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-01-19 17:37:36
Source code size: 2242 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 262 / 266
Version history: 11 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1011234 & http://1011234.tinybrain.de