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

29
LINES

< > BotCompany Repo | #1004069 // renameSourceInfoInClassFile

JavaX fragment (include)

!include once #1019934 // BCEL

import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.SourceFile;
import org.apache.bcel.generic.*;

static void renameSourceInfoInClassFile(File classFile, S newName) {
  JavaClass c = new ClassParser(classFile.getPath(), classFile.getName()).parse();
  //print(c.getSourceFileName());
  
  ClassGen g = new ClassGen(c);
  
  int nameIndex = g.getConstantPool().addUtf8(newName);
  
  for (Attribute attr : g.getAttributes())
    if (attr instanceof SourceFile) {
      g.removeAttribute(attr);
      break;
    }
    
  g.addAttribute(new SourceFile(
    g.getConstantPool().addUtf8("SourceFile"), 
    2,
    nameIndex,
    g.getConstantPool().getConstantPool()));
      
  c = g.getJavaClass();
  c.dump(classFile); // overwrite!
}

Author comment

Began life as a copy of #1004056

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004069
Snippet name: renameSourceInfoInClassFile
Eternal ID of this version: #1004069/2
Text MD5: 2a98ec057555a5584720cba5359563f3
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-12-01 15:52:34
Source code size: 818 bytes / 29 lines
Pitched / IR pitched: No / No
Views / Downloads: 492 / 525
Version history: 1 change(s)
Referenced in: [show references]