Skip to content

Commit

Permalink
performance - cache names of members in currentThis
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Nov 11, 2016
1 parent 6eb13c7 commit 7b76054
Showing 1 changed file with 47 additions and 15 deletions.
62 changes: 47 additions & 15 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,46 @@ public class DefaultJavaPrettyPrinter implements CtVisitor, PrettyPrinter {
*/
public static final String LINE_SEPARATOR = System.getProperty("line.separator");

protected static class TypeContext {
CtTypeReference<?> type;
Set<String> memberNames;

TypeContext(CtTypeReference<?> p_type) {
type = p_type;
}

public boolean isNameConflict(String name) {
if (memberNames == null) {
Collection<CtFieldReference<?>> allFields = type.getAllFields();
memberNames = new HashSet<>(allFields.size());
for (CtFieldReference<?> field : allFields) {
memberNames.add(field.getSimpleName());
}
}
return memberNames.contains(name);
}

public String getSimpleName() {
return type.getSimpleName();
}

public CtPackageReference getPackage() {
return type.getPackage();
}
}

public class PrintingContext {
boolean noTypeDecl = false;

Deque<CtTypeReference<?>> currentThis = new ArrayDeque<>();
Deque<TypeContext> currentThis = new ArrayDeque<>();

public void pushCurrentThis(CtTypeReference<?> type) {
currentThis.push(new TypeContext(type));
}
public void popCurrentThis() {
currentThis.pop();
}


Deque<CtElement> elementStack = new ArrayDeque<>();

Expand Down Expand Up @@ -510,7 +546,7 @@ public void visitCtCatch(CtCatch catchBlock) {

@Override
public <T> void visitCtClass(CtClass<T> ctClass) {
context.currentThis.push(ctClass.getReference());
context.pushCurrentThis(ctClass.getReference());

if (ctClass.getSimpleName() != null && !CtType.NAME_UNKNOWN.equals(ctClass.getSimpleName()) && !ctClass.isAnonymous()) {
visitCtType(ctClass);
Expand All @@ -529,7 +565,7 @@ public <T> void visitCtClass(CtClass<T> ctClass) {
printer.write(" {").incTab();
elementPrinterHelper.writeElementList(ctClass.getTypeMembers());
printer.decTab().writeTabs().write("}");
context.currentThis.pop();
context.popCurrentThis();
}

@Override
Expand Down Expand Up @@ -634,7 +670,7 @@ public <T extends Enum<?>> void visitCtEnum(CtEnum<T> ctEnum) {
visitCtType(ctEnum);
printer.write("enum " + ctEnum.getSimpleName());
elementPrinterHelper.writeImplementsClause(ctEnum);
context.currentThis.push(ctEnum.getReference());
context.pushCurrentThis(ctEnum.getReference());
printer.write(" {").incTab().writeln();

if (ctEnum.getEnumValues().size() == 0) {
Expand All @@ -650,7 +686,7 @@ public <T extends Enum<?>> void visitCtEnum(CtEnum<T> ctEnum) {

elementPrinterHelper.writeElementList(ctEnum.getTypeMembers());
printer.decTab().writeTabs().write("}");
context.currentThis.pop();
context.popCurrentThis();
}

@Override
Expand Down Expand Up @@ -894,7 +930,7 @@ public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
if (context.currentTopLevel != null) {
CtTypeReference<?> ref2;
if (context.currentThis != null && context.currentThis.size() > 0) {
ref2 = context.currentThis.peekFirst();
ref2 = context.currentThis.peekFirst().type;
} else {
ref2 = context.currentTopLevel.getReference();
}
Expand Down Expand Up @@ -1562,17 +1598,13 @@ private boolean printQualified(CtTypeReference<?> ref) {
//A) we are in the context of a class which is also called "Something",
//B) we are in the context of a class which defines field which is also called "Something",
// we should still use qualified version my.pkg.Something
for (CtTypeReference<?> enclosingClassRef : context.currentThis) {
if (enclosingClassRef.getSimpleName().equals(ref.getSimpleName())
&& !Objects.equals(enclosingClassRef.getPackage(), ref.getPackage())) {
for (TypeContext typeContext : context.currentThis) {
if (typeContext.getSimpleName().equals(ref.getSimpleName())
&& !Objects.equals(typeContext.getPackage(), ref.getPackage())) {
return true;
}
CtType<?> enclosingClass = enclosingClassRef.getDeclaration();
if (enclosingClass != null) {
CtFieldReference<?> field = enclosingClass.getDeclaredOrInheritedField(ref.getSimpleName());
if (field != null) {
return true;
}
if (typeContext.isNameConflict(ref.getSimpleName())) {
return true;
}
}
return false;
Expand Down

0 comments on commit 7b76054

Please sign in to comment.