Skip to content

Commit

Permalink
fix(pretty-print): fix pretty-printing of 'this' access for inner cla…
Browse files Browse the repository at this point in the history
…sses (#1061)

closes #1055
  • Loading branch information
surli authored and monperrus committed Dec 20, 2016
1 parent 75f027f commit 747c36d
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -757,15 +757,21 @@ public <T> void visitCtThisAccess(CtThisAccess<T> thisAccess) {
}

// complex case of qualifed this
if (context.currentThis.peekLast() != null
&& !context.currentThis.peekLast().type.getQualifiedName().equals(targetType.getQualifiedName())) {
printer.snapshotLength();
visitCtTypeReferenceWithoutGenerics(targetType);
if (printer.hasNewContent()) {
printer.write(".");
if (!context.currentThis.isEmpty()) {

CtType lastType = context.currentThis.peekFirst().type;
String lastTypeQualifiedName = lastType.getQualifiedName();
String targetTypeQualifiedName = targetType.getQualifiedName();

if (!lastTypeQualifiedName.equals(targetTypeQualifiedName)) {
printer.snapshotLength();
visitCtTypeReferenceWithoutGenerics(targetType);
if (printer.hasNewContent()) {
printer.write(".");
}
printer.write("this");
return;
}
printer.write("this");
return;
}

// the default super simple case only comes at the end
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/visitor/PrintingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private TypeContext getCurrentTypeContext() {
}

public void pushCurrentThis(CtType<?> type) {
currentThis.add(new TypeContext(type));
currentThis.push(new TypeContext(type));
}
public void popCurrentThis() {
currentThis.pop();
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/spoon/test/prettyprinter/PrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.canBeBuilt;

import org.junit.Test;

Expand Down Expand Up @@ -75,4 +76,44 @@ public void testFQNModeWriteFQNConstructorInCtVisitor() {
assertTrue("The result should not contain reduced constructors: "+result, !result.contains("new ReplacementVisitor("));
}

@Test
public void testRuleCanBeBuild() {
Launcher spoon = new Launcher();
PrettyPrinter printer = spoon.createPrettyPrinter();
spoon.getEnvironment().setAutoImports(true);
String output = "./target/spoon-rule/";
spoon.addInputResource("./src/test/java/spoon/test/prettyprinter/testclasses/Rule.java");
spoon.setSourceOutputDirectory(output);
spoon.run();

CtType element = spoon.getFactory().Class().getAll().get(0);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

assertTrue("The result should contain direct this accessor for field: "+result, !result.contains("Rule.Phoneme.this.phonemeText"));
canBeBuilt(output, 7);
}

@Test
public void testJDTBatchCompilerCanBeBuild() {
Launcher spoon = new Launcher();
PrettyPrinter printer = spoon.createPrettyPrinter();
spoon.getEnvironment().setAutoImports(false);
String output = "./target/spoon-jdtbatchcompiler/";
spoon.addInputResource("./src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java");
spoon.setSourceOutputDirectory(output);
spoon.run();

CtType element = spoon.getFactory().Class().getAll().get(0);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();

//assertTrue("The result should contain direct this accessor for field: "+result, !result.contains("Rule.Phoneme.this.phonemeText"));
canBeBuilt(output, 7);
}

}
98 changes: 98 additions & 0 deletions src/test/java/spoon/test/prettyprinter/testclasses/Rule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (C) 2006-2016 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and abiding by the rules of distribution of free software. You can use, modify and/or redistribute the software under the terms of the CeCILL-C license as circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program 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 CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had knowledge of the CeCILL-C license and that you accept its terms.
*/

package spoon.test.prettyprinter.testclasses;

import java.util.*;

/**
* Created by urli on 19/12/2016.
*/
public class Rule {
public enum Language {
DTD, WSDL, OTHER
}
public static final class Phoneme implements PhonemeExpr {
public interface Bidule {}

public static final Comparator<Phoneme> COMPARATOR = new Comparator<Phoneme>() {
@Override
public int compare(final Phoneme o1, final Phoneme o2) {
for (int i = 0; i < o1.phonemeText.length(); i++) {
if (i >= o2.phonemeText.length()) {
return +1;
}
final int c = o1.phonemeText.charAt(i) - o2.phonemeText.charAt(i);
if (c != 0) {
return c;
}
}

if (o1.phonemeText.length() < o2.phonemeText.length()) {
return -1;
}

return 0;
}
};

private final StringBuilder phonemeText;
private final Language language;

public Phoneme(final CharSequence phonemeText, final Language language) {
this.phonemeText = new StringBuilder(phonemeText);
this.language = language;
}

@Override
public Iterable<Phoneme> getPhonemes() {
return Collections.singleton(this);
}
}

public interface PhonemeExpr {
Iterable<Phoneme> getPhonemes();
}

public static final class PhonemeList implements PhonemeExpr {
private final List<Phoneme> phonemes;

public PhonemeList(final List<Phoneme> phonemes) {
this.phonemes = phonemes;
}

@Override
public List<Phoneme> getPhonemes() {
return this.phonemes;
}
}

public static final String ALL = "ALL";

private static final String DOUBLE_QUOTE = "\"";

private static final String HASH_INCLUDE = "#include";

private static Phoneme parsePhoneme(final String ph) {
final int open = ph.indexOf("[");
if (open >= 0) {
if (!ph.endsWith("]")) {
throw new IllegalArgumentException("Phoneme expression contains a '[' but does not end in ']'");
}
final String before = ph.substring(0, open);
final String in = ph.substring(open + 1, ph.length() - 1);
final Set<String> langs = new HashSet<String>(Arrays.asList(in.split("[+]")));

return new Phoneme(before, Language.DTD);
}
return new Phoneme(ph, Language.WSDL);
}
}

0 comments on commit 747c36d

Please sign in to comment.