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).

!include once #1025810 // JavaParser
!include once #1025812 // javaParseCompilationUnit

import com.github.javaparser.ast.NodeList;
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.setIndentSize(2);
  ppconf.setIndentType(PrettyPrinterConfiguration.IndentType.SPACES);
  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
  
  delegate Modifier to com.github.javaparser.ast.
  
  void makePublic(NodeList<Modifier> modifiers) {
    // XXX: does this work?
    modifiers.remove(Modifier.privateModifier());
    modifiers.remove(Modifier.protectedModifier());
    if (!modifiers.contains(Modifier.publicModifier()))
      modifiers.add(Modifier.publicModifier());
  }

  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) {
    NodeList<Modifier> m = n.getModifiers();
    //print("Method found: " + n.getName() + " with modifiers: " + m + ", position: " + n.getRange()->begin);
    if (!m.contains(Modifier.privateModifier())) {
      makePublic(m);
    } /*else
      if (!m.contains(Modifier.staticModifier()) && !m.contains(Modifier.finalModifier()))
        m.add(Modifier.finalModifier());*/
        
    super.visit(n, arg);
  }
}

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: 245 / 1638
Version history: 15 change(s)
Referenced in: [show references]