Java Code Examples for spoon.reflect.code.CtTry

The following examples show how to use spoon.reflect.code.CtTry. These examples are extracted from open source projects. 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 Project: nopol   Source File: SpoonModelLibrary.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static CtTry newTryCatch(Factory factory, CtStatement tryBlock, List<CtCatch> catchers, CtElement parent) {
    CtTry tryCatch = factory.Core().createTry();
    tryCatch.setBody(asBlock(tryBlock, tryCatch));
    tryCatch.setCatchers(catchers);
    setParent(tryCatch, catchers);
    setParent(parent, tryCatch);
    return tryCatch;
}
 
Example 2
Source Project: coming   Source File: AbstractCodeAnalyzer.java    License: MIT License 5 votes vote down vote up
public static boolean whethereffectivetrycatch(CtTry trystatement, CtStatement targetstatement) {

		CtBlock tryblock = trystatement.getBody();

		if (tryblock != null) {
			List<CtStatement> trystatements = tryblock.getStatements();
			// if(trystatements.size()>0 && trystatements.get(0)==targetstatement)
			if (trystatements.size() > 0)
				return true;
		}

		return false;
	}
 
Example 3
Source Project: astor   Source File: SingleTryOperator.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean applyModification() {

	CtElement el = MetaGenerator.geOriginalElement(statementToWrap);
	if (!(el instanceof CtStatement)) {
		return false;
	}

	CtStatement original = (CtStatement) el;
	this.setParentBlock(original.getParent(CtBlock.class));

	CtTry tryNew = MutationSupporter.getFactory().createTry();
	List<CtCatch> catchers = new ArrayList<>();
	CtCatch catchEx1 = MutationSupporter.getFactory().createCtCatch("e", Exception.class, new CtBlockImpl());
	catchers.add(catchEx1);
	tryNew.setCatchers(catchers);
	CtBlock tryBoddy = new CtBlockImpl();
	tryNew.setBody(tryBoddy);

	CtStatement stmtC = statementToWrap.clone();

	MutationSupporter.clearPosition(stmtC);
	tryBoddy.addStatement(stmtC);

	//
	super.setOriginal(original);
	super.setModified(tryNew);
	//

	return super.applyModification();
}
 
Example 4
@Override
public void process(CtStatement element) {
	if (!(element instanceof CtBlock || element instanceof CtClass || element instanceof CtMethod
			|| element instanceof CtTry || element instanceof CtCatch) && 
			(element.getParent() instanceof CtBlock) && 
			(!(element.toString().startsWith("super"))
					|| ConfigurationProperties.getPropertyBool("manipulatesuper"))) {
		add(element);
	}
}
 
Example 5
Source Project: nopol   Source File: SpoonModelLibrary.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static <E extends Throwable> CtTry newTryCatch(Factory factory, CtStatement tryBlock, Class<E> exception, String catchName, CtStatement catchBlock, CtElement parent) {
    CtCatch newCatch = newCatch(factory, exception, catchName, catchBlock);
    return newTryCatch(factory, tryBlock, asList(newCatch), parent);
}
 
