spoon.reflect.code.CtVariableRead Java Examples

The following examples show how to use spoon.reflect.code.CtVariableRead. 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: NumericVariableMetaMutator.java    From metamutator with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Accept Numeric Variable
 */
@Override
public boolean isToBeProcessed(CtVariableRead candidate) {
	
	// SKIP not declared variable and Finale variable
	if(candidate.getVariable() == null) return false;

	if(candidate.getVariable().getSimpleName().equals("class")) return false;

	if(candidate.getVariable().getModifiers().contains(ModifierKind.FINAL)) return false;
	
	
	// SKIP IF VARIABLE IS CASTED
	if(candidate.getTypeCasts().size() > 0) return false;
	for(CtTypeReference type : candidate.getReferencedTypes()) {
		if(!this.isNumber(type)) return false;
	}
	
	if( candidate.getParent().getClass().equals(CtUnaryOperatorImpl.class)) return false;
	
	if(this.isNumber(candidate.getVariable().getType())){
		return true;
	}
	return false;
}
 
Example #2
Source File: NumericVariableMetaMutator.java    From metamutator with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Add AbsoluteValue, Plus, Minus, Increment or Decrement Unary Operator on Numeric Variable 
 */
@Override
public void process(CtVariableRead candidate) {
	thisIndex++;
	
	String expression = "(";
	for(UNARY unary : absSet){
		if(unary.equals(UNARY.INIT)) continue;
		/*expression += PREFIX+thisIndex + ".is(\"" + unary.toString() + "\")?( " + UnaryEquivalent(unary)  + candidate.toString() + ")):";*/
		expression += PREFIX+thisIndex + ".is(" + unary.getClass().getCanonicalName()+'.'+unary.toString()+ ")?( " + UnaryEquivalent(unary)  + candidate.getVariable().getSimpleName() + ")):";
	}
	expression += "(" + candidate.toString() + "))";
	CtCodeSnippetExpression<Boolean> codeSnippet = getFactory().Core()
			.createCodeSnippetExpression();
	codeSnippet.setValue(expression);
	candidate.replace(codeSnippet);
	
	Selector.generateSelector(candidate, UNARY.INIT, thisIndex, absSet, PREFIX);
}
 
Example #3
Source File: VarLiPlaceholderGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public List<VarLiPlaceholder> createTOS(T ingredientStatement) {
	List<VarLiPlaceholder> results = new ArrayList<>();
	List<CtVariableAccess> varAccessCollected = VariableResolver.collectVariableAccess(ingredientStatement, true);
	for (CtVariableAccess ctVariableAccess : varAccessCollected) {

		int i = 0;

		if (ctVariableAccess instanceof CtVariableRead) {

			if (!(ctVariableAccess instanceof CtFieldRead)
					|| !((CtFieldReference) ctVariableAccess.getVariable()).isStatic()) {

				VarLiPlaceholder pc = new VarLiPlaceholder(ctVariableAccess,
						"_lit_" + ctVariableAccess.getType().getSimpleName() + "_" + i);
				results.add(pc);
			}
		}
	}
	return results;
}
 
Example #4
Source File: StaSynthBuilder.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public List<CtExpression> createNotNullExpr(List<CtVariableRead> cteVarReadList) {

		List<CtExpression> result = new ArrayList<>();

		for (CtVariableRead varr : cteVarReadList) {

			if (!varr.getType().isPrimitive()) {
				CtBinaryOperatorImpl binex = new CtBinaryOperatorImpl<>();
				binex.setKind(BinaryOperatorKind.NE);
				binex.setLeftHandOperand(varr);
				binex.setRightHandOperand(nullExp);
				result.add(binex);
			}
		}
		return result;
	}
 
Example #5
Source File: IntegerConstantReplacementMetaMutator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isToBeProcessed(CtVariableRead element){
	try {
		if((element.getType().toString().contains("int"))&&
				(!(element.getVariable().getDeclaration().getDefaultExpression() == null))){
					if(!(element.getVariable().getDeclaration().getDefaultExpression().toString().contains(PREFIX))
							&& (!element.getVariable().getDeclaration().getModifiers().contains(ModifierKind.STATIC)))
								return true;				
		}
	} catch (Exception e) {

	}
	return false;	
}
 
Example #6
Source File: VariableResolver.java    From coming with MIT License 5 votes vote down vote up
public static List<CtVariableAccess> collectVariableRead(CtElement element) {
	List<CtVariableAccess> varaccess = new ArrayList<>();
	List<String> varaccessCacheNames = new ArrayList<>();
	CtScanner sc = new CtScanner() {

		public void add(CtVariableAccess e) {
			if (!varaccessCacheNames.contains(e.getVariable().getSimpleName()))
				varaccess.add(e);
			varaccessCacheNames.add(e.getVariable().getSimpleName());
		}

		@Override
		public <T> void visitCtVariableRead(CtVariableRead<T> variableRead) {
			super.visitCtVariableRead(variableRead);
			add(variableRead);
		}

		@Override
		public <T> void visitCtTypeAccess(CtTypeAccess<T> typeAccess) {
			super.visitCtTypeAccess(typeAccess);
			// varaccess.add(typeAccess);
		}

		@Override
		public <T> void visitCtFieldRead(CtFieldRead<T> fieldRead) {
			super.visitCtFieldRead(fieldRead);
			add(fieldRead);
		}

	};

	sc.scan(element);

	return varaccess;

}
 
