/** * Get the source code for a CtClass element * * @param anElement * @return as a string the source ready for compilation */ public static String generateAssociatedClassContent(CtClass<?> anElement) { DefaultJavaPrettyPrinter prettyPrinter = new DefaultJavaPrettyPrinter(new StandardEnvironment()); prettyPrinter.scan(anElement); String sourceCode = ("package " + anElement.getPackage().getQualifiedName() + "; " + prettyPrinter.toString()); return sourceCode; }
@Override public String toString() { DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(getFactory().getEnvironment()); String errorMessage = ""; try { printer.computeImports(this); printer.scan(this); } catch (ParentNotInitializedException ignore) { errorMessage = ERROR_MESSAGE_TO_STRING; } return printer.toString() + errorMessage; }
public String getCode() { if (astElement != null) { try { if (printer == null) return astElement.toString(); printer.reset(); printer.scan(astElement); return printer.toString(); } catch (NullPointerException ex) { throw new NullPointerException( "Unable to get the code. Code field was empty and astElement.toString() throw null"); } } return code; }