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

review: feat(sniper): detailed tree of source fragments of compilation unit #2283

Merged
merged 16 commits into from
Sep 11, 2018
Merged
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/cu/CompilationUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package spoon.reflect.cu;

import spoon.processing.FactoryAccessor;
import spoon.reflect.declaration.CtImport;
import spoon.reflect.declaration.CtModule;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtImport;
import spoon.support.Experimental;

import java.io.File;
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/spoon/reflect/cu/SourcePositionHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,25 @@
*/
package spoon.reflect.cu;

import spoon.reflect.visitor.printer.internal.ElementSourceFragment;
import spoon.support.Experimental;

/**
* This interface represents an element which knows it's position in a source file.
* This interface represents an element which knows its position in a source file.
*/
public interface SourcePositionHolder {
/** If the element comes from a Java source file (hence has not created during transformation), returns the position in the original source file */
SourcePosition getPosition();

/**
* Returns the original source code (maybe different from toString() if a transformation has been applied.
* Or {@link ElementSourceFragment#NO_SOURCE_FRAGMENT} if this element has no original source fragment.
*
* Warning: this is a advanced method which cannot be considered as part of the stable API
*
*/
@Experimental
default ElementSourceFragment getOriginalSourceFragment() {
return ElementSourceFragment.NO_SOURCE_FRAGMENT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright (C) 2006-2018 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.reflect.visitor.printer.internal;

import java.util.List;

import spoon.reflect.meta.ContainerKind;
import spoon.reflect.path.CtRole;
import spoon.support.Experimental;

/**
* {@link SourceFragment} of List or Set of {@link ElementSourceFragment}s which belong to collection role.
* For example list of Type members or list of parameters, etc.
* Or set of modifiers and annotations
*/
@Experimental
public class CollectionSourceFragment implements SourceFragment {

private final List<SourceFragment> items;

public CollectionSourceFragment(List<SourceFragment> items) {
super();
this.items = items;
}

@Override
public String getSourceCode() {
StringBuilder sb = new StringBuilder();
for (SourceFragment childSourceFragment : items) {
sb.append(childSourceFragment.getSourceCode());
}
return sb.toString();
}

/**
* @return child source fragments of this collection
*/
public List<SourceFragment> getItems() {
return items;
}

@Override
public String toString() {
return items.toString();
}

/**
* @return true if collection contains only children of one role handler with container kind LIST
*/
public boolean isOrdered() {
CtRole role = null;
ContainerKind kind = null;
for (SourceFragment childSourceFragment : items) {
if (childSourceFragment instanceof ElementSourceFragment) {
ElementSourceFragment esf = (ElementSourceFragment) childSourceFragment;
if (role == null) {
role = esf.getRoleInParent();
kind = esf.getContainerKindInParent();
if (kind != ContainerKind.LIST) {
return false;
}
} else {
if (role != esf.getRoleInParent()) {
//the collection contains elements of different roles. It cannot be ordered
return false;
}
//else there is another element of the same role - ok
}
}
}
//there are only elements of one role of container kind LIST
return true;
}
}
Loading