Example 6
Source Project: coming   Source File: WholeStatementAnalyzer.java    License: MIT License 4 votes vote down vote up
private void analyzeS18_InSynchronizedMethod(CtElement element, Cntx<Object> context) {

		try {
			boolean S18Insynchronizedmethod = false;

			CtStatement parent = element.getParent(new LineFilter());
			CtTry potentionalTryCatch = element.getParent(CtTry.class);

			if (potentionalTryCatch != null && whethereffectivetrycatch(potentionalTryCatch, parent)) {
				context.put(CodeFeatures.S18_In_Synchronized_Method, false);
			} else {
				CtMethod methodParent = element.getParent(CtMethod.class);
				CtClass parentClass = element.getParent(CtClass.class);

				if (methodParent != null) {
					if (methodParent.getModifiers().contains(ModifierKind.SYNCHRONIZED))
						S18Insynchronizedmethod = true;
				}

				if (!S18Insynchronizedmethod && parentClass != null) {

					Set<CtTypeReference<?>> superInterfaces = parentClass.getSuperInterfaces();
					CtTypeReference<?> superType = parentClass.getSuperclass();

					if (superType != null && superType.getQualifiedName().toLowerCase().indexOf("thread") != -1)
						S18Insynchronizedmethod = true;

					if (superInterfaces.size() > 0) {
						for (CtTypeReference specificreference : superInterfaces) {
							if (specificreference != null
									&& specificreference.getQualifiedName().toLowerCase().indexOf("thread") != -1) {
								S18Insynchronizedmethod = true;
								break;
							}
						}
					}
				}

				context.put(CodeFeatures.S18_In_Synchronized_Method, S18Insynchronizedmethod);
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
 
Example 7
Source Project: coming   Source File: WholeStatementAnalyzer.java    License: MIT License 4 votes vote down vote up
/**
 * whether the associated method or class for the faulty line throws exception
 * 
 * @param element
 * @param context
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void analyzeS6S11_Method_Method_Features(CtElement element, Cntx<Object> context) {
	try {

		CtExecutable parentMethod = element.getParent(CtExecutable.class);

		CtClass parentClass = element.getParent(CtClass.class);

		CtStatement parent = element.getParent(new LineFilter());
		CtTry potentionalTryCatch = element.getParent(CtTry.class);

		if (potentionalTryCatch != null && whethereffectivetrycatch(potentionalTryCatch, parent)) {

			context.put(CodeFeatures.S6_METHOD_THROWS_EXCEPTION, false);
			context.put(CodeFeatures.S11_FAULTY_CLASS_EXCEPTION_TYPE, false);

		} else {

			if (parentMethod != null) {
				// Exception
				context.put(CodeFeatures.S6_METHOD_THROWS_EXCEPTION, parentMethod.getThrownTypes().size() > 0);
			}

			boolean s11ExceptionType = false;

			if (parentClass != null) {

				Set<CtTypeReference<?>> superInterfaces = parentClass.getSuperInterfaces();
				CtTypeReference<?> superType = parentClass.getSuperclass();

				if (superType != null && superType.getQualifiedName().toLowerCase().indexOf("exception") != -1)
					s11ExceptionType = true;

				if (superInterfaces.size() > 0) {
					for (CtTypeReference specificreference : superInterfaces) {
						if (specificreference != null
								&& specificreference.getQualifiedName().toLowerCase().indexOf("exception") != -1) {
							s11ExceptionType = true;
							break;
						}
					}
				}
				context.put(CodeFeatures.S11_FAULTY_CLASS_EXCEPTION_TYPE, s11ExceptionType);
			}
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 8
Source Project: astor   Source File: UnwrapfromTryOp.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean canBeAppliedToPoint(ModificationPoint point) {

	return point.getCodeElement() instanceof CtTry;
}
 
Example 9
Source Project: astor   Source File: UnwrapfromTryOp.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<OperatorInstance> createOperatorInstances(ModificationPoint modificationPoint) {

	log.debug("Unwrap Try:");

	CtTry trytoremove = (targetElement == null) ? (CtTry) modificationPoint.getCodeElement()
			: (CtTry) targetElement;

	CtBlock blockOfTry = trytoremove.getBody().clone();
	MutationSupporter.clearPosition(blockOfTry);

	List<OperatorInstance> opInstances = new ArrayList<>();

	OperatorInstance opInstace = new StatementOperatorInstance(modificationPoint, this, trytoremove, blockOfTry);
	opInstances.add(opInstace);

	return opInstances;
}
 
Example 10
Source Project: astor   Source File: UnwrapfromTryOp.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean checkTargetCompatibility(CtElement target) {

	return target instanceof CtTry;
}