Java Code Examples for spoon.reflect.visitor.CtScanner

The following examples show how to use spoon.reflect.visitor.CtScanner. 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: coming   Source File: VariableResolver.java    License: 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 2
Source Project: astor   Source File: InvocationResolver.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return all variables related to the element passed as argument
 * 
 * @param element
 * @return
 */
@SuppressWarnings("rawtypes")
public static List<CtAbstractInvocation> collectInvocation(CtElement element, boolean duplicates) {
	List<CtAbstractInvocation> varaccess = new ArrayList<>();

	CtScanner sc = new CtScanner() {

		public void add(CtAbstractInvocation e) {
			if (duplicates || !varaccess.contains(e))
				varaccess.add(e);
		}

		@Override
		public <T> void visitCtInvocation(CtInvocation<T> invocation) {
			super.visitCtInvocation(invocation);
			add(invocation);
		}

		@Override
		public <T> void visitCtConstructorCall(CtConstructorCall<T> ctConstructorCall) {
			super.visitCtConstructorCall(ctConstructorCall);
			add(ctConstructorCall);
		}

	};

	sc.scan(element);

	return varaccess;

}
 
Example 3
Source Project: astor   Source File: VariableResolver.java    License: 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 4
Source Project: coming   Source File: VariableAnalyzer.java    License: MIT License 4 votes vote down vote up
/**
 * For each involved variable, is there any other variable in scope that is a
 * certain function transformation of the involved variable
 * 
 * @param varsAffected
 * @param element
 * @param context
 */
