@Override public String toString() { DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(getFactory().getEnvironment()); String errorMessage = ""; try { // we do not want to compute imports of for CtImport and CtReference // as it may change the print of a reference if (!(this instanceof CtImport) && !(this instanceof CtReference)) { printer.getImportsContext().computeImports(this); } printer.scan(this); } catch (ParentNotInitializedException ignore) { LOGGER.error(ERROR_MESSAGE_TO_STRING, ignore); errorMessage = ERROR_MESSAGE_TO_STRING; } // in line-preservation mode, newlines are added at the beginning to matches the lines // removing them from the toString() representation return printer.toString().replaceFirst("^\\s+", "") + errorMessage; }