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

refactor: applies the null design pattern to compilation units #3151

Merged
merged 2 commits into from
Oct 10, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.support.reflect.cu.CompilationUnitImpl;

import java.io.File;
import java.io.Serializable;
Expand All @@ -23,9 +24,15 @@ public File getFile() {
return null;
}

// avoid null pointer exceptions later
public static class NullCompilationUnit extends CompilationUnitImpl {
private NullCompilationUnit() { }
}
private static final CompilationUnit instanceNullCompilationUnit = new NullCompilationUnit();

@Override
public CompilationUnit getCompilationUnit() {
return null;
return instanceNullCompilationUnit;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import spoon.SpoonException;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.CtModule;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
Expand Down Expand Up @@ -49,7 +50,7 @@ public CompilationUnit create() {
}

public CompilationUnit getOrCreate(CtPackage ctPackage) {
if (ctPackage.getPosition().getCompilationUnit() != null) {
if (!(ctPackage.getPosition().getCompilationUnit() instanceof NoSourcePosition.NullCompilationUnit)) {
return ctPackage.getPosition().getCompilationUnit();
} else {

Expand All @@ -76,7 +77,7 @@ public CompilationUnit getOrCreate(CtType type) {
if (type == null) {
return null;
}
if (type.getPosition().getCompilationUnit() != null) {
if (!(type.getPosition().getCompilationUnit() instanceof NoSourcePosition.NullCompilationUnit)) {
return type.getPosition().getCompilationUnit();
}

Expand All @@ -103,7 +104,7 @@ public CompilationUnit getOrCreate(CtType type) {
}

public CompilationUnit getOrCreate(CtModule module) {
if (module.getPosition().getCompilationUnit() != null) {
if (!(module.getPosition().getCompilationUnit() instanceof NoSourcePosition.NullCompilationUnit)) {
return module.getPosition().getCompilationUnit();
} else {
File file = this.factory.getEnvironment().getOutputDestinationHandler().getOutputPath(module, null, null).toFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package spoon.support.modelobs;

import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.path.CtRole;
import spoon.support.sniper.internal.ElementSourceFragment;
Expand All @@ -23,7 +24,7 @@ protected void onChange(CtElement currentElement, CtRole role) {
return;
}
CompilationUnit cu = currentElement.getPosition().getCompilationUnit();
if (cu != null) {
if (!(cu instanceof NoSourcePosition.NullCompilationUnit)) {
//getOriginalSourceFragment is not only a getter, it actually
//builds a tree of SourceFragments of compilation unit of the modified element
cu.getOriginalSourceFragment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.SourcePositionHolder;
import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.CtCompilationUnit;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtModifiable;
Expand Down Expand Up @@ -176,7 +177,7 @@ protected void exit(CtElement e) {
*/
private ElementSourceFragment addChild(ElementSourceFragment parentFragment, CtRole roleInParent, SourcePositionHolder otherElement) {
SourcePosition otherSourcePosition = otherElement.getPosition();
if (otherSourcePosition instanceof SourcePositionImpl && otherSourcePosition.getCompilationUnit() != null) {
if (otherSourcePosition instanceof SourcePositionImpl && !(otherSourcePosition.getCompilationUnit() instanceof NoSourcePosition.NullCompilationUnit)) {
if (parentFragment.isFromSameSource(otherSourcePosition)) {
ElementSourceFragment otherFragment = new ElementSourceFragment(otherElement, parentFragment.getRoleHandler(roleInParent, otherElement));
//parent and child are from the same file. So we can connect their positions into one tree
Expand Down