Example #7
Source File: ExpressionTransform.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> void visitCtVariableRead(CtVariableRead<T> variableRead) {
	
	String type = variableRead.getType().getQualifiedName();
	type = type.replaceAll("\\d","");
	@SuppressWarnings("rawtypes")
	CtExpression exp = null;
	exp = ExpressionGenerator.fetchEXP(this.mutSupporter, this.modificationPoint, type, querytype);
			
	if (exp != null)
		candidates.put(variableRead, exp);
}
 
Example #8
Source File: StaSynthBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public List synthesizer(List<CtExpression> singleExpressions, List<CtVariableRead> cteVarReadList) {
	NopolContext fakeContext = fakeContext();

	List<CtInvocation> invocations = this.createtinvocations(cteVarReadList, singleExpressions);

	List<CtExpression> notnull = createNotNullExpr(cteVarReadList);

	for (CtExpression notNullExpression : notnull) {
		// System.out.println("not null: " + notNullExpression);
	}

	singleExpressions.addAll(invocations);

	long maxCombinerTime = TimeUnit.SECONDS.toMillis(10);
	// Passing expression from Collection to Candidates...

	for (CtExpression expr : singleExpressions) {
		// System.out.println(expr + " type " + expr.getType());
	}
	System.out.println("Starting calculating expressions");
	List<CtExpression> allCombinedExpression = new ArrayList<>();
	DataCombinerSpoon combiner = new DataCombinerSpoon();

	combiner.addCombineListener(new DataCombinatorListenerSpoon(allCombinedExpression));

	combiner.combine(singleExpressions, maxCombinerTime, fakeContext);

	List<CtExpression> result = new ArrayList<>();

	System.out.println("Space: expressions: " + singleExpressions.size() + " not null " + notnull.size()
			+ " all combined " + allCombinedExpression.size());
	result.addAll(singleExpressions);
	result.addAll(notnull);
	result.addAll(allCombinedExpression);

	System.out.println("End calculating space " + result.size());
	return result;

}
 
Example #9
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static List<CtVariableAccess> collectVariableRead(CtElement element) {
	List<CtVariableAccess> varaccess = new ArrayList<>();
	List<String> varaccessCacheNames = new ArrayList<>();
	CtScanner sc = new CtScanner() {

		public void add(CtVariableAccess e) {
			if (!varaccessCacheNames.contains(e.getVariable().getSimpleName()))
				varaccess.add(e);
			varaccessCacheNames.add(e.getVariable().getSimpleName());
		}

		@Override
		public <T> void visitCtVariableRead(CtVariableRead<T> variableRead) {
			super.visitCtVariableRead(variableRead);
			add(variableRead);
		}

		@Override
		public <T> void visitCtTypeAccess(CtTypeAccess<T> typeAccess) {
			super.visitCtTypeAccess(typeAccess);
			// varaccess.add(typeAccess);
		}

		@Override
		public <T> void visitCtFieldRead(CtFieldRead<T> fieldRead) {
			super.visitCtFieldRead(fieldRead);
			add(fieldRead);
		}

	};

	sc.scan(element);

	return varaccess;

}
 
