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

62
LINES

< > BotCompany Repo | #1011234 // javaParser_makeAllPublic (OLD) - also pretty-prints whole source

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

Transpiled version (8117L) is out of date.

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);
  }
  
  @Override
  public void visit(EnumDeclaration n, O arg) {
    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  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1011234
Snippet name: javaParser_makeAllPublic (OLD) - also pretty-prints whole source
Eternal ID of this version: #1011234/14
Text MD5: b7d4b73bc0048ee9721e38076e05696f
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:09
Source code size: 2376 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 572 / 622
Version history: 13 change(s)
Referenced in: [show references]