当前位置: 首页>>代码示例>>Java>>正文


Java CtElement.getParent方法代码示例

本文整理汇总了Java中spoon.reflect.declaration.CtElement.getParent方法的典型用法代码示例。如果您正苦于以下问题:Java CtElement.getParent方法的具体用法?Java CtElement.getParent怎么用?Java CtElement.getParent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在spoon.reflect.declaration.CtElement的用法示例。


在下文中一共展示了CtElement.getParent方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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;
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:23,代码来源:Command.java

示例2: 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();
	}
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:27,代码来源:SpoonHelper.java

示例3: getStatementParentNotCtrlFlow

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
/**
 * Returns the first parent statement or the element before this statement if the statement is a control flow statement.
 * @param elt The element to analyse.
 * @return The found element.
 */
public Optional<CtElement> getStatementParentNotCtrlFlow(@Nullable CtElement elt) {
	CtElement res = elt;
	boolean found = false;

	while(!found && res != null) {
		if(res instanceof CtStatement) {
			found = true;
		}else {
			if(res.isParentInitialized()) {
				CtElement parent = res.getParent();
				// FIXME use CtBodyHolder Spoon 5.5
				if(parent instanceof CtIf || parent instanceof CtSwitch || parent instanceof CtLoop || parent instanceof CtTry ||
					parent instanceof CtCatch || parent instanceof CtCase) {
					found = true;
				}else {
					res = parent;
				}
			}else {
				res = null;
			}
		}
	}

	return Optional.ofNullable(res);
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:31,代码来源:SpoonHelper.java

示例4: 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;
}
 
开发者ID:STAMP-project,项目名称:dspot,代码行数:8,代码来源:TestMethodCallAdder.java

示例5: getParentLine

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
private CtStatement getParentLine(CtElement element) {
	LineFilter lineFilter = new LineFilter();
	if (element instanceof CtStatement) {
		if (lineFilter.matches((CtStatement) element)) {
			return (CtStatement) element;
		}
	}
	return element.getParent(lineFilter);
}
 
开发者ID:SpoonLabs,项目名称:nopol,代码行数:10,代码来源:PatchGenerator.java

示例6: isMethod

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
private boolean isMethod(CtElement ctElement) {
    CtExecutable parent = ctElement.getParent(CtExecutable.class);
    if (parent == null) {
        return false;
    }
    return parent.getSimpleName().equals(buggyMethod);
}
 
开发者ID:SpoonLabs,项目名称:nopol,代码行数:8,代码来源:StatCollector.java

示例7: getFirstStatement

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
private CtStatement getFirstStatement(CtExpression ctElement) {
    CtElement ctParent = ctElement.getParent();
    while (!(ctParent instanceof CtStatement) && ctParent != null) {
        ctParent = ctParent.getParent();
    }
    if (ctParent == null) {
        return null;
    }
    return (CtStatement) ctParent;
}
 
开发者ID:SpoonLabs,项目名称:nopol,代码行数:11,代码来源:LiteralReplacer.java

示例8: getDeclaringMethod

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
private CtExecutable<?> getDeclaringMethod(CtElement elem) {

		if (elem == null) {
			return null;
		} else if (elem.getParent() instanceof CtConstructor) {
			return (CtExecutable<?>) elem.getParent();
		} else if (elem.getParent() instanceof CtMethod) {
			return (CtExecutable<?>) elem.getParent();
		} else {
			return getDeclaringMethod(elem.getParent());
		}
	}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:13,代码来源:DefUse.java

示例9: gatherContainingCondition

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
/**
 * Get expression from the containment hierarchy of the action
 * (while,for,switch,conditional)
 */
