本文整理汇总了Java中spoon.reflect.declaration.CtElement类的典型用法代码示例。如果您正苦于以下问题:Java CtElement类的具体用法?Java CtElement怎么用?Java CtElement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CtElement类属于spoon.reflect.declaration包,在下文中一共展示了CtElement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addBlock
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
public void addBlock(CtBlock ctBlock) {
if (ctBlock == null) {
return;
}
System.out.println("BLOCK: " + ctBlock.getParent().toString());
for (CtStatement ctStatement : ctBlock.getStatements()) {
System.out.println(" STATEMENT: " + ctStatement);
StatementShovel statementShovel = new StatementShovel(codeWriter, typeMapper);
if (ctStatement.toString().equals("super()")) {
// skip super() calls for now
continue;
}
if (ctStatement instanceof CtComment) {
new CommentWriter(codeWriter).write((CtComment)ctStatement);
}
for (CtElement ctElement : ctStatement.getElements(new MatchAllFilter<CtElement>())) {
System.out.println(" ELEMENT: " + ctElement.getClass().getSimpleName() + " " + ctElement);
statementShovel.addElement(ctElement);
}
statementShovel.finish();
}
}
示例2: changeThisAccesses
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
/**
* The statements of the command may refer to the external listener through 'this'. In such a case, the 'this' has to be changed
* in a variable access.
*/
private void changeThisAccesses(final @NotNull List<CtElement> stats, final @NotNull CtAbstractInvocation<?> regInvok) {
final Filter<CtThisAccess<?>> filter = new ThisAccessFilter(false);
final CtExecutable<?> regMethod = regInvok.getParent(CtExecutable.class);
stats.stream().map(stat -> stat.getElements(filter)).flatMap(s -> s.stream()).forEach(th -> {
List<CtLocalVariable<?>> thisVar = regMethod.getElements(new BasicFilter<CtLocalVariable<?>>(CtLocalVariable.class) {
@Override
public boolean matches(final CtLocalVariable<?> element) {
final CtType<?> decl = element.getType().getDeclaration();
return decl!=null && decl.equals(th.getType().getDeclaration());
}
});
if(thisVar.isEmpty()) {
LOG.log(Level.SEVERE, "Cannot find a local variable for a 'this' access: " + thisVar);
}else {
th.replace(regInvok.getFactory().Code().createVariableRead(thisVar.get(0).getReference(), false));
}
});
}
示例3: processCondition
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
@Override
public CtIf processCondition(CtStatement element, String newCondition) {
//logger.debug("##### {} ##### Before:\n{}", element, element.getParent());
// if the element is not a line
if (!new LineFilter().matches(element)) {
element = element.getParent(new LineFilter());
}
CtElement parent = element.getParent();
CtIf newIf = element.getFactory().Core().createIf();
CtCodeSnippetExpression<Boolean> condition = element.getFactory().Core().createCodeSnippetExpression();
condition.setValue(newCondition);
newIf.setCondition(condition);
// Fix : warning: ignoring inconsistent parent for [CtElem1] ( [CtElem2] != [CtElem3] )
newIf.setParent(parent);
element.replace(newIf);
// this should be after the replace to avoid an StackOverflowException caused by the circular reference.
newIf.setThenStatement(element);
//logger.debug("##### {} ##### After:\n{}", element, element.getParent().getParent());
return newIf;
}
示例4: extractCommandsFromConditionalStatements
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
private void extractCommandsFromConditionalStatements(final @NotNull CtElement condStat, final @NotNull CtExecutable<?> listenerMethod,
final @NotNull List<CtStatement> conds) {
UIListener uiListener;
synchronized(commands) {
uiListener = commands.computeIfAbsent(listenerMethod, k -> new UIListener(listenerMethod));
}
if(condStat instanceof CtIf) {
extractCommandsFromIf((CtIf) condStat, uiListener, conds);
return;
}
if(condStat instanceof CtCase<?>) {
extractCommandsFromSwitchCase((CtCase<?>) condStat, uiListener);
return;
}
LOG.log(Level.SEVERE, "Unsupported conditional blocks: " + condStat);
}
示例5: fieldsOfParameters
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
@Test
public void fieldsOfParameters() {
CtElement element = elementInClassToSpoon("nested2 != null");
testReachedVariableNames(element, "comparable",
"nested2",
"comparable.privateNestedInstanceField",
"comparable.publicNestedInstanceField",
"comparable.protectedNestedInstanceField",
"spoon.example.ClassToSpoon.protectedStaticField",
"spoon.example.ClassToSpoon.privateStaticField",
"spoon.example.ClassToSpoon.publicStaticField",
"nested2.privateInstanceField",
"nested2.publicInstanceField",
"nested2.protectedInstanceField",
"spoon.example.ClassToSpoon.this.publicInstanceField",
"spoon.example.ClassToSpoon.this.privateInstanceField",
"spoon.example.ClassToSpoon.this.protectedInstanceField");
}
示例6: gatherContainingCondition
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
public List<CtExpression<?>> gatherContainingCondition(List<CtCodeElement> elements, CtExecutable<?> method) {
List<CtExpression<?>> res = new ArrayList<>();
CtCodeElement action = elements.get(0);
CtElement parent = action;
if (action != null) {
try {
parent = action.getParent();
while (parent != null && parent != method) {
res.addAll(exploreConditionals(action, (CtCodeElement) parent));// optimize
// to
// explore
// only
// action's
// parents
parent = parent.getParent();
}
} catch (ParentNotInitializedException e) {
System.out.println("Parent init exeption: " + parent);
}
}
return res;
}
示例7: getParentOf
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
/**
* Looks for a parent of the given type from a given element.
* @param elt The element from which the parent is looked for.
* @param typeParent The class of the parent to look for.
* @param butNot The research will stop as soon as the current parent is this element. Can be null.
* @return The found element or nothing.
*/
public <T extends CtElement> @NotNull Optional<CtElement> getParentOf(final @Nullable CtElement elt, final @NotNull Class<T> typeParent,
final @Nullable CtElement butNot) {
if(elt == null) return Optional.empty();
try {
CtElement parent = elt.getParent();
while(!typeParent.isInstance(parent) && parent != butNot) {
parent = parent.getParent();
}
if(parent == butNot || !typeParent.isInstance(parent)) return Optional.empty();
return Optional.of(parent);
}catch(final ParentNotInitializedException ex) {
return Optional.empty();
}
}
示例8: getStatement
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
/**
* Searches for the parent or current statement. If elt is a statement, elt is returned.
* Otherwise, a statement is searched in the parent hierarchy.
* @param elt The element to analyse.
* @return The parent statement or elt or nothing.
*/
public Optional<CtStatement> getStatement(final @Nullable CtElement elt) {
if(elt==null) {
return Optional.empty();
}
if(elt instanceof CtStatement) {
return Optional.of((CtStatement)elt);
}
if(elt.isParentInitialized()) {
return getStatement(elt.getParent());
}
return Optional.empty();
}
示例9: refactorRegistrationAsLambda
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
private <T> void refactorRegistrationAsLambda(final @NotNull CtAbstractInvocation<?> regInvok,
final @NotNull Set<CtAbstractInvocation<?>> unregInvoks,
final int regPos, final String widgetName) {
final Factory fac = regInvok.getFactory();
final CtTypeReference<T> typeRef = (CtTypeReference<T>) regInvok.getExecutable().getParameters().get(regPos).getTypeDeclaration().getReference();
final CtLambda<T> lambda = fac.Core().createLambda();
final List<CtElement> stats = cleanStatements(regInvok, fac);
if(stats.size()==1 && stats.get(0) instanceof CtExpression<?>) {
lambda.setExpression((CtExpression<T>)stats.get(0));
} else {
final CtBlock<T> block = fac.Core().createBlock();
stats.stream().filter(stat -> stat instanceof CtStatement).forEach(stat -> block.insertEnd((CtStatement)stat));
lambda.setBody(block);
}
CtParameter<?> oldParam = cmd.getExecutable().getParameters().get(0);
CtParameter<?> param = fac.Executable().createParameter(lambda, oldParam.getType(), oldParam.getSimpleName());
lambda.setParameters(Collections.singletonList(param));
lambda.setType(typeRef);
replaceRegistrationParameter(lambda, regInvok, unregInvoks, regPos, widgetName);
changeThisAccesses(stats, regInvok);
}
示例10: getParent
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
private CtStatement getParent(CtInvocation invocationToBeCloned) {
CtElement parent = invocationToBeCloned;
while (!(parent.getParent() instanceof CtBlock)){
parent = parent.getParent();
}
return (CtStatement) parent;
}
示例11: elementUsesGUIParam
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
public static boolean elementUsesGUIParam(final CtElement elt, final List<CtParameterReference<?>> guiParams, final CtBlock<?> mainBlock,
final Set<CtElement> alreadyVisited) {
if(alreadyVisited.contains(elt)) {
return false;
}
// Check whether a GUI parameter is directly used in the statement.
//FIXME 5.5
// if(guiParams.stream().anyMatch(param -> elt.getElements(new BasicFilter<>(CtParameterReference.class)).stream().noneMatch(ref -> ref.equals(param)))){
if(guiParams.stream().anyMatch(param -> !elt.getReferences(new DirectReferenceFilter<>(param)).isEmpty())) {
return true;
}
alreadyVisited.add(elt);
// Otherwise, looking for local variables that use a GUI parameter.
return elt.getElements(new LocalVariableAccessFilter()).stream().
anyMatch(var ->
// Maybe the declaration of the variable refers to a GUI parameter
elementUsesGUIParam(var.getDeclaration(), guiParams, mainBlock, alreadyVisited) ||
// or an assignment of this variable in the main block refers to a GUI parameter
// 1. Looking for the assignments in the block
mainBlock.getElements(new BasicFilter<>(CtAssignment.class)).stream().
// 2. Keeping only the variable write
anyMatch(assig -> assig.getAssigned() instanceof CtVariableWrite<?> &&
// 3. Checking that the assigned variable is our current variable
((CtVariableWrite<?>)assig.getAssigned()).getVariable().equals(var) &&
// 4. Checking that the assignment directly or indirectly refers to GUI parameter
elementUsesGUIParam(assig.getAssignment(), guiParams, mainBlock, alreadyVisited)));
}
示例12: asBlock
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
public static CtBlock<?> asBlock(CtStatement statement, CtElement parent) {
CtBlock<?> block;
if (isBlock(statement)) {
block = (CtBlock<?>) statement;
} else {
block = newBlock(statement.getFactory(), statement);
}
setParent(parent, block);
return block;
}
示例13: insertBeforeUnderSameParent
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
public static void insertBeforeUnderSameParent(CtStatement toBeInserted, CtStatement insertionPoint) {
CtElement parent;
if (isBlock(insertionPoint)) {
CtBlock<?> block = (CtBlock<?>) insertionPoint;
block.insertBegin(toBeInserted);
parent = block;
} else {
insertionPoint.insertBefore(toBeInserted);
parent = insertionPoint.getParent();
}
setParent(parent, toBeInserted);
}
示例14: matches
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
@Override
public boolean matches(CtElement element) {
SourcePosition position = element.getPosition();
if (position != null && position != SourcePosition.NOPOSITION) {
return FileLibrary.isSameFile(classSourceFile(), position.getFile()) && codeSnippet().equals(element.toString());
}
return false;
}
示例15: removingLocalVarsIfs
import spoon.reflect.declaration.CtElement; //导入依赖的package包/类
private void removingLocalVarsIfs() {
CtBlock<?> body = cmd.getExecutable().getBody();
List<CtIf> iffs = body.getElements(new BasicFilter<>(CtIf.class));
int oldsize;
IntegerProperty cpt = new SimpleIntegerProperty();
Set<CtElement> remain = cmd.getStatements().stream().filter(stat -> !stat.isMainEntry()).flatMap(s -> s.getStatmts().stream()).collect(Collectors.toSet());
do {
// Removing all the empty if statements.
cpt.set(0);
oldsize = iffs.size();
iffs.stream().filter(ctif ->
(ctif.getThenStatement() == null || ctif.getThenStatement() instanceof CtBlock && ((CtBlock<?>) ctif.getThenStatement()).getStatements().isEmpty()) &&
(ctif.getElseStatement() == null || ctif.getElseStatement() instanceof CtBlock && ((CtBlock<?>) ctif.getElseStatement()).getStatements().isEmpty())).
forEach(ctif -> ctif.delete());
iffs = body.getElements(new BasicFilter<>(CtIf.class));
// Removing all the unused local vars
Set<CtElement> toRemove = new HashSet<>();
remain.stream().filter(r -> r instanceof CtLocalVariable).forEach(var -> {
if(body.getElements(new MyVariableAccessFilter((CtVariable<?>) var)).stream().
allMatch(access -> remain.stream().allMatch(r -> r != var && r.getElements(new FindElementFilter(access, false)).isEmpty()))) {
var.delete();
cpt.set(cpt.get() + 1);
toRemove.add(var);
}
});
remain.removeAll(toRemove);
// Doing that until no more removal.
}while(cpt.get()>0 || iffs.size()!=oldsize);
}