use of spoon.SpoonException in project spoon by INRIA.
the class FieldReferenceFunction method apply.
@Override
public void apply(CtElement fieldOrScope, CtConsumer<Object> outputConsumer) {
CtElement scope;
CtField<?> field = this.field;
if (field == null) {
if (fieldOrScope instanceof CtField) {
field = (CtField<?>) fieldOrScope;
} else {
throw new SpoonException("The input of FieldReferenceFunction must be a CtField but is " + fieldOrScope.getClass().getSimpleName());
}
scope = field.getFactory().getModel().getUnnamedModule();
} else {
scope = fieldOrScope;
}
scope.filterChildren(new DirectReferenceFilter<CtFieldReference<?>>(field.getReference())).forEach(outputConsumer);
}
use of spoon.SpoonException in project spoon by INRIA.
the class LocalVariableReferenceFunction method apply.
@Override
public void apply(final CtElement scope, CtConsumer<Object> outputConsumer) {
CtVariable<?> var = targetVariable;
if (var == null) {
if (variableClass.isInstance(scope)) {
var = (CtVariable<?>) scope;
} else {
throw new SpoonException("The input of " + getClass().getSimpleName() + " must be a " + variableClass.getSimpleName() + " but is " + scope.getClass().getSimpleName());
}
}
final CtVariable<?> variable = var;
final String simpleName = variable.getSimpleName();
// the context which knows whether we are scanning in scope of local type or not
final Context context = new Context();
CtQuery scopeQuery;
if (scope == variable) {
// we are starting search from local variable declaration
scopeQuery = createScopeQuery(variable, scope, context);
} else {
// we are starting search later, somewhere deep in scope of variable declaration
final CtElement variableParent = variable.getParent();
/*
* search in parents of searching scope for the variableParent
* 1) to check that scope is a child of variableParent
* 2) to detect if there is an local class between variable declaration and scope
*/
if (scope.map(new ParentFunction()).select(new Filter<CtElement>() {
@Override
public boolean matches(CtElement element) {
if (element instanceof CtType) {
// detected that the search scope is in local class declared in visibility scope of variable
context.nrTypes++;
}
return variableParent == element;
}
}).first() == null) {
// the scope is not under children of localVariable
throw new SpoonException("Cannot search for references of variable in wrong scope.");
}
// search in all children of the scope element
scopeQuery = scope.map(new CtScannerFunction().setListener(context));
}
scopeQuery.select(new Filter<CtElement>() {
@Override
public boolean matches(CtElement element) {
if (variableReferenceClass.isInstance(element)) {
CtVariableReference<?> varRef = (CtVariableReference<?>) element;
if (simpleName.equals(varRef.getSimpleName())) {
// we have found a variable reference of required type in visibility scope of targetVariable
if (context.hasLocalType()) {
// so finally check that found variable reference is really a reference to target variable
return variable == varRef.getDeclaration();
}
// else we can be sure that found reference is reference to variable
return true;
}
}
return false;
}
}).forEach(outputConsumer);
}
use of spoon.SpoonException in project spoon by INRIA.
the class CtQueryImpl method getIndexOfCallerInStackOfLambda.
/**
* JVM implementations reports exception in call of lambda in different way.
* A) the to be called lambda expression whose input parameters are invalid is on top of stack trace
* B) the to be called lambda expression whose input parameters are invalid is NOT in stack trace at all
*
* This method detects actual behavior of JVM, so the code, which decides whether ClassCastException is expected (part of filtering process)
* or unexpected - thrown by clients wrong code works on all JVM implementations
*/
private static int getIndexOfCallerInStackOfLambda() {
CtConsumer<CtType<?>> f = (CtType<?> t) -> {
};
CtConsumer<Object> unchecked = (CtConsumer) f;
Object obj = new Integer(1);
try {
unchecked.accept(obj);
throw new SpoonException("The lambda expression with input type CtType must throw ClassCastException when input type is Integer. Basic CtQuery contract is violated by JVM!");
} catch (ClassCastException e) {
StackTraceElement[] stack = e.getStackTrace();
for (int i = 0; i < stack.length; i++) {
if ("getIndexOfCallerInStackOfLambda".equals(stack[i].getMethodName())) {
// check whether we can detect type of lambda input parameter from CCE
Class<?> detectectedClass = detectTargetClassFromCCE(e, obj);
if (CtType.class.equals(detectectedClass) == false) {
// mark it by negative index, so the query engine will fall back to eating of all CCEs and slow implementation
return -1;
}
return i;
}
}
throw new SpoonException("Spoon cannot detect index of caller of lambda expression in stack trace.", e);
}
}
use of spoon.SpoonException in project spoon by INRIA.
the class CompilationTest method testURLClassLoaderWithOtherResourcesThanOnlyFiles.
@Test
public void testURLClassLoaderWithOtherResourcesThanOnlyFiles() throws Exception {
// contract: Spoon handles URLClassLoader which contain other resources than only files by not adding anything
String file = "target/classes/";
String distantJar = "http://central.maven.org/maven2/fr/inria/gforge/spoon/spoon-core/5.8.0/spoon-core-5.8.0.jar";
File f = new File(file);
URL url = new URL(distantJar);
URL[] urls = new URL[] { f.toURL(), url };
URLClassLoader urlClassLoader = new URLClassLoader(urls);
Launcher launcher = new Launcher();
try {
launcher.getEnvironment().setInputClassLoader(urlClassLoader);
fail();
} catch (SpoonException e) {
assertTrue(e.getMessage().contains("Spoon does not support a URLClassLoader containing other resources than local file."));
}
}
use of spoon.SpoonException in project spoon by INRIA.
the class FileCompilerConfig method initializeCompiler.
@Override
public void initializeCompiler(JDTBatchCompiler compiler) {
JDTBasedSpoonCompiler jdtCompiler = compiler.getJdtCompiler();
List<CompilationUnit> cuList = new ArrayList<>();
InputStream inputStream = null;
try {
for (SpoonFile f : getFiles(compiler)) {
if (compiler.filesToBeIgnored.contains(f.getPath())) {
continue;
}
String fName = f.isActualFile() ? f.getPath() : f.getName();
inputStream = f.getContent();
char[] content = IOUtils.toCharArray(inputStream, jdtCompiler.getEnvironment().getEncoding());
cuList.add(new CompilationUnit(content, fName, null));
IOUtils.closeQuietly(inputStream);
}
} catch (Exception e) {
IOUtils.closeQuietly(inputStream);
throw new SpoonException(e);
}
compiler.setCompilationUnits(cuList.toArray(new CompilationUnit[0]));
}
Aggregations