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

element.toString() crashes with a NP in DefaultJavaPrettyPrinter.visitCtTypeReference() #1099

Closed
arturbosch opened this issue Jan 10, 2017 · 5 comments

Comments

@arturbosch
Copy link
Contributor

When analyzing elasticsearch we get a NP when calling element.toString().
As this has something to do with type references, I can't give you a concrete source file with this problem, but rather a part of the project as tar archive. (
elasticsearch.tar.gz
)

2017-01-10 11:37:50,692 ERROR  [de.unibremen.st.ismell.config.DetectorStarter] : Exception while running: GodClassDetector 
java.lang.NullPointerException
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtTypeReference(DefaultJavaPrettyPrinter.java:1609)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtTypeReference(DefaultJavaPrettyPrinter.java:1578)
	at spoon.support.reflect.reference.CtTypeReferenceImpl.accept(CtTypeReferenceImpl.java:68)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.scan(DefaultJavaPrettyPrinter.java:265)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtLocalVariable(DefaultJavaPrettyPrinter.java:1101)
	at spoon.support.reflect.code.CtLocalVariableImpl.accept(CtLocalVariableImpl.java:49)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.scan(DefaultJavaPrettyPrinter.java:254)
	at spoon.reflect.visitor.printer.ElementPrinterHelper.writeStatement(ElementPrinterHelper.java:140)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtBlock(DefaultJavaPrettyPrinter.java:401)
	at spoon.support.reflect.code.CtBlockImpl.accept(CtBlockImpl.java:45)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.scan(DefaultJavaPrettyPrinter.java:254)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtMethod(DefaultJavaPrettyPrinter.java:1153)
	at spoon.support.reflect.declaration.CtMethodImpl.accept(CtMethodImpl.java:62)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.scan(DefaultJavaPrettyPrinter.java:254)
	at spoon.reflect.visitor.printer.ElementPrinterHelper.writeElementList(ElementPrinterHelper.java:154)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtClass(DefaultJavaPrettyPrinter.java:496)
	at spoon.support.reflect.declaration.CtClassImpl.accept(CtClassImpl.java:62)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.scan(DefaultJavaPrettyPrinter.java:254)
	at spoon.reflect.visitor.printer.ElementPrinterHelper.writeElementList(ElementPrinterHelper.java:154)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.visitCtClass(DefaultJavaPrettyPrinter.java:496)
	at spoon.support.reflect.declaration.CtClassImpl.accept(CtClassImpl.java:62)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.scan(DefaultJavaPrettyPrinter.java:254)
	at spoon.support.reflect.declaration.CtElementImpl.toString(CtElementImpl.java:226)
	at de.unibremen.st.ismell.detection.common.BadSmellHelper.createSourceCode(BadSmellHelper.java:63)
	at de.unibremen.st.ismell.detection.common.BadSmellHelper.createEntity(BadSmellHelper.java:54)
	at de.unibremen.st.ismell.detection.common.BadSmellHelper.createEntity(BadSmellHelper.java:32)
	at de.unibremen.st.ismell.detection.godclass.GodClassDetector.handleCtType(GodClassDetector.java:125)
	at de.unibremen.st.ismell.detection.godclass.GodClassDetector.visitCtClass(GodClassDetector.java:102)
	at spoon.support.reflect.declaration.CtClassImpl.accept(CtClassImpl.java:62)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:148)
	at de.unibremen.st.ismell.detection.common.Scanner.scan(Scanner.java:38)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:137)
	at spoon.reflect.visitor.CtScanner.visitCtPackage(CtScanner.java:628)
	at spoon.support.reflect.declaration.CtPackageImpl.accept(CtPackageImpl.java:47)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:148)
	at de.unibremen.st.ismell.detection.common.Scanner.scan(Scanner.java:38)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:137)
	at spoon.reflect.visitor.CtScanner.visitCtPackage(CtScanner.java:627)
	at spoon.support.reflect.declaration.CtPackageImpl.accept(CtPackageImpl.java:47)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:148)
	at de.unibremen.st.ismell.detection.common.Scanner.scan(Scanner.java:38)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:137)
	at spoon.reflect.visitor.CtScanner.visitCtPackage(CtScanner.java:627)
	at spoon.support.reflect.declaration.CtPackageImpl.accept(CtPackageImpl.java:47)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:148)
	at de.unibremen.st.ismell.detection.common.Scanner.scan(Scanner.java:38)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:137)
	at spoon.reflect.visitor.CtScanner.visitCtPackage(CtScanner.java:627)
	at spoon.support.reflect.declaration.CtPackageImpl.accept(CtPackageImpl.java:47)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:148)
	at de.unibremen.st.ismell.detection.common.Scanner.scan(Scanner.java:38)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:137)
	at spoon.reflect.visitor.CtScanner.visitCtPackage(CtScanner.java:627)
	at spoon.support.reflect.declaration.CtPackageImpl.accept(CtPackageImpl.java:47)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:148)
	at de.unibremen.st.ismell.detection.common.Scanner.scan(Scanner.java:38)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:137)
	at spoon.reflect.visitor.CtScanner.visitCtPackage(CtScanner.java:627)
	at spoon.support.reflect.declaration.CtPackageImpl.accept(CtPackageImpl.java:47)
	at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:148)
	at de.unibremen.st.ismell.detection.common.Scanner.scan(Scanner.java:34)
	at de.unibremen.st.ismell.config.DetectorStarter.lambda$execute$0(DetectorStarter.java:61)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
