spoon.reflect.code.CtFieldAccess Java Examples

The following examples show how to use spoon.reflect.code.CtFieldAccess. 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: VariablePlaceholder.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply() {
	olderNameOfVariable.clear();
	for (String placeholder : palceholdersToVariables.keySet()) {

		List<CtVariableAccess> variables = palceholdersToVariables.get(placeholder);
		for (CtVariableAccess variableUnderAnalysis : variables) {
			this.olderNameOfVariable.put(variableUnderAnalysis,
					variableUnderAnalysis.getVariable().getSimpleName());
			variableUnderAnalysis.getVariable().setSimpleName(placeholder);
			// workaround: Problems with var Shadowing
			variableUnderAnalysis.getFactory().getEnvironment().setNoClasspath(true);
			if (variableUnderAnalysis instanceof CtFieldAccess) {
				CtFieldAccess fieldAccess = (CtFieldAccess) variableUnderAnalysis;
				fieldAccess.getVariable().setDeclaringType(null);

			}
		}
	}
}
 
Example #2
Source File: MetaGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private static List<CtExpression<?>> filterParameter(List<CtExpression<?>> realParameters) {
	List<CtExpression<?>> parametersN = new ArrayList<>();
	HashMap<String, CtVariableAccess> seen = new HashMap();
	for (CtExpression<?> ctParameter : realParameters) {
		if (ctParameter instanceof CtVariableAccess) {
			CtVariableAccess va = (CtVariableAccess) ctParameter;
			if (!seen.containsKey(va.getVariable().getSimpleName())) {
				parametersN.add(ctParameter);
				seen.put(va.getVariable().getSimpleName(), va);
			} else {
				// we have a var with the same name
				CtVariableAccess other = seen.get(va.getVariable().getSimpleName());
				if (other instanceof CtFieldAccess && !(va instanceof CtFieldAccess)) {
					// replace the field access by the other var access
					int ind = parametersN.indexOf(other);
					parametersN.remove(ind);
					parametersN.add(ind, va);
					seen.put(va.getVariable().getSimpleName(), va);
				}
			}
		}
	}

	return parametersN;
}
 
Example #3
Source File: VarLiPlaceholder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void apply() {
	// previousVariable.replace(newLiteral);
	previousname = affectedVariable.getVariable().getSimpleName();
	affectedVariable.getVariable().setSimpleName(placeholder_name);

	//Workarround 
	affectedVariable.getFactory().getEnvironment().setNoClasspath(true);
	if (affectedVariable instanceof CtFieldAccess) {
		CtFieldAccess fieldAccess = (CtFieldAccess) affectedVariable;
		fieldAccess.getVariable().setDeclaringType(null);

	}
}
 
Example #4
Source File: ExpressionTypeIngredientSpace.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void formatIngredient(CtElement ingredientCtElement) {

	// log.debug("\n------" + ingredientCtElement);
	List<CtVariableAccess> varAccessCollected = VariableResolver.collectVariableAccess(ingredientCtElement, true);
	Map<String, String> varMappings = new HashMap<>();
	int nrvar = 0;
	for (int i = 0; i < varAccessCollected.size(); i++) {
		CtVariableAccess var = varAccessCollected.get(i);

		if (VariableResolver.isStatic(var.getVariable())) {
			continue;
		}

		String abstractName = "";
		if (!varMappings.containsKey(var.getVariable().getSimpleName())) {
			String currentTypeName = var.getVariable().getType().getSimpleName();
			if (currentTypeName.contains("?")) {
				// Any change in case of ?
				abstractName = var.getVariable().getSimpleName();
			} else {
				abstractName = "_" + currentTypeName + "_" + nrvar;
			}
			varMappings.put(var.getVariable().getSimpleName(), abstractName);
			nrvar++;
		} else {
			abstractName = varMappings.get(var.getVariable().getSimpleName());
		}

		var.getVariable().setSimpleName(abstractName);
		// workaround: Problems with var Shadowing
		var.getFactory().getEnvironment().setNoClasspath(true);
		if (var instanceof CtFieldAccess) {
			CtFieldAccess fieldAccess = (CtFieldAccess) var;
			fieldAccess.getVariable().setDeclaringType(null);
		}

	}

}