diff options
Diffstat (limited to 'javaparser-testing/src/test/test_sourcecode')
2 files changed, 1806 insertions, 0 deletions
diff --git a/javaparser-testing/src/test/test_sourcecode/com/github/javaparser/printer/JavaConcepts.java b/javaparser-testing/src/test/test_sourcecode/com/github/javaparser/printer/JavaConcepts.java new file mode 100644 index 000000000..2a53e5f5b --- /dev/null +++ b/javaparser-testing/src/test/test_sourcecode/com/github/javaparser/printer/JavaConcepts.java @@ -0,0 +1,393 @@ +package japa.bdd.samples; + +import com.github.javaparser.JavaParser; +import japa.parser.ParseException; +import com.github.javaparser.ast.CompilationUnit; +import org.junit.Ignore; + +import java.io.*; +import java.util.*; + +@Ignore +@Deprecated +public class JavaConcepts<T extends List<int[]>, X> extends Base implements Serializable { + + static Class clz1 = String.class; + + protected Class clz2 = (String.class); + + Class clz3 = int.class; + + Class clz4 = (int.class); + + int[] arr = new int[10]; + + byte bye = 0; + + byte byebye[] = null; + + short sh1, sh2 = 1; + + int intWithUnderscore = 1234_5678; + + long longWithUnderscore = 1234_5678L; + + float floatWithUnderscore = 1_234.5_678f; + + float floatWithUnderscoreAndExponent = 1_234e1_0f; + + double doubleWithUnderscore = 1_234.5_678; + + double doubleWithUnderscoreAndExponent = 1_234e1_0; + + int binaryLiteral = 0b101101; + + List<String>[][] arrLS = (List<String>[][]) new List<?>[10][]; + + { + int z = 0, y = 0; + int a = (z) + y; + a = (+z) + y; + byte b = (byte) +y; + } + + List<String> diamond1 = new LinkedList<>(); + + @Deprecated() + static class Ugly { + + static int x = 0; + + public static void main(String[] args) { + x = +x; + x = ~x; + --x; + boolean b = !false; + x &= 2; + x |= 2; + x ^= 2; + x -= 2; + x %= 2; + x /= 2; + x *= 2; + x <<= 2; + x >>= 2; + x >>>= 2; + b = b || false; + b = b | false; + b = b & false; + b = b ^ false; + b = b != false; + b = x > 1; + b = x < 1; + b = x >= 1; + b = x <= 1; + x = x << 1; + x = x >> 1; + x = x >>> 1; + x = x - 1; + x = x * 1; + x = x % 1; + x = x / 1; + } + } + + @Deprecated() + int[][][][] arr2 = new int[10][2][1][0]; + + volatile float fff = 0x1.fffeP+127f; + + char cc = 'a'; + + int[][] arr3 = {{1, 2}, {3, 4}}; + + static int[] arr4[] = {}; + + public static JavaConcepts t; + + static { + arr4 = new int[][]{{2}, {1}}; + } + + { + arr3 = new int[][]{{2}, {1}}; + } + + public enum Teste { + + asc, def + } + + public enum Sexo { + + m, @Deprecated + f; + + public enum Sexo_ implements Serializable, Cloneable { + } + + private Sexo() { + } + } + + @Deprecated + public enum Enum { + + m(1) { + @Override + void mm() { + } + }, f(2) { + void mm() { + } + }; + + native void nnn(); + + transient int x; + + private Enum(int x) { + this.x = x; + } + + abstract void mm(); + } + + strictfp double ddd() { + return 0.0; + } + + public <T, E> JavaConcepts(int x) { + this.arr[0] = x; + T val1 = null; + E val2 = null; + super.<T, E>check2(val1, val2); + boolean b = true, y = false; + abstract class X { + + int i = 0; + + public <D> X() { + } + + public void m() { + } + } + @Deprecated + final class Y extends X { + + public Y() { + super(); + JavaConcepts.this.cc = 'c'; + super.i = 1; + Y.super.m(); + } + + public Y(int y) { + super(); + } + + public Y(long x) { + this(); + } + } + } + + public <T> JavaConcepts(String str) { + } + + private class QWE extends JavaConcepts<List<int[]>, String> { + + @Deprecated + final int z = 0; + + int i = (int) -1; + + public QWE(String... x) { + <String>super(x[0]); + } + + public QWE(int... x) { + super(x[0]); + i = x[0]; + assert true; + assert 1 == 1 : 2; + { + int iii = 3; + iii += 3; + } + label: + { + int iii = 1; + } + ; + ; + int min = -2147483648; + long sl = 123123123123l; + long minl = -9223372036854775808L; + switch (i) { + } + ll: + switch (i) { + case 1: + System.out.println(1); + break ll; + default: { + System.out.println("default"); + break; + } + case 2: + if (t instanceof Base) { + System.out.println(1); + } + i++; + ++i; + } + } + + private synchronized int doSomething()[] { + List<? extends Number> x = new ArrayList<Integer>(); + return new int[]{1}; + } + } + + public static void main(String[] args) throws ParseException, IOException { + int x = 2; + CompilationUnit cu = parse(new File("src/japa/parser/javacc/Parser.java")); + System.out.println(cu); + JavaConcepts teste = new JavaConcepts(2); + JavaConcepts.QWE qwe = teste.new QWE(1); + if (1 + 1 == 2) { + teste = null; + teste = new JavaConcepts(1); + } else { + x = 3; + teste = new JavaConcepts(1); + x = x == 0 ? 2 : 4; + } + if (true) + x = 1; + else + x = 3; + if (true) + x = 1; + else if (false) + x = 3; + else + x = 2; + while (true) { + xxx: + while (x == 3) continue xxx; + break; + } + do { + x++; + } while (x < 100); + do x++; while (x < 100); + for (@Deprecated int i : arr4[0]) { + x--; + } + for (@Deprecated final int i = 0, j = 1; i < 10; x++) { + break; + } + int i, j; + for (i = 0, j = 1; i < 10 && j < 2; i++, j--) { + break; + } + } + + public static CompilationUnit parse(@Deprecated File file) throws ParseException, IOException { + String a = ((String) "qwe"); + String x = ((String) clz1.getName()); + int y = ((Integer) (Object) x).intValue(); + synchronized (file) { + file = null; + file = new File(""); + } + try { + if (file == null) { + throw new NullPointerException("blah"); + } + } catch (final NullPointerException e) { + System.out.println("catch"); + } catch (RuntimeException e) { + System.out.println("catch"); + } finally { + System.out.println("finally"); + } + try { + if (file == null) { + throw new NullPointerException("blah"); + } + } finally { + System.out.println("finally"); + } + try { + if (file == null) { + throw new NullPointerException("blah"); + } + } catch (RuntimeException e) { + System.out.println("catch"); + } + try (InputStream in = createInputStream()) { + System.out.println(in); + } catch (IOException e) { + System.out.println("catch"); + } + try (InputStream in = createInputStream(); + InputStream in2 = createInputStream()) { + System.out.println(in); + } catch (IOException e) { + System.out.println("catch"); + } + try (InputStream in = createInputStream()) { + System.out.println(in); + } + try { + System.out.println("whatever"); + } catch (RuntimeException e) { + System.out.println(e); + } catch (final Exception | Error e) { + System.out.println(e); + } + return JavaParser.parse(file); + } + + class A<T extends Integer & Serializable> implements XXX, Serializable { + + public <ABC> A(Integer integer, ABC string) throws Exception, IOException { + } + } + + private <Y> void x(Map<? extends X, ? super T> x) { + @Deprecated Comparator c = new Comparator() { + + public int compare(Object o1, Object o2) { + try { + A<Integer> a = new <String>A<Integer>(new Integer(11), "foo") { + }; + } catch (Exception e) { + } + return 0; + } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + }; + } + + private static InputStream createInputStream() { + return new ByteArrayInputStream(null); + } +} + +class Base { + + public <A, B> void check2(A val1, B val2) { + } +} + +interface XXX extends Serializable, Cloneable { +} diff --git a/javaparser-testing/src/test/test_sourcecode/com/github/javaparser/printer/PrettyPrintVisitor.java b/javaparser-testing/src/test/test_sourcecode/com/github/javaparser/printer/PrettyPrintVisitor.java new file mode 100644 index 000000000..4a4c9f07c --- /dev/null +++ b/javaparser-testing/src/test/test_sourcecode/com/github/javaparser/printer/PrettyPrintVisitor.java @@ -0,0 +1,1413 @@ +/* + * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2016 The JavaParser Team. + * + * This file is part of JavaParser. + * + * JavaParser can be used either under the terms of + * a) the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * b) the terms of the Apache License + * + * You should have received a copy of both licenses in LICENCE.LGPL and + * LICENCE.APACHE. Please refer to those files for details. + * + * JavaParser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +package com.github.javaparser.printer; + +import com.github.javaparser.ast.*; +import com.github.javaparser.ast.body.*; +import com.github.javaparser.ast.comments.BlockComment; +import com.github.javaparser.ast.comments.Comment; +import com.github.javaparser.ast.comments.JavadocComment; +import com.github.javaparser.ast.comments.LineComment; +import com.github.javaparser.ast.expr.*; +import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments; +import com.github.javaparser.ast.nodeTypes.NodeWithVariables; +import com.github.javaparser.ast.stmt.*; +import com.github.javaparser.ast.type.*; +import com.github.javaparser.ast.visitor.VoidVisitor; + +import java.util.EnumSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import static com.github.javaparser.utils.PositionUtils.sortByBeginPosition; +import static com.github.javaparser.utils.Utils.isNullOrEmpty; + +/** + * Outputs the AST as formatted Java source code. + * + * @author Julio Vilmar Gesser + */ +public class PrettyPrintVisitor implements VoidVisitor<Void> { + protected final PrettyPrinterConfiguration configuration; + protected final SourcePrinter printer; + + public PrettyPrintVisitor(PrettyPrinterConfiguration prettyPrinterConfiguration) { + configuration = prettyPrinterConfiguration; + printer = new SourcePrinter(configuration.getIndent()); + } + + public String getSource() { + return printer.getSource(); + } + + private void printModifiers(final EnumSet<Modifier> modifiers) { + if (modifiers.size() > 0) { + printer.print(modifiers.stream().map(Modifier::asString).collect(Collectors.joining(" ")) + " "); + } + } + + private void printMembers(final NodeList<BodyDeclaration<?>> members, final Void arg) { + for (final BodyDeclaration<?> member : members) { + printer.println(); + member.accept(this, arg); + printer.println(); + } + } + + private void printMemberAnnotations(final NodeList<AnnotationExpr> annotations, final Void arg) { + if (annotations.isEmpty()) { + return; + } + for (final AnnotationExpr a : annotations) { + a.accept(this, arg); + printer.println(); + } + } + + private void printAnnotations(final NodeList<AnnotationExpr> annotations, boolean prefixWithASpace, + final Void arg) { + if (annotations.isEmpty()) { + return; + } + if (prefixWithASpace) { + printer.print(" "); + } + for (AnnotationExpr annotation : annotations) { + annotation.accept(this, arg); + printer.print(" "); + } + } + + private void printTypeArgs(final NodeWithTypeArguments<?> nodeWithTypeArguments, final Void arg) { + NodeList<Type> typeArguments = nodeWithTypeArguments.getTypeArguments().orElse(null); + if (!isNullOrEmpty(typeArguments)) { + printer.print("<"); + for (final Iterator<Type> i = typeArguments.iterator(); i.hasNext(); ) { + final Type t = i.next(); + t.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + printer.print(">"); + } + } + + private void printTypeParameters(final NodeList<TypeParameter> args, final Void arg) { + if (!isNullOrEmpty(args)) { + printer.print("<"); + for (final Iterator<TypeParameter> i = args.iterator(); i.hasNext(); ) { + final TypeParameter t = i.next(); + t.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + printer.print(">"); + } + } + + private void printArguments(final NodeList<Expression> args, final Void arg) { + printer.print("("); + if (!isNullOrEmpty(args)) { + for (final Iterator<Expression> i = args.iterator(); i.hasNext(); ) { + final Expression e = i.next(); + e.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + printer.print(")"); + } + + private void printJavaComment(final Comment javacomment, final Void arg) { + if (javacomment != null) { + javacomment.accept(this, arg); + } + } + + @Override + public void visit(final CompilationUnit n, final Void arg) { + printJavaComment(n.getComment(), arg); + + if (n.getPackageDeclaration().isPresent()) { + n.getPackageDeclaration().get().accept(this, arg); + } + + n.getImports().accept(this, arg); + if (!n.getImports().isEmpty()) { + printer.println(); + } + + for (final Iterator<TypeDeclaration<?>> i = n.getTypes().iterator(); i.hasNext(); ) { + i.next().accept(this, arg); + printer.println(); + if (i.hasNext()) { + printer.println(); + } + } + + printOrphanCommentsEnding(n); + } + + @Override + public void visit(final PackageDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + printAnnotations(n.getAnnotations(), false, arg); + printer.print("package "); + n.getName().accept(this, arg); + printer.println(";"); + printer.println(); + + printOrphanCommentsEnding(n); + } + + @Override + public void visit(final NameExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getName().accept(this, arg); + + printOrphanCommentsEnding(n); + } + + @Override + public void visit(final Name n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.getQualifier().isPresent()) { + n.getQualifier().get().accept(this, arg); + printer.print("."); + } + printer.print(n.getIdentifier()); + + printOrphanCommentsEnding(n); + } + + @Override + public void visit(SimpleName n, Void arg) { + printer.print(n.getIdentifier()); + } + + @Override + public void visit(final ClassOrInterfaceDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + printMemberAnnotations(n.getAnnotations(), arg); + printModifiers(n.getModifiers()); + + if (n.isInterface()) { + printer.print("interface "); + } else { + printer.print("class "); + } + + n.getName().accept(this, arg); + + printTypeParameters(n.getTypeParameters(), arg); + + if (!n.getExtendedTypes().isEmpty()) { + printer.print(" extends "); + for (final Iterator<ClassOrInterfaceType> i = n.getExtendedTypes().iterator(); i.hasNext(); ) { + final ClassOrInterfaceType c = i.next(); + c.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + + if (!n.getImplementedTypes().isEmpty()) { + printer.print(" implements "); + for (final Iterator<ClassOrInterfaceType> i = n.getImplementedTypes().iterator(); i.hasNext(); ) { + final ClassOrInterfaceType c = i.next(); + c.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + + printer.println(" {"); + printer.indent(); + if (!isNullOrEmpty(n.getMembers())) { + printMembers(n.getMembers(), arg); + } + + printOrphanCommentsEnding(n); + + printer.unindent(); + printer.print("}"); + } + + @Override + public void visit(final JavadocComment n, final Void arg) { + printer.print("/**"); + printer.print(n.getContent()); + printer.println("*/"); + } + + @Override + public void visit(final ClassOrInterfaceType n, final Void arg) { + printJavaComment(n.getComment(), arg); + + if (n.getScope().isPresent()) { + n.getScope().get().accept(this, arg); + printer.print("."); + } + for (AnnotationExpr ae : n.getAnnotations()) { + ae.accept(this, arg); + printer.print(" "); + } + + n.getName().accept(this, arg); + + if (n.isUsingDiamondOperator()) { + printer.print("<>"); + } else { + printTypeArgs(n, arg); + } + } + + @Override + public void visit(final TypeParameter n, final Void arg) { + printJavaComment(n.getComment(), arg); + for (AnnotationExpr ann : n.getAnnotations()) { + ann.accept(this, arg); + printer.print(" "); + } + n.getName().accept(this, arg); + if (!isNullOrEmpty(n.getTypeBound())) { + printer.print(" extends "); + for (final Iterator<ClassOrInterfaceType> i = n.getTypeBound().iterator(); i.hasNext(); ) { + final ClassOrInterfaceType c = i.next(); + c.accept(this, arg); + if (i.hasNext()) { + printer.print(" & "); + } + } + } + } + + @Override + public void visit(final PrimitiveType n, final Void arg) { + printJavaComment(n.getComment(), arg); + printAnnotations(n.getAnnotations(), true, arg); + printer.print(n.getType().asString()); + } + + @Override + public void visit(final ArrayType n, final Void arg) { + final List<ArrayType> arrayTypeBuffer = new LinkedList<>(); + Type type = n; + while (type instanceof ArrayType) { + final ArrayType arrayType = (ArrayType) type; + arrayTypeBuffer.add(arrayType); + type = arrayType.getComponentType(); + } + + type.accept(this, arg); + for (ArrayType arrayType : arrayTypeBuffer) { + printAnnotations(arrayType.getAnnotations(), true, arg); + printer.print("[]"); + } + } + + @Override + public void visit(final ArrayCreationLevel n, final Void arg) { + printAnnotations(n.getAnnotations(), true, arg); + printer.print("["); + if (n.getDimension().isPresent()) { + n.getDimension().get().accept(this, arg); + } + printer.print("]"); + } + + @Override + public void visit(final IntersectionType n, final Void arg) { + printJavaComment(n.getComment(), arg); + printAnnotations(n.getAnnotations(), false, arg); + boolean isFirst = true; + for (ReferenceType element : n.getElements()) { + element.accept(this, arg); + if (isFirst) { + isFirst = false; + } else { + printer.print(" & "); + } + } + } + + @Override + public void visit(final UnionType n, final Void arg) { + printJavaComment(n.getComment(), arg); + printAnnotations(n.getAnnotations(), true, arg); + boolean isFirst = true; + for (ReferenceType element : n.getElements()) { + if (isFirst) { + isFirst = false; + } else { + printer.print(" | "); + } + element.accept(this, arg); + } + } + + @Override + public void visit(final WildcardType n, final Void arg) { + printJavaComment(n.getComment(), arg); + printAnnotations(n.getAnnotations(), false, arg); + printer.print("?"); + if (n.getExtendedTypes().isPresent()) { + printer.print(" extends "); + n.getExtendedTypes().get().accept(this, arg); + } + if (n.getSuperTypes().isPresent()) { + printer.print(" super "); + n.getSuperTypes().get().accept(this, arg); + } + } + + @Override + public void visit(final UnknownType n, final Void arg) { + // Nothing to print + } + + @Override + public void visit(final FieldDeclaration n, final Void arg) { + printOrphanCommentsBeforeThisChildNode(n); + + printJavaComment(n.getComment(), arg); + printMemberAnnotations(n.getAnnotations(), arg); + printModifiers(n.getModifiers()); + if (!n.getVariables().isEmpty()) { + n.getMaximumCommonType().accept(this, arg); + } + + printer.print(" "); + for (final Iterator<VariableDeclarator> i = n.getVariables().iterator(); i.hasNext(); ) { + final VariableDeclarator var = i.next(); + var.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + + printer.print(";"); + } + + @Override + public void visit(final VariableDeclarator n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getName().accept(this, arg); + + Type commonType = n.getAncestorOfType(NodeWithVariables.class).get().getMaximumCommonType(); + + Type type = n.getType(); + + ArrayType arrayType = null; + + for (int i = commonType.getArrayLevel(); i < type.getArrayLevel(); i++) { + if (arrayType == null) { + arrayType = (ArrayType) type; + } else { + arrayType = (ArrayType) arrayType.getComponentType(); + } + printAnnotations(arrayType.getAnnotations(), true, arg); + printer.print("[]"); + } + + if (n.getInitializer().isPresent()) { + printer.print(" = "); + n.getInitializer().get().accept(this, arg); + } + } + + @Override + public void visit(final ArrayInitializerExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("{"); + if (!isNullOrEmpty(n.getValues())) { + printer.print(" "); + for (final Iterator<Expression> i = n.getValues().iterator(); i.hasNext(); ) { + final Expression expr = i.next(); + expr.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + printer.print(" "); + } + printer.print("}"); + } + + @Override + public void visit(final VoidType n, final Void arg) { + printJavaComment(n.getComment(), arg); + printAnnotations(n.getAnnotations(), false, arg); + printer.print("void"); + } + + @Override + public void visit(final ArrayAccessExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getName().accept(this, arg); + printer.print("["); + n.getIndex().accept(this, arg); + printer.print("]"); + } + + @Override + public void visit(final ArrayCreationExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("new "); + n.getElementType().accept(this, arg); + for (ArrayCreationLevel level : n.getLevels()) { + level.accept(this, arg); + } + if (n.getInitializer().isPresent()) { + printer.print(" "); + n.getInitializer().get().accept(this, arg); + } + } + + @Override + public void visit(final AssignExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getTarget().accept(this, arg); + printer.print(" "); + printer.print(n.getOperator().asString()); + printer.print(" "); + n.getValue().accept(this, arg); + } + + @Override + public void visit(final BinaryExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getLeft().accept(this, arg); + printer.print(" "); + printer.print(n.getOperator().asString()); + printer.print(" "); + n.getRight().accept(this, arg); + } + + @Override + public void visit(final CastExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("("); + n.getType().accept(this, arg); + printer.print(") "); + n.getExpression().accept(this, arg); + } + + @Override + public void visit(final ClassExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getType().accept(this, arg); + printer.print(".class"); + } + + @Override + public void visit(final ConditionalExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getCondition().accept(this, arg); + printer.print(" ? "); + n.getThenExpr().accept(this, arg); + printer.print(" : "); + n.getElseExpr().accept(this, arg); + } + + @Override + public void visit(final EnclosedExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("("); + if (n.getInner().isPresent()) { + n.getInner().get().accept(this, arg); + } + printer.print(")"); + } + + @Override + public void visit(final FieldAccessExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.getScope().isPresent()) + n.getScope().get().accept(this, arg); + printer.print("."); + n.getName().accept(this, arg); + } + + @Override + public void visit(final InstanceOfExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getExpression().accept(this, arg); + printer.print(" instanceof "); + n.getType().accept(this, arg); + } + + @Override + public void visit(final CharLiteralExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("'"); + printer.print(n.getValue()); + printer.print("'"); + } + + @Override + public void visit(final DoubleLiteralExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print(n.getValue()); + } + + @Override + public void visit(final IntegerLiteralExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print(n.getValue()); + } + + @Override + public void visit(final LongLiteralExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print(n.getValue()); + } + + @Override + public void visit(final StringLiteralExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("\""); + printer.print(n.getValue()); + printer.print("\""); + } + + @Override + public void visit(final BooleanLiteralExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print(String.valueOf(n.getValue())); + } + + @Override + public void visit(final NullLiteralExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("null"); + } + + @Override + public void visit(final ThisExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.getClassExpr().isPresent()) { + n.getClassExpr().get().accept(this, arg); + printer.print("."); + } + printer.print("this"); + } + + @Override + public void visit(final SuperExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.getClassExpr().isPresent()) { + n.getClassExpr().get().accept(this, arg); + printer.print("."); + } + printer.print("super"); + } + + @Override + public void visit(final MethodCallExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.getScope().isPresent()) { + n.getScope().get().accept(this, arg); + printer.print("."); + } + printTypeArgs(n, arg); + n.getName().accept(this, arg); + printArguments(n.getArguments(), arg); + } + + @Override + public void visit(final ObjectCreationExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.getScope().isPresent()) { + n.getScope().get().accept(this, arg); + printer.print("."); + } + + printer.print("new "); + + printTypeArgs(n, arg); + if (!isNullOrEmpty(n.getTypeArguments().orElse(null))) { + printer.print(" "); + } + + n.getType().accept(this, arg); + + printArguments(n.getArguments(), arg); + + if (n.getAnonymousClassBody().isPresent()) { + printer.println(" {"); + printer.indent(); + printMembers(n.getAnonymousClassBody().get(), arg); + printer.unindent(); + printer.print("}"); + } + } + + @Override + public void visit(final UnaryExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.getOperator().isPrefix()) { + printer.print(n.getOperator().asString()); + } + + n.getExpression().accept(this, arg); + + if (n.getOperator().isPostfix()) { + printer.print(n.getOperator().asString()); + } + } + + @Override + public void visit(final ConstructorDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + printMemberAnnotations(n.getAnnotations(), arg); + printModifiers(n.getModifiers()); + + printTypeParameters(n.getTypeParameters(), arg); + if (n.isGeneric()) { + printer.print(" "); + } + n.getName().accept(this, arg); + + printer.print("("); + if (!n.getParameters().isEmpty()) { + for (final Iterator<Parameter> i = n.getParameters().iterator(); i.hasNext(); ) { + final Parameter p = i.next(); + p.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + printer.print(")"); + + if (!isNullOrEmpty(n.getThrownExceptions())) { + printer.print(" throws "); + for (final Iterator<ReferenceType> i = n.getThrownExceptions().iterator(); i.hasNext(); ) { + final ReferenceType name = i.next(); + name.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + printer.print(" "); + n.getBody().accept(this, arg); + } + + @Override + public void visit(final MethodDeclaration n, final Void arg) { + printOrphanCommentsBeforeThisChildNode(n); + + printJavaComment(n.getComment(), arg); + printMemberAnnotations(n.getAnnotations(), arg); + printModifiers(n.getModifiers()); + if (n.isDefault()) { + printer.print("default "); + } + printTypeParameters(n.getTypeParameters(), arg); + if (!isNullOrEmpty(n.getTypeParameters())) { + printer.print(" "); + } + + n.getType().accept(this, arg); + printer.print(" "); + n.getName().accept(this, arg); + + printer.print("("); + if (!isNullOrEmpty(n.getParameters())) { + for (final Iterator<Parameter> i = n.getParameters().iterator(); i.hasNext(); ) { + final Parameter p = i.next(); + p.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + printer.print(")"); + + if (!isNullOrEmpty(n.getThrownExceptions())) { + printer.print(" throws "); + for (final Iterator<ReferenceType> i = n.getThrownExceptions().iterator(); i.hasNext(); ) { + final ReferenceType name = i.next(); + name.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + if (!n.getBody().isPresent()) { + printer.print(";"); + } else { + printer.print(" "); + n.getBody().get().accept(this, arg); + } + } + + @Override + public void visit(final Parameter n, final Void arg) { + printJavaComment(n.getComment(), arg); + printAnnotations(n.getAnnotations(), false, arg); + printModifiers(n.getModifiers()); + if (n.getType() != null) { + n.getType().accept(this, arg); + } + if (n.isVarArgs()) { + printer.print("..."); + } + printer.print(" "); + n.getName().accept(this, arg); + } + + @Override + public void visit(final ExplicitConstructorInvocationStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.isThis()) { + printTypeArgs(n, arg); + printer.print("this"); + } else { + if (n.getExpression().isPresent()) { + n.getExpression().get().accept(this, arg); + printer.print("."); + } + printTypeArgs(n, arg); + printer.print("super"); + } + printArguments(n.getArguments(), arg); + printer.print(";"); + } + + @Override + public void visit(final VariableDeclarationExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printAnnotations(n.getAnnotations(), false, arg); + printModifiers(n.getModifiers()); + + if (!n.getVariables().isEmpty()) { + n.getMaximumCommonType().accept(this, arg); + } + printer.print(" "); + + for (final Iterator<VariableDeclarator> i = n.getVariables().iterator(); i.hasNext(); ) { + final VariableDeclarator v = i.next(); + v.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + + @Override + public void visit(final LocalClassDeclarationStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getClassDeclaration().accept(this, arg); + } + + @Override + public void visit(final AssertStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("assert "); + n.getCheck().accept(this, arg); + if (n.getMessage().isPresent()) { + printer.print(" : "); + n.getMessage().get().accept(this, arg); + } + printer.print(";"); + } + + @Override + public void visit(final BlockStmt n, final Void arg) { + printOrphanCommentsBeforeThisChildNode(n); + printJavaComment(n.getComment(), arg); + printer.println("{"); + if (n.getStatements() != null) { + printer.indent(); + for (final Statement s : n.getStatements()) { + s.accept(this, arg); + printer.println(); + } + printer.unindent(); + } + printOrphanCommentsEnding(n); + printer.print("}"); + + } + + @Override + public void visit(final LabeledStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getLabel().accept(this, arg); + printer.print(": "); + n.getStatement().accept(this, arg); + } + + @Override + public void visit(final EmptyStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print(";"); + } + + @Override + public void visit(final ExpressionStmt n, final Void arg) { + printOrphanCommentsBeforeThisChildNode(n); + printJavaComment(n.getComment(), arg); + n.getExpression().accept(this, arg); + printer.print(";"); + } + + @Override + public void visit(final SwitchStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("switch("); + n.getSelector().accept(this, arg); + printer.println(") {"); + if (n.getEntries() != null) { + printer.indent(); + for (final SwitchEntryStmt e : n.getEntries()) { + e.accept(this, arg); + } + printer.unindent(); + } + printer.print("}"); + + } + + @Override + public void visit(final SwitchEntryStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.getLabel().isPresent()) { + printer.print("case "); + n.getLabel().get().accept(this, arg); + printer.print(":"); + } else { + printer.print("default:"); + } + printer.println(); + printer.indent(); + if (n.getStatements() != null) { + for (final Statement s : n.getStatements()) { + s.accept(this, arg); + printer.println(); + } + } + printer.unindent(); + } + + @Override + public void visit(final BreakStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("break"); + n.getLabel().ifPresent(l -> printer.print(" ").print(l.getIdentifier())); + printer.print(";"); + } + + @Override + public void visit(final ReturnStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("return"); + if (n.getExpression().isPresent()) { + printer.print(" "); + n.getExpression().get().accept(this, arg); + } + printer.print(";"); + } + + @Override + public void visit(final EnumDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + printMemberAnnotations(n.getAnnotations(), arg); + printModifiers(n.getModifiers()); + + printer.print("enum "); + n.getName().accept(this, arg); + + if (!n.getImplementedTypes().isEmpty()) { + printer.print(" implements "); + for (final Iterator<ClassOrInterfaceType> i = n.getImplementedTypes().iterator(); i.hasNext(); ) { + final ClassOrInterfaceType c = i.next(); + c.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + + printer.println(" {"); + printer.indent(); + if (n.getEntries() != null) { + printer.println(); + for (final Iterator<EnumConstantDeclaration> i = n.getEntries().iterator(); i.hasNext(); ) { + final EnumConstantDeclaration e = i.next(); + e.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + if (!n.getMembers().isEmpty()) { + printer.println(";"); + printMembers(n.getMembers(), arg); + } else { + if (!n.getEntries().isEmpty()) { + printer.println(); + } + } + printer.unindent(); + printer.print("}"); + } + + @Override + public void visit(final EnumConstantDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + printMemberAnnotations(n.getAnnotations(), arg); + n.getName().accept(this, arg); + + if (!n.getArguments().isEmpty()) { + printArguments(n.getArguments(), arg); + } + + if (!n.getClassBody().isEmpty()) { + printer.println(" {"); + printer.indent(); + printMembers(n.getClassBody(), arg); + printer.unindent(); + printer.println("}"); + } + } + + @Override + public void visit(final EmptyMemberDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print(";"); + } + + @Override + public void visit(final InitializerDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + if (n.isStatic()) { + printer.print("static "); + } + n.getBody().accept(this, arg); + } + + @Override + public void visit(final IfStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("if ("); + n.getCondition().accept(this, arg); + final boolean thenBlock = n.getThenStmt() instanceof BlockStmt; + if (thenBlock) // block statement should start on the same line + printer.print(") "); + else { + printer.println(")"); + printer.indent(); + } + n.getThenStmt().accept(this, arg); + if (!thenBlock) + printer.unindent(); + if (n.getElseStmt().isPresent()) { + if (thenBlock) + printer.print(" "); + else + printer.println(); + final boolean elseIf = n.getElseStmt().orElse(null) instanceof IfStmt; + final boolean elseBlock = n.getElseStmt().orElse(null) instanceof BlockStmt; + if (elseIf || elseBlock) // put chained if and start of block statement on a same level + printer.print("else "); + else { + printer.println("else"); + printer.indent(); + } + if (n.getElseStmt().isPresent()) + n.getElseStmt().get().accept(this, arg); + if (!(elseIf || elseBlock)) + printer.unindent(); + } + } + + @Override + public void visit(final WhileStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("while ("); + n.getCondition().accept(this, arg); + printer.print(") "); + n.getBody().accept(this, arg); + } + + @Override + public void visit(final ContinueStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("continue"); + n.getLabel().ifPresent(l -> printer.print(" ").print(l.getIdentifier())); + printer.print(";"); + } + + @Override + public void visit(final DoStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("do "); + n.getBody().accept(this, arg); + printer.print(" while ("); + n.getCondition().accept(this, arg); + printer.print(");"); + } + + @Override + public void visit(final ForeachStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("for ("); + n.getVariable().accept(this, arg); + printer.print(" : "); + n.getIterable().accept(this, arg); + printer.print(") "); + n.getBody().accept(this, arg); + } + + @Override + public void visit(final ForStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("for ("); + if (n.getInitialization() != null) { + for (final Iterator<Expression> i = n.getInitialization().iterator(); i.hasNext(); ) { + final Expression e = i.next(); + e.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + printer.print("; "); + if (n.getCompare().isPresent()) { + n.getCompare().get().accept(this, arg); + } + printer.print("; "); + if (n.getUpdate() != null) { + for (final Iterator<Expression> i = n.getUpdate().iterator(); i.hasNext(); ) { + final Expression e = i.next(); + e.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + printer.print(") "); + n.getBody().accept(this, arg); + } + + @Override + public void visit(final ThrowStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("throw "); + n.getExpression().accept(this, arg); + printer.print(";"); + } + + @Override + public void visit(final SynchronizedStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("synchronized ("); + n.getExpression().accept(this, arg); + printer.print(") "); + n.getBody().accept(this, arg); + } + + @Override + public void visit(final TryStmt n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("try "); + if (!n.getResources().isEmpty()) { + printer.print("("); + Iterator<VariableDeclarationExpr> resources = n.getResources().iterator(); + boolean first = true; + while (resources.hasNext()) { + visit(resources.next(), arg); + if (resources.hasNext()) { + printer.print(";"); + printer.println(); + if (first) { + printer.indent(); + } + } + first = false; + } + if (n.getResources().size() > 1) { + printer.unindent(); + } + printer.print(") "); + } + if (n.getTryBlock().isPresent()) { + n.getTryBlock().get().accept(this, arg); + } + for (final CatchClause c : n.getCatchClauses()) { + c.accept(this, arg); + } + if (n.getFinallyBlock().isPresent()) { + printer.print(" finally "); + n.getFinallyBlock().get().accept(this, arg); + } + } + + @Override + public void visit(final CatchClause n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print(" catch ("); + n.getParameter().accept(this, arg); + printer.print(") "); + n.getBody().accept(this, arg); + + } + + @Override + public void visit(final AnnotationDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + printMemberAnnotations(n.getAnnotations(), arg); + printModifiers(n.getModifiers()); + + printer.print("@interface "); + n.getName().accept(this, arg); + printer.println(" {"); + printer.indent(); + if (n.getMembers() != null) { + printMembers(n.getMembers(), arg); + } + printer.unindent(); + printer.print("}"); + } + + @Override + public void visit(final AnnotationMemberDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + printMemberAnnotations(n.getAnnotations(), arg); + printModifiers(n.getModifiers()); + + n.getType().accept(this, arg); + printer.print(" "); + n.getName().accept(this, arg); + printer.print("()"); + if (n.getDefaultValue().isPresent()) { + printer.print(" default "); + n.getDefaultValue().get().accept(this, arg); + } + printer.print(";"); + } + + @Override + public void visit(final MarkerAnnotationExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("@"); + n.getName().accept(this, arg); + } + + @Override + public void visit(final SingleMemberAnnotationExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("@"); + n.getName().accept(this, arg); + printer.print("("); + n.getMemberValue().accept(this, arg); + printer.print(")"); + } + + @Override + public void visit(final NormalAnnotationExpr n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("@"); + n.getName().accept(this, arg); + printer.print("("); + if (n.getPairs() != null) { + for (final Iterator<MemberValuePair> i = n.getPairs().iterator(); i.hasNext(); ) { + final MemberValuePair m = i.next(); + m.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + } + printer.print(")"); + } + + @Override + public void visit(final MemberValuePair n, final Void arg) { + printJavaComment(n.getComment(), arg); + n.getName().accept(this, arg); + printer.print(" = "); + n.getValue().accept(this, arg); + } + + @Override + public void visit(final LineComment n, final Void arg) { + if (!configuration.isPrintComments()) { + return; + } + printer.print("//"); + String tmp = n.getContent(); + tmp = tmp.replace('\r', ' '); + tmp = tmp.replace('\n', ' '); + printer.println(tmp); + } + + @Override + public void visit(final BlockComment n, final Void arg) { + if (!configuration.isPrintComments()) { + return; + } + printer.print("/*").print(n.getContent()).println("*/"); + } + + @Override + public void visit(LambdaExpr n, Void arg) { + printJavaComment(n.getComment(), arg); + + final NodeList<Parameter> parameters = n.getParameters(); + final boolean printPar = n.isEnclosingParameters(); + + if (printPar) { + printer.print("("); + } + for (Iterator<Parameter> i = parameters.iterator(); i.hasNext(); ) { + Parameter p = i.next(); + p.accept(this, arg); + if (i.hasNext()) { + printer.print(", "); + } + } + if (printPar) { + printer.print(")"); + } + + printer.print(" -> "); + final Statement body = n.getBody(); + if (body instanceof ExpressionStmt) { + // Print the expression directly + ((ExpressionStmt) body).getExpression().accept(this, arg); + } else { + body.accept(this, arg); + } + } + + @Override + public void visit(MethodReferenceExpr n, Void arg) { + printJavaComment(n.getComment(), arg); + Expression scope = n.getScope(); + String identifier = n.getIdentifier(); + if (scope != null) { + n.getScope().accept(this, arg); + } + + printer.print("::"); + printTypeArgs(n, arg); + if (identifier != null) { + printer.print(identifier); + } + + } + + @Override + public void visit(TypeExpr n, Void arg) { + printJavaComment(n.getComment(), arg); + if (n.getType() != null) { + n.getType().accept(this, arg); + } + } + + @Override + public void visit(NodeList n, Void arg) { + for (Object node : n) { + ((Node) node).accept(this, arg); + } + } + + @Override + public void visit(final ImportDeclaration n, final Void arg) { + printJavaComment(n.getComment(), arg); + printer.print("import "); + if (n.isStatic()) { + printer.print("static "); + } + n.getName().accept(this, arg); + if (n.isAsterisk()) { + printer.print(".*"); + } + printer.println(";"); + + printOrphanCommentsEnding(n); + } + + private void printOrphanCommentsBeforeThisChildNode(final Node node) { + if (node instanceof Comment) return; + + Node parent = node.getParentNode().orElse(null); + if (parent == null) return; + List<Node> everything = new LinkedList<>(); + everything.addAll(parent.getChildNodes()); + sortByBeginPosition(everything); + int positionOfTheChild = -1; + for (int i = 0; i < everything.size(); i++) { + if (everything.get(i) == node) positionOfTheChild = i; + } + if (positionOfTheChild == -1) { + throw new AssertionError("I am not a child of my parent."); + } + int positionOfPreviousChild = -1; + for (int i = positionOfTheChild - 1; i >= 0 && positionOfPreviousChild == -1; i--) { + if (!(everything.get(i) instanceof Comment)) positionOfPreviousChild = i; + } + for (int i = positionOfPreviousChild + 1; i < positionOfTheChild; i++) { + Node nodeToPrint = everything.get(i); + if (!(nodeToPrint instanceof Comment)) + throw new RuntimeException( + "Expected comment, instead " + nodeToPrint.getClass() + ". Position of previous child: " + + positionOfPreviousChild + ", position of child " + positionOfTheChild); + nodeToPrint.accept(this, null); + } + } + + private void printOrphanCommentsEnding(final Node node) { + List<Node> everything = new LinkedList<>(); + everything.addAll(node.getChildNodes()); + sortByBeginPosition(everything); + if (everything.isEmpty()) { + return; + } + + int commentsAtEnd = 0; + boolean findingComments = true; + while (findingComments && commentsAtEnd < everything.size()) { + Node last = everything.get(everything.size() - 1 - commentsAtEnd); + findingComments = (last instanceof Comment); + if (findingComments) { + commentsAtEnd++; + } + } + for (int i = 0; i < commentsAtEnd; i++) { + everything.get(everything.size() - commentsAtEnd + i).accept(this, null); + } + } + +} |