spoon.reflect.declaration.CtTypedElement Java Examples

The following examples show how to use spoon.reflect.declaration.CtTypedElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VariablesInSuspiciousCollector.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
/**
 * get all dependencies added by a CtTypedElement
 *
 * @param element
 * @return all dependencies added by element
 */
private List<CtTypeReference<?>> getDependencies(CtTypedElement<?> element) {
    List<CtTypeReference<?>> listDependencies = new ArrayList<>();
    // Literal
    if (element instanceof CtAnnotation) {
        return listDependencies;
    }
    if (element instanceof CtLiteral<?>) {
        CtLiteral<?> literal = (CtLiteral<?>) element;
        literal.getValue();
        if (literal.getValue() instanceof CtTypeReference<?>) {
            listDependencies.add((CtTypeReference<?>) literal.getValue());
        } else if (literal.getValue() instanceof CtTypedElement<?>) {
            listDependencies.add(((CtTypedElement<?>) literal.getValue())
                    .getType());
        }
    }
    // method invocation
    if (element instanceof CtInvocation<?>) {
        CtInvocation<?> invocation = (CtInvocation<?>) element;
        // the class of the method
        listDependencies.add(invocation.getExecutable().getDeclaringType());
    }
    listDependencies.add(element.getType());
    return listDependencies;
}
 
Example #2
Source File: ClassCollector.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
/**
 * get all dependencies added by a CtTypedElement
 *
 * @param element
 * @return all dependencies added by element
 */
private List<CtTypeReference<?>> getDependencies(CtTypedElement<?> element) {
    List<CtTypeReference<?>> listDependencies = new ArrayList<>();
    // Literal
    if (element instanceof CtAnnotation) {
        return listDependencies;
    }
    if (element instanceof CtLiteral<?>) {
        CtLiteral<?> literal = (CtLiteral<?>) element;
        literal.getValue();
        if (literal.getValue() instanceof CtTypeReference<?>) {
            listDependencies.add((CtTypeReference<?>) literal.getValue());
        } else if (literal.getValue() instanceof CtTypedElement<?>) {
            listDependencies.add(((CtTypedElement<?>) literal.getValue())
                    .getType());
        }
    }
    // method invocation
    if (element instanceof CtInvocation<?>) {
        CtInvocation<?> invocation = (CtInvocation<?>) element;
        // the class of the method
        listDependencies.add(invocation.getExecutable().getDeclaringType());
    }
    listDependencies.add(element.getType());
    return listDependencies;
}
 
Example #3
Source File: VariablesInSuspiciousCollector.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtTypedElement<?> candidate) {
    if (candidate.getPosition() == null || candidate.getPosition() instanceof NoSourcePosition) {
        return false;
    }
    return candidate.getPosition().getLine() == location.getLineNumber();
}
 
Example #4
Source File: ClassCollector.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtTypedElement<?> candidate) {
    CtMethod parent = candidate.getParent(CtMethod.class);
    if (parent == null) {
        return false;
    }
    return parent.getSimpleName().equals(buggyMethod);
}
 
Example #5
Source File: ClassCollector.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void process(CtTypedElement element) {
    List<CtTypeReference<?>> listDependencies = getDependencies(element);

    CtTypeReference<Exception> exception = getFactory().Class().createReference(Exception.class);
    for (CtTypeReference<?> ctTypeReference : listDependencies) {
        if (ctTypeReference == null) {
            continue;
        }
        if (ctTypeReference.isPrimitive()) {
            continue;
        }
        if (ctTypeReference.toString().equals("<nulltype>")) {
            continue;
        }
        if (exception.isSubtypeOf(ctTypeReference)) {
            continue;
        }
        if (classes.contains(ctTypeReference.getQualifiedName())) {
            return;
        }
        if (classes.add(ctTypeReference.getQualifiedName())) {
            logger.debug("[class] " + ctTypeReference.getQualifiedName());
        }
    }

}
 
Example #6
Source File: LiteralReplacer.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void process(CtStatement ctStatement) {
    if (!(ctStatement instanceof CtLocalVariable<?>) && !(ctStatement instanceof CtAssignment<?, ?>))
        return;

    Class<?> localVariableClass = ((CtTypedElement<?>) ctStatement).getType().getActualClass();
    if (localVariableClass.equals(Integer.class) || localVariableClass.equals(int.class)) {
        if (ctStatement instanceof CtAssignment<?, ?>) {
            replaceInteger(((CtAssignment<?, ?>) ctStatement).getAssignment());
        } else {
            replaceInteger(((CtLocalVariable<?>) ctStatement).getDefaultExpression());
        }
    } else if (localVariableClass.equals(Double.class) || localVariableClass.equals(double.class)) {
        if (ctStatement instanceof CtAssignment<?, ?>) {
            replaceDouble(((CtAssignment<?, ?>) ctStatement).getAssignment());
        } else {
            replaceDouble(((CtLocalVariable<?>) ctStatement).getDefaultExpression());
        }
    } else if (localVariableClass.equals(Boolean.class) || localVariableClass.equals(boolean.class)) {
        if (ctStatement instanceof CtAssignment<?, ?>) {
            replaceBoolean(((CtAssignment<?, ?>) ctStatement).getAssignment());
        } else {
            replaceBoolean(((CtLocalVariable<?>) ctStatement).getDefaultExpression());
        }
    }
}
 
Example #7
Source File: SpoonElementsCollector.java    From nopol with GNU General Public License v2.0 4 votes vote down vote up
public SpoonElementsCollector(Set<CtTypedElement> elements, NopolContext nopolContext) {
    this.elements = elements;
    this.nopolContext = nopolContext;
}
 
Example #8
Source File: VariablesInSuspiciousCollector.java    From nopol with GNU General Public License v2.0 4 votes vote down vote up
public Set<CtTypedElement> getVariables() {
    return variables;
}
 
Example #9
Source File: Cardumen.java    From coming with MIT License 4 votes vote down vote up
private String getType(CtElement element) {
    CtTypeReference type = ((CtTypedElement) element).getType();
    if (type == null)
        return "<nulltype>";
    return type.toString();
}
 
Example #10
Source File: Arja.java    From coming with MIT License 4 votes vote down vote up
private String getType(CtElement element) {
	CtTypeReference type = ((CtTypedElement) element).getType();
	if (type == null)
		return "<nulltype>";
	return type.toString();
}