Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inner types without file should not crash Environment#report #4998

Merged
merged 4 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/main/java/spoon/support/StandardEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,32 @@ public void report(Processor<?> processor, Level level, CtElement element, Strin
if (sp == null) {
buffer.append(" (Unknown Source)");
} else {
// TODO: will explode if type == null
buffer.append(" at " + type.getQualifiedName() + ".");
if (type != null) {
buffer.append(" at ")
.append(type.getQualifiedName())
.append(".");
} else {
buffer.append("at (?).");
}
CtExecutable<?> exe = (element instanceof CtExecutable) ? (CtExecutable<?>) element : element.getParent(CtExecutable.class);
if (exe != null) {
buffer.append(exe.getSimpleName());
}
buffer.append("(" + sp.getFile().getName() + ":" + sp.getLine() + ")");
if (sp.getFile() != null) {
buffer.append("(")
.append(sp.getFile().getName())
.append(":")
.append(sp.getLine())
.append(")");
} else {
buffer.append("(?:?)");
}
}

print(buffer.toString(), level);
}


@Override
public void report(Processor<?> processor, Level level, String message) {
StringBuilder buffer = new StringBuilder();
Expand Down
31 changes: 28 additions & 3 deletions src/test/java/spoon/test/logging/LogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.ParameterizedTest;
import spoon.FluentLauncher;
import spoon.Launcher;
import spoon.MavenLauncher;
import spoon.processing.AbstractProcessor;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.JavaOutputProcessor;
import spoon.support.Level;
import spoon.test.GitHubIssue;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;

import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -103,4 +111,21 @@ public void testLoggingOff() {
}
assertEquals(0, logger.getLoggingEvents().size());
}
}

@GitHubIssue(issueNumber = 4997, fixed = true)
void innerTypesCrashesLogging() {
// contract: when a class has inner types, the logging should not crash with a NPE
String codePath = "src/test/resources/logging/TestCase2.java";
var processor = new AbstractProcessor<CtConstructor<?>>() {
@Override
public void process(CtConstructor<?> element) {
// do nothing
}
public boolean isToBeProcessed(CtConstructor<?> candidate) {
List<CtInvocation<?>> invocations = Query.getElements(candidate, new TypeFilter<>(CtInvocation.class));
invocations.forEach(i -> getEnvironment().report(this, Level.INFO, i, "Message"));
return false;
}};
assertDoesNotThrow(() -> new FluentLauncher().inputResource(codePath).processor(processor).buildModel());
}
}
33 changes: 33 additions & 0 deletions src/test/resources/logging/TestCase2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fr.inria.gforge.spoon.analysis;

/**
* The Class Testcase2.
*
* testcase to show NullPointerException
*/
public class Testcase2 {

private class A {

}

private class B extends A {

/**
* Instantiates a new b.
*/
public B() {
super();
}
}

public static void main(String[] args) {
Testcase2 t = new Testcase2();
t.test();
}

public String test() {
var b = new B();
return b.toString();
}
}