[Spoon-discuss] How to remove a method?

Marcelino Rodriguez cancio marcelino.rodriguez-cancio at irisa.fr
Thu Feb 26 10:42:54 CET 2015


Carsten,

What we do (perhaps there is a better way ) is to replace the code fragment by an empty one. :

/**
 * Replaces a method for a comment. Leave the comment blank to simply delete the method.
 */
private void deleteMethod(CtMethod m, String comment) {
  CtCodeSnippetStatement comment = new CtCodeSnippetStatementImpl();
  comment.setValue("//" + comment);
  m.replace(comment);
}

If you want the source code without the method simply pretty print the Compilation unit of the method:

CtSimpleType<?> type = m.cf.getCompilationUnit().getMainType();
Factory factory = type.getFactory();
Environment env = factory.getEnvironment();
JavaOutputProcessor processor = new JavaOutputProcessor(new File(directory), new DefaultJavaPrettyPrinter(env));
processor.setFactory(factory);
processor.createJavaFile(type);

Best
Marcelino






----- Mail original -----
> De: "Carsten Schmied" <carsten.schmied at gmail.com>
> À: spoon-discuss at lists.gforge.inria.fr
> Envoyé: Mercredi 25 Février 2015 22:18:09
> Objet: [Spoon-discuss] How to remove a method?
> 
> Hi,
> 
> how can I remove a method from a class programmatically? How can I
> remove a constructor?
> How can I remove unused imports, fields, etc.?
> 
> I am in general interested in as many examples as possible showing me
> how to remove parts from a class, how to build a class from the ground
> up (maybe there exists a comprehensive example?).
> 
> Thanks in advance,
> Carsten
> _______________________________________________
> Spoon-discuss mailing list
> Spoon-discuss at lists.gforge.inria.fr
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/spoon-discuss
>


More information about the Spoon-discuss mailing list