private List<CtExpression<?>> gatherContainingCondition(CtCodeElement action, CtMethod<?> method) {

	List<CtExpression<?>> res = new ArrayList<>();

	CtElement parent = action;
	try {
		parent = action.getParent();

		while (parent != null && parent != method) {

			if (parent instanceof CtIf) { // TODO: get the negation too
				CtIf if_ = (CtIf) parent;
				res.add(if_.getCondition());
			} else if (parent instanceof CtWhile) {
				CtWhile while_ = (CtWhile) parent;
				res.add(while_.getLoopingExpression());
			} else if (parent instanceof CtFor) {
				CtFor for_ = (CtFor) parent;
				res.add(for_.getExpression());
			} else if (parent instanceof CtCase) {
				CtCase<?> case_ = (CtCase<?>) parent;
				res.add(case_.getCaseExpression());
			} else if (parent instanceof CtConditional) {// TODO: get the
															// negation too
				CtConditional<?> cond = (CtConditional<?>) parent;
				res.add(cond.getCondition());
			} else if (parent instanceof CtDo) {
				CtDo do_ = (CtDo) parent;
				res.add(do_.getLoopingExpression());
			}

			parent = parent.getParent();
		}
	} catch (ParentNotInitializedException e) {
		System.out.println("Parent init exeption: " + parent);
	}

	return res;
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:44,代码来源:MethodSummary.java

示例10: getSuperConditionalExpressions

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
/**
 * @param elt The element from which the research starts.
 * @return All the conditional expressions from the given element 'elt' up to the given top parent.
 */
public @NotNull List<CtElement> getSuperConditionalExpressions(final @NotNull CtElement elt) {
	CtElement parent = elt.isParentInitialized() ? elt.getParent() : null;
	List<CtElement> conds = new ArrayList<>();

	// Exploring the parents to identify the conditional statements
	while(parent != null) {
		if(parent instanceof CtIf) {
			conds.add(((CtIf) parent).getCondition());
		}else {
			if(parent instanceof CtCase<?>) {
				conds.add(((CtCase<?>) parent).getCaseExpression());
				CtSwitch<?> switzh = parent.getParent(CtSwitch.class);
				if(switzh == null) System.err.println("Cannot find the switch statement from the case statement: " + parent);
				else conds.add(switzh.getSelector());
			}else {
				if(parent instanceof CtWhile) {
					conds.add(((CtWhile) parent).getLoopingExpression());
				}else {
					if(parent instanceof CtDo) {
						conds.add(((CtDo) parent).getLoopingExpression());
					}else {
						if(parent instanceof CtFor) {
							conds.add(((CtFor) parent).getExpression());
						}
					}
				}
			}
		}

		parent = parent.isParentInitialized() ? parent.getParent() : null;
	}

	return conds;
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:39,代码来源:SpoonHelper.java

示例11: getLinePosition

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
public int getLinePosition(final CtElement elt) {
	if(elt == null) return -1;

	SourcePosition pos = elt.getPosition();
	CtElement parent = elt.isParentInitialized() ? elt.getParent() : null;

	while(pos == null && parent != null) {
		pos = parent.getPosition();
		parent = parent.isParentInitialized() ? parent.getParent() : null;
	}

	if(pos == null) return -1;
	return pos.getLine();
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:15,代码来源:SpoonHelper.java

示例12: canBeRepairedByAddingPrecondition

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
public static boolean canBeRepairedByAddingPrecondition(final CtElement element) {
    CtElement parent = element.getParent();
    if (parent == null) {
        return false;
    }
    if (element.toString().startsWith("super(")) {
        return false;
    }
    boolean isCtStatement = element instanceof CtStatement && !(element instanceof CtBlock);
    boolean isCtReturn = element instanceof CtReturn;
    boolean isInsideIf = parent.getParent() instanceof CtIf; // Checking parent isn't enough, parent will be CtBlock and grandpa will be CtIf
    boolean isCtLocalVariable = element instanceof CtLocalVariable;
    boolean isInBlock = parent instanceof CtBlock;
    if (isInBlock) {
        boolean isInMethod = parent.getParent() instanceof CtMethod;
        if (isInMethod) {
            if (((CtBlock) parent).getLastStatement() == element && !((CtMethod) parent.getParent()).getType().box().equals(element.getFactory().Class().VOID)) {
                return false;
            }
        }
    }
    boolean isInsideIfLoopCaseBlock = (parent instanceof CtIf || parent instanceof CtLoop || parent instanceof CtCase || parent instanceof CtBlock);
    boolean isInsideForDeclaration = parent instanceof CtFor ? ((CtFor) (parent)).getForUpdate().contains(element) || ((CtFor) (parent)).getForInit().contains(element) : false;
    boolean isCtSynchronized = element instanceof CtSynchronized;

    boolean result = isCtStatement
            // element instanceof CtClass ||

            // cannot insert code before '{}', for example would try to add code between 'Constructor()' and '{}'
            // element instanceof CtBlock ||
            && !isCtSynchronized
            // cannot insert a conditional before 'return', it won't compile.
            && !(isCtReturn && !(isInsideIf))
            // cannot insert a conditional before a variable declaration, it won't compile if the variable is used
            // later on.
            && !isCtLocalVariable
            // Avoids ClassCastException's. @see spoon.support.reflect.code.CtStatementImpl#insertBefore(CtStatement
            // target, CtStatementList<?> statements)
            && isInsideIfLoopCaseBlock
            // cannot insert if inside update statement in for loop declaration
            && !isInsideForDeclaration
            && isInBlock;
    return result;
}
 
开发者ID:SpoonLabs,项目名称:nopol,代码行数:45,代码来源:SpoonPredicate.java

示例13: getSurroundedBlock

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
private List<CtCodeElement> getSurroundedBlock(Action cmd, CtExpression<?> cond) {
	List<CtCodeElement> res = new ArrayList<>();
	CtUnaryOperator<Boolean> negation = factory.Core().createUnaryOperator();

	List<CtCodeElement> stmts = cmd.getStatements();
	CtCodeElement stmt1 = stmts.get(0);
	CtElement parent = stmt1;
	if (stmt1 != null) {
		try {
			parent = stmt1.getParent();
			while (parent != null && parent != cmd.getSource()) {
				if (parent instanceof CtIf) {
					CtIf if_ = (CtIf) parent;
					negation.setOperand(if_.getCondition());
					negation.setKind(UnaryOperatorKind.NOT);
					if (if_.getCondition() == cond) {
						CtStatement thenPart = if_.getThenStatement();
						res.add(thenPart);
						return res;
					} else if (negation.toString().equals(cond.toString())) {// Handle
																				// the
																				// else
																				// part
																				// as
																				// a
																				// command
						CtStatement elsePart = if_.getElseStatement();
						res.add(elsePart);
						return res;
					}
				} else if (parent instanceof CtWhile) {
					CtWhile while_ = (CtWhile) parent;
					if (while_.getLoopingExpression() == cond) {
						res.add(while_.getBody());
						return res;
					}
				} else if (parent instanceof CtFor) {
					CtFor for_ = (CtFor) parent;
					if (for_.getExpression() == cond) {
						res.add(for_.getBody());
						return res;
					}
				} else if (parent instanceof CtCase) {
					CtCase<?> case_ = (CtCase<?>) parent;
					if (case_.getCaseExpression() == cond) {
						res.addAll(case_.getStatements());
						return res;
					}
				} else if (parent instanceof CtConditional) {
					CtConditional<?> conditional = (CtConditional<?>) parent;
					if (conditional.getCondition() == cond) {
						res.add(conditional.getThenExpression());
						return res;
					}
				} else if (parent instanceof CtDo) {
					CtDo do_ = (CtDo) parent;
					if (do_.getLoopingExpression() == cond) {
						res.add(do_.getBody());
						return res;
					}
				} else if (parent instanceof CtSwitch) {
					CtSwitch<?> switch_ = (CtSwitch<?>) parent;
					if (switch_.getSelector() == cond) {
						res.addAll(switch_.getCases());
						return res;
					}
				}
				parent = parent.getParent();
			}
		} catch (ParentNotInitializedException e) {
			System.out.println("Parent init exeption: " + parent);
		}
	}
	return null;
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:76,代码来源:Command.java

示例14: getsuperConditionalStatements

import spoon.reflect.declaration.CtElement; //导入方法依赖的package包/类
/**
 * Explores the parent of the given statement up to the method definition to identify all the conditional statements that
 * lead to the given one.
 * @param condStat The conditional statement to use.
 * @return The list of all the conditional statements.
 */
private List<CommandConditionEntry> getsuperConditionalStatements(final @NotNull CtElement condStat) {
	CtElement currElt = condStat;
	CtElement parent = currElt.getParent();
	List<CommandConditionEntry> conds = new ArrayList<>();

	// Exploring the parents to identify the conditional statements
	while(parent!=null) {
		if(parent instanceof CtIf) {
			CtIf ctif = (CtIf) parent;
			CtExpression<Boolean> condition = ctif.getCondition();

			// Identifying the block of the if used and adding a condition.
			if(ctif.getThenStatement()==currElt) {
				conds.add(new CommandConditionEntry(condition));
			}else {
				if(ctif.getElseStatement() == currElt) {
					conds.add(new CommandConditionEntry(condition, SpoonHelper.INSTANCE.negBoolExpression(condition)));
				}else {
					LOG.log(Level.SEVERE, "Cannot find the origin of the statement in the if statement " +
						SpoonHelper.INSTANCE.formatPosition(parent.getPosition()) + " + : " + parent);
				}
			}
		}else if(parent instanceof CtSwitch<?>) {
			final CtElement elt = currElt;
			CtSwitch<?> ctswitch = (CtSwitch<?>) parent;
			// Identifying the case statement used and creating a condition.
			// The use of orElse(null) is mandatory here (berk!) to avoid a strange compilation bug with generics and casting.
			CtCase<?> caz = ctswitch.getCases().stream().filter(cas -> cas == elt).findFirst().orElse(null);

			if(caz==null) {
				LOG.log(Level.SEVERE, "Cannot find the origin of the statement " + elt + " in the switch statement " +
						SpoonHelper.INSTANCE.formatPosition(parent.getPosition()) +  " + : " + parent);
			}else {
				conds.add(new CommandConditionEntry(caz.getCaseExpression(),
													SpoonHelper.INSTANCE.createEqExpressionFromSwitchCase(ctswitch, caz)));
			}
		}

		currElt = parent;
		parent = parent.getParent();
	}

	return conds;
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:51,代码来源:CommandAnalyser.java


注:本文中的spoon.reflect.declaration.CtElement.getParent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。