@SuppressWarnings("rawtypes")
private void analyzeV5_AffectedVariablesInTransformation(List<CtVariableAccess> varsAffected, CtElement element,
		Cntx<Object> context) {
	try {
		CtMethod methodParent = element.getParent(CtMethod.class);

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

		CtScanner assignmentScanner = new CtScanner() {

			@Override
			public <T, A extends T> void visitCtAssignment(CtAssignment<T, A> assignement) {
				if (assignement.getAssignment() != null)
					assignments.add(assignement.getAssignment());
			}

			@Override
			public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) {
				if (localVariable.getAssignment() != null)
					assignments.add(localVariable.getAssignment());
			}

		};
		assignmentScanner.scan(methodParent);

		for (CtVariableAccess variableAffected : varsAffected) {

			boolean v5_currentVarHasvar = false;

			for (CtExpression assignment : assignments) {

				if (!isElementBeforeVariable(variableAffected, assignment))
					continue;

				// let's collect the var access in the right part
				List<CtVariableAccess> varsInRightPart = VariableResolver.collectVariableRead(assignment); // VariableResolver.collectVariableAccess(assignment);

				// if the var access in the right is the same that the affected
				for (CtVariableAccess varInAssign : varsInRightPart) {
					if (hasSameName(variableAffected, varInAssign)) {

						v5_currentVarHasvar = true;
						break;
					}
				}
			}

			writeGroupedInfo(context, adjustIdentifyInJson(variableAffected), 
					CodeFeatures.V5_HAS_VAR_IN_TRANSFORMATION,
					(v5_currentVarHasvar), "FEATURES_VARS");
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 5
Source Project: coming   Source File: LogicalExpressionAnalyzer.java    License: MIT License 4 votes vote down vote up
private void analyzeLE9_BothNULLAndNormal(List<CtExpression> logicalExperssions, Cntx<Object> context) {
	
	try {
		
		List<BinaryOperatorKind> logicOperators = Arrays.asList(BinaryOperatorKind.OR, BinaryOperatorKind.AND);

		for (int indexlogical=0; indexlogical<logicalExperssions.size(); indexlogical++) {
			
               CtExpression logicalexpression = logicalExperssions.get(indexlogical);	
			
			List<CtBinaryOperator> binOps = new ArrayList();
			
			CtScanner scanner = new CtScanner() {
				@Override
				public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
					super.visitCtBinaryOperator(operator);
					
					if (logicOperators.contains(operator.getKind()))
						binOps.add(operator);
				}
			};
			
			scanner.scan(logicalexpression);
			
			List<CtExpression> atomicboolexperssions = new ArrayList();
			
			for(int index=0; index<binOps.size(); index++) {
				CtExpression left= binOps.get(index).getLeftHandOperand();
				CtExpression right=binOps.get(index).getRightHandOperand();
				if(!wheteherCompundBoolExper(binOps, left))
					atomicboolexperssions.add(left);
				if(!wheteherCompundBoolExper(binOps, right))
					atomicboolexperssions.add(right);
			}
			
			boolean whethercontainnormalcheck=false;
			boolean whethercontainnullcheck=false;
			boolean equalnullcheck=false;
			boolean notequalnullcheck=false;
			
			for(int index=0; index<atomicboolexperssions.size(); index++) {
				
				if(!whethercontainnormalcheck) {
				  if(checkNormalGuardCondition(atomicboolexperssions.get(index)))
					  whethercontainnormalcheck=true;
				}
				
			    if(checkNullCheckGuardCondition(atomicboolexperssions.get(index))) {
			    	
					 whethercontainnullcheck=true;
				     List<CtBinaryOperator> specificbinOps = atomicboolexperssions.get(index).getElements(new TypeFilter<>(CtBinaryOperator.class));
 
				     for(int binopindex=0; binopindex<specificbinOps.size(); binopindex++) {
				    	 
				    	 if(specificbinOps.get(binopindex).getKind().toString().equals(BinaryOperatorKind.EQ.toString()))
				    		 equalnullcheck=true;
				    	 if(specificbinOps.get(binopindex).getKind().toString().equals(BinaryOperatorKind.NE.toString()))
				    		 notequalnullcheck=true;
				    	 
				    	 if(equalnullcheck && notequalnullcheck)
				    		 break;
				     }   
				}	
			    
			    if(whethercontainnormalcheck && whethercontainnullcheck && equalnullcheck && notequalnullcheck)
			    	break;
			}
			
			writeGroupedInfo(context, "logical_expression_"+Integer.toString(indexlogical)+"_"+logicalexpression,  
					CodeFeatures.LE9_NORMAL_CHECK,
					(whethercontainnormalcheck && !whethercontainnullcheck), "FEATURES_LOGICAL_EXPRESSION");
			
			writeGroupedInfo(context, "logical_expression_"+Integer.toString(indexlogical)+"_"+logicalexpression,  
					CodeFeatures.LE9_NULL_CHECK,
					(!whethercontainnormalcheck && whethercontainnullcheck), "FEATURES_LOGICAL_EXPRESSION");
			
			writeGroupedInfo(context, "logical_expression_"+Integer.toString(indexlogical)+"_"+logicalexpression,  
					CodeFeatures.LE9_MIX_CHECK,
					(whethercontainnormalcheck && whethercontainnullcheck), "FEATURES_LOGICAL_EXPRESSION");
			
			writeGroupedInfo(context, "logical_expression_"+Integer.toString(indexlogical)+"_"+logicalexpression,  
					CodeFeatures.LE9_EQUAL_NOTEQUAL_NULL_CHECK,
					(equalnullcheck && notequalnullcheck), "FEATURES_LOGICAL_EXPRESSION");
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 6
Source Project: coming   Source File: LogicalExpressionAnalyzer.java    License: MIT License 4 votes vote down vote up
private void analyzeLE10_Analyze_Atomic_Boolexps(List<CtExpression> logicalExperssions, Cntx<Object> context) {
	
	try {	
		for (int indexlogical=0; indexlogical<logicalExperssions.size(); indexlogical++) {
			
			CtExpression logicalexpression = logicalExperssions.get(indexlogical);
			
			List<BinaryOperatorKind> logicOperators = Arrays.asList(BinaryOperatorKind.OR, BinaryOperatorKind.AND);
			List<CtBinaryOperator> binOps = new ArrayList();
			
			CtScanner scanner = new CtScanner() {
				@Override
				public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
					super.visitCtBinaryOperator(operator);
					
					if (logicOperators.contains(operator.getKind()))
						binOps.add(operator);
				}
			};
			
			ExpressionCapturerScanner scanner2 = new ExpressionCapturerScanner();
			scanner2.scan(logicalexpression);
			if (scanner2.toScan != null) {
				scanner.scan(scanner2.toScan);
			} else {
				scanner.scan(logicalexpression);
			}
			
			List<CtExpression> atomicboolexperssions = new ArrayList();
			
			for(int index=0; index<binOps.size(); index++) {
				CtExpression left= binOps.get(index).getLeftHandOperand();
				CtExpression right=binOps.get(index).getRightHandOperand();
				if(!wheteherCompundBoolExper(binOps, left))
					atomicboolexperssions.add(left);
				if(!wheteherCompundBoolExper(binOps, right))
					atomicboolexperssions.add(right);
			}
			
			if(binOps.size()==0)
				atomicboolexperssions.add(logicalexpression);
			
			analyzeExpressions(atomicboolexperssions, context, indexlogical,logicalexpression);
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 7
Source Project: coming   Source File: BinaryOperatorAnalyzer.java    License: MIT License 4 votes vote down vote up
private boolean scannotoperator (CtExpression expressiontostudy) {
	
	List<String> unaryOps = new ArrayList();
	
	CtScanner scanner = new CtScanner() {

		@Override
		public <T> void visitCtUnaryOperator(CtUnaryOperator<T> operator) {

			super.visitCtUnaryOperator(operator);
			unaryOps.add(operator.getKind().toString());
		}
	};
	
	scanner.scan(expressiontostudy);
	
	return unaryOps.contains(UnaryOperatorKind.NOT.toString());
}
 
Example 8
Source Project: coming   Source File: VariableResolver.java    License: 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 9
Source Project: coming   Source File: VariableResolver.java    License: MIT License 4 votes vote down vote up
public static List<CtLiteral> collectLiterals(CtElement element) {

		List<CtLiteral> literalsValues = new ArrayList<>();

		CtScanner scanner = new CtScanner() {

			@Override
			public <T> void visitCtLiteral(CtLiteral<T> literal) {

				super.visitCtLiteral(literal);
				if (!literalsValues.contains(literal))
					literalsValues.add(literal);
			}

		};

		scanner.scan(element);

		return literalsValues;
	}
 
Example 10
Source Project: coming   Source File: VariableResolver.java    License: MIT License 4 votes vote down vote up
public static List<CtLiteral> collectLiteralsNoString(CtElement element) {

		List<CtLiteral> literalsValues = new ArrayList<>();

		CtScanner scanner = new CtScanner() {

			@Override
			public <T> void visitCtLiteral(CtLiteral<T> literal) {

				super.visitCtLiteral(literal);
				if (!literalsValues.contains(literal) && !"String".equals(literal.getType().getSimpleName()))
					literalsValues.add(literal);
			}

		};

		scanner.scan(element);

		return literalsValues;
	}
 
Example 11
Source Project: astor   Source File: VariableResolver.java    License: 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;

}
 
Example 12
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static List<CtLiteral> collectLiterals(CtElement element) {

		List<CtLiteral> literalsValues = new ArrayList<>();

		CtScanner scanner = new CtScanner() {

			@Override
			public <T> void visitCtLiteral(CtLiteral<T> literal) {

				super.visitCtLiteral(literal);
				if (!literalsValues.contains(literal))
					literalsValues.add(literal);
			}

		};

		scanner.scan(element);

		return literalsValues;
	}
 
Example 13
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static List<CtLiteral> collectLiteralsNoString(CtElement element) {

		List<CtLiteral> literalsValues = new ArrayList<>();

		CtScanner scanner = new CtScanner() {

			@Override
			public <T> void visitCtLiteral(CtLiteral<T> literal) {

				super.visitCtLiteral(literal);
				if (!literalsValues.contains(literal) && !"String".equals(literal.getType().getSimpleName()))
					literalsValues.add(literal);
			}

		};

		scanner.scan(element);

		return literalsValues;
	}