Example #10
Source File: NumericExpressionMetaMutatorTest.java    From metamutator with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testNumericExpressionMetaMutator() throws Exception {
    // build the model and apply the transformation
    Launcher l = new Launcher();
    l.addInputResource("src/test/java/resources/Foo.java");
    l.addProcessor(new NumericVariableMetaMutator());
    l.run();

    // now we get the code of Foo
    CtClass c = l.getFactory().Package().getRootPackage().getElements(new NamedElementFilter<>(CtClass.class, "Foo")).get(0);
    
    // printing the metaprogram
    System.out.println("// Metaprogram: ");
    System.out.println(c.toString());

    // we prepare an interpreter for the transformed code
    Interpreter bsh = new Interpreter();
    // creating a new instance of the class
    Object o = ((Class)bsh.eval(c.toString())).newInstance();
    
    // test with the second mutation hotspot
    Selector sel1=Selector.getSelectorByName(NumericVariableMetaMutator.PREFIX + "1");
    
    sel1.choose(0);// INIT B
    assertEquals(-1, invokeExactMethod(o, "add", new Object[] {3, -4}));   
    sel1.choose(1);// ABS B
    assertEquals(1, invokeExactMethod(o, "add", new Object[] {3, -4}));  
    sel1.choose(2);// MINUS B
    assertEquals(-6, invokeExactMethod(o, "add", new Object[] {3, 3})); 
    sel1.choose(3);// INC B
    assertEquals(0, invokeExactMethod(o, "add", new Object[] {3, -4}));  
    sel1.choose(4);// DEC B
    assertEquals(-2, invokeExactMethod(o, "add", new Object[] {3, -4}));
    
    NumericVariableMetaMutator numericPROC = new NumericVariableMetaMutator();
   
    CtVariableRead candidate = l.getFactory().Core().createVariableRead();

    // TEST IsNumeric() on typeReference
    // TEST GOOD TYPE
    CtTypeReference type =  l.getFactory().Core().createTypeReference().setSimpleName(int.class.getName());
    assertEquals(true,numericPROC.isNumber(type));
    type =  l.getFactory().Core().createTypeReference().setSimpleName(long.class.getName());
    assertEquals(true,numericPROC.isNumber(type));
    type =  l.getFactory().Core().createTypeReference().setSimpleName(byte.class.getName());
    assertEquals(true,numericPROC.isNumber(type));
    type =  l.getFactory().Core().createTypeReference().setSimpleName(float.class.getName());
    assertEquals(true,numericPROC.isNumber(type));
    type =  l.getFactory().Core().createTypeReference().setSimpleName(double.class.getName());
    assertEquals(true,numericPROC.isNumber(type));
    
    // TEST NOT ALLOW TYPE
    type =  l.getFactory().Core().createTypeReference().setSimpleName(String.class.getName());
    assertEquals(false,numericPROC.isNumber(type));
    type =  l.getFactory().Core().createTypeReference().setSimpleName(boolean.class.getName());
    assertEquals(false,numericPROC.isNumber(type));
    type =  l.getFactory().Core().createTypeReference().setSimpleName(Object.class.getName());
    assertEquals(false,numericPROC.isNumber(type));
    type =  l.getFactory().Core().createTypeReference().setSimpleName(List.class.getName());
    assertEquals(false,numericPROC.isNumber(type));
}
 
Example #11
Source File: VariableResolver.java    From coming with MIT License 4 votes vote down vote up
/**
 * Return all variables related to the element passed as argument
 * 
 * @param element
 * @return
 */
public static List<CtVariableAccess> collectVariableAccess(CtElement element, boolean duplicates) {
	List<CtVariableAccess> varaccess = new ArrayList<>();
	List<String> varaccessCacheNames = new ArrayList<>();
	CtScanner sc = new CtScanner() {

		public void add(CtVariableAccess e) {
			if (duplicates || !varaccessCacheNames.contains(e.getVariable().getSimpleName()))
				varaccess.add(e);
			varaccessCacheNames.add(e.getVariable().getSimpleName());
		}

		@Override
		public <T> void visitCtVariableRead(CtVariableRead<T> variableRead) {
			super.visitCtVariableRead(variableRead);
			add(variableRead);
		}

		@Override
		public <T> void visitCtVariableWrite(CtVariableWrite<T> variableWrite) {
			super.visitCtVariableWrite(variableWrite);
			add(variableWrite);
		}

		@Override
		public <T> void visitCtTypeAccess(CtTypeAccess<T> typeAccess) {
			super.visitCtTypeAccess(typeAccess);
			// varaccess.add(typeAccess);
		}

		@Override
		public <T> void visitCtFieldRead(CtFieldRead<T> fieldRead) {
			super.visitCtFieldRead(fieldRead);
			add(fieldRead);
		}

		@Override
		public <T> void visitCtFieldWrite(CtFieldWrite<T> fieldWrite) {
			super.visitCtFieldWrite(fieldWrite);
			add(fieldWrite);
		}

	};

	sc.scan(element);

	return varaccess;

}
 
Example #12
Source File: VariableResolver.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return all variables related to the element passed as argument
 * 
 * @param element
 * @return
 */
public static List<CtVariableAccess> collectVariableAccess(CtElement element, boolean duplicates) {
	List<CtVariableAccess> varaccess = new ArrayList<>();
	List<String> varaccessCacheNames = new ArrayList<>();
	CtScanner sc = new CtScanner() {

		public void add(CtVariableAccess e) {
			if (duplicates || !varaccessCacheNames.contains(e.getVariable().getSimpleName()))
				varaccess.add(e);
			varaccessCacheNames.add(e.getVariable().getSimpleName());
		}

		@Override
		public <T> void visitCtVariableRead(CtVariableRead<T> variableRead) {
			super.visitCtVariableRead(variableRead);
			add(variableRead);
		}

		@Override
		public <T> void visitCtVariableWrite(CtVariableWrite<T> variableWrite) {
			super.visitCtVariableWrite(variableWrite);
			add(variableWrite);
		}

		@Override
		public <T> void visitCtTypeAccess(CtTypeAccess<T> typeAccess) {
			super.visitCtTypeAccess(typeAccess);
			// varaccess.add(typeAccess);
		}

		@Override
		public <T> void visitCtFieldRead(CtFieldRead<T> fieldRead) {
			super.visitCtFieldRead(fieldRead);
			add(fieldRead);
		}

		@Override
		public <T> void visitCtFieldWrite(CtFieldWrite<T> fieldWrite) {
			super.visitCtFieldWrite(fieldWrite);
			add(fieldWrite);
		}

	};

	sc.scan(element);

	return varaccess;

}