/** * Replaces the element by an statement list containing all unary operators in the element * * @param element */ private void replaceByUnaryBlock(CtElement element) { CtBlock<CtUnaryOperator> opBlock = new CtBlockImpl<CtUnaryOperator>(); for (CtUnaryOperator s : element.getElements(new TypeFilter<CtUnaryOperator>(CtUnaryOperator.class))) if (s.getKind().compareTo(UnaryOperatorKind.PREINC) >= 0) { s.setParent(null); opBlock.addStatement(s); s.setParent(opBlock); } element.replace(opBlock); }
private void replace(CtElement e, CtElement op) { if (e instanceof CtStatement && op instanceof CtStatement) { ((CtStatement) e).replace((CtStatement) op); return; } if (e instanceof CtExpression && op instanceof CtExpression) { ((CtExpression) e).replace((CtExpression) op); return; } throw new IllegalArgumentException(e.getClass() + " " + op.getClass()); }
private <T extends FactoryAccessor> T getReplacement(T replacement, Object parent) { // T ret = replacement.getFactory().Core().clone(replacement); if (replacement instanceof CtElement && parent instanceof CtElement) { ((CtElement) replacement).setParent((CtElement) parent); } return replacement; }
@Override public void scan(final CtElement element) { if (element != null && clazz.isAssignableFrom(element.getClass())) { final T potentialVariable = (T) element; if (name.equals(potentialVariable.getSimpleName())) { // Since the AST is not completely available yet, we can not check if element's // parent (ep) contains the innermost element of `stack` (ie). Therefore, we // have to check if one of the following condition holds: // // 1) Does `stack` contain `ep`? // 2) Is `ep` the body of one of `stack`'s CtExecutable elements? // // The first condition is easy to see. If `stack` contains `ep` then `ep` and // all it's declared variables are in scope of `ie`. Unfortunately, there is a // special case in which a variable (a CtLocalVariable) has been declared in a // block (CtBlock) of, for instance, a method. Such a block is not contained in // `stack`. This peculiarity calls for the second condition. final CtElement parentOfPotentialVariable = potentialVariable.getParent(); for (final ASTPair astPair : stack) { if (astPair.element == parentOfPotentialVariable) { finish(potentialVariable); return; } else if (astPair.element instanceof CtExecutable) { final CtExecutable executable = (CtExecutable) astPair.element; if (executable.getBody() == parentOfPotentialVariable) { finish(potentialVariable); return; } } } } } super.scan(element); }
public int compareTo(CtElement o) { String current = getSignature(); String other = o.getSignature(); if (current.length() <= 0 || other.length() <= 0) { throw new ClassCastException("Unable to compare elements"); } return current.compareTo(other); }
@Override() public void report(Processor processor, Severity severity, CtElement element, String message) { String msg = processor.getClass().getSimpleName() + ">>" + element.getPosition().getFile() + "@[" + element.getPosition().getLine() + "," + element.getPosition().getColumn() + "]:" + message; if (Severity.ERROR.equals(severity)) { logger_.error(msg); } else if (Severity.WARNING.equals(severity)) { logger_.warn(msg); } else { logger_.info(msg); } }
/** * Indicates that contains operation assignment over variables non local to the element * * @param element * @return */ private boolean containNonLocalOperatorAssignment(CtElement element) { // Handling operators assignment for (CtOperatorAssignment op : element.getElements(new TypeFilter<CtOperatorAssignment>(CtOperatorAssignment.class))) { for (CtVariableAccess a : accessOfExpression(op.getAssigned())) { // Add cyclic dependencies if (!declaredInsideBlock.contains(a.getVariable())) return true; } } return false; }
/** * Indicates if the element contains unary operators acting on variables declared outside the * block being degraded * * @param element Element to inspect * @return True if contains */ private boolean containsNonLocalUnaryOperators(CtElement element) { // Handling unary operators for (CtUnaryOperator op : element.getElements(new TypeFilter<CtUnaryOperator>(CtUnaryOperator.class))) { for (CtVariableAccess a : accessOfExpression(op)) { // We don't care about cyclic dependencies to vars declared inside the block being degraded if (!declaredInsideBlock.contains(a.getVariable())) return true; } } return false; }
/** * Dans cette méthode on vérifie si la classe/méthode de l'élément analysé correspondent et si la * vérification particulière correspond.On retourne le résultat sou forme de boolean * * @param candidate l'élément analysé * @return résultat de la vérification */ @Override public boolean isToBeProcessed(CtElement candidate) { CtPackage p = candidate.getParent(CtPackage.class); CtClass c = candidate.getParent(CtClass.class); CtMethod m = candidate.getParent(CtMethod.class); try { if (pecularVerify(candidate)) { if (verifyPackage(p.getSimpleName())) { if (verifyClass(c.getSimpleName())) { if (verifyMethod(m.getSignature())) { return verifyNbApplication(); } } } } } catch (NullPointerException e) { return false; } return false; }
@SuppressWarnings("unchecked") void enter(CtElement e, ASTNode node) { stack.push(new ASTPair(e, node)); if (!(e instanceof CtPackage) || (compilationUnitSpoon.getFile() != null && compilationUnitSpoon .getFile() .getName() .equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION))) { if (compilationunitdeclaration != null) { e.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPositionCtElement(e, node)); } } ASTPair pair = stack.peek(); CtElement current = pair.element; if (current instanceof CtExpression) { while (!casts.isEmpty()) { ((CtExpression<?>) current).addTypeCast(casts.remove(0)); } } if (current instanceof CtStatement && !this.label.isEmpty()) { ((CtStatement) current).setLabel(this.label.pop()); } try { if (e instanceof CtTypedElement && !(e instanceof CtConstructorCall) && node instanceof Expression) { if (((CtTypedElement<?>) e).getType() == null) { ((CtTypedElement<Object>) e) .setType( this.jdtTreeBuilder .getReferencesBuilder() .getTypeReference(((Expression) node).resolvedType)); } } } catch (UnsupportedOperationException ignore) { // For some element, we throw an UnsupportedOperationException when we call setType(). } }