@arturbosch
Copy link
Contributor Author

On 22.11.16 Pavel V. changed

			if (!context.ignoreEnclosingClass && !ref.isLocalType() && !ref.getDeclaringType().isAnonymous()) {
				boolean ign = context.ignoreGenerics;
				if (!withGenerics) {
					context.ignoreGenerics = true;
				}
				scan(ref.getDeclaringType());
				if (!withGenerics) {
					context.ignoreGenerics = ign;
				}
				printer.write(".");
			}

to

			if (!context.ignoreEnclosingClass && !ref.isLocalType()) {
				//compute visible type which can be used to print access path to ref
				CtTypeReference<?> accessType = ref.getAccessType(context.getCurrentTypeReference(true));
				if (!accessType.isAnonymous()) {
					boolean ign = context.ignoreGenerics;
					if (!withGenerics) {
						context.ignoreGenerics = true;
					}
					scan(accessType);
					if (!withGenerics) {
						context.ignoreGenerics = ign;
					}
					printer.write(".");
				}
			}

A Nullcheck would fix this issue for us java accessType != null && !accessType.isAnonymous(), but is it the correct behaviour?

@pvojtechovsky
Copy link
Collaborator

getAccessType() returns null when it cannot found access path to the type.
I have to analyze your model and situation when it fails, then I can say whether null check is good solution or we need a better one. I will have a look at it probably today in the evening.

@msteinbeck
Copy link
Contributor

It should be emphasized that we are using noclasspath mode which, by its nature, may produce unresolvable references. Is this a problem for the DefaultPrettyPrinter?

@pvojtechovsky
Copy link
Collaborator

Thanks for info. Yes it might be the cause of that problem. It would be helpful if you provide details about ref which failed. What is the full class name? What is the full class name of actually generated file?

pvojtechovsky added a commit to pvojtechovsky/spoon that referenced this issue Jan 10, 2017
surli pushed a commit that referenced this issue Jan 13, 2017
* reproduce ElasticSearch access path problem #1099

* fix access path problem

* fix other tests

* Add one more assert on tests to check behaviour when overriding inner class. Replace some assertTrue by assertEquals to help debug test.
@surli
Copy link
Collaborator

surli commented Jan 13, 2017

Should be fixed by #1102. Thanks @pvojtechovsky!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants