Java Code Examples for spoon.reflect.reference.CtFieldReference

The following examples show how to use spoon.reflect.reference.CtFieldReference. 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: VariableAnalyzer.java    License: MIT License 6 votes vote down vote up
/**
 * For each involved variable, is it constant?–can assume variables whose
 * identifier names are majorly capital letters are constant variables
 * 
 * @param varsAffected
 * @param element
 * @param context
 */
private void analyzeV3_AffectedHasConstant(List<CtVariableAccess> varsAffected, CtElement element,
		Cntx<Object> context) {
	try {
		
		for (CtVariableAccess aVarAffected : varsAffected) {
			boolean currentIsConstant = false;
			if (aVarAffected.getVariable() instanceof CtFieldReference &&
			// Check if it's uppercase
					aVarAffected.getVariable().getSimpleName().toUpperCase()
							.equals(aVarAffected.getVariable().getSimpleName())) {
				currentIsConstant = true;
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(aVarAffected), 
					CodeFeatures.V3_HAS_CONSTANT,
					currentIsConstant, "FEATURES_VARS");
		}
	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 2
@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 3
Source Project: nopol   Source File: SpoonReferenceLibrary.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static Collection<CtField<?>> accessibleFieldsFrom(CtTypeReference<?> accessingType, CtTypeReference<?> accessedType) {
    Collection<CtField<?>> accessibleFields = MetaList.newLinkedList();
    try {
        Collection<CtFieldReference<?>> allFields = accessedType.getAllFields();
        for (CtFieldReference<?> field : allFields) {
            CtField<?> actualField = field.getDeclaration();
            if (actualField != null && isVisibleFrom(accessingType, actualField, field.getDeclaringType(), accessedType)) {
                accessibleFields.add(actualField);
            }
        }
    } catch (Throwable e) {
        logWarning(logger(), e.toString());
    }
    return accessibleFields;
}
 
Example 4
Source Project: coming   Source File: VariableResolver.java    License: MIT License 4 votes vote down vote up
/**
 * Returns all variables in scope, reachable from the ctelement passes as
 * argument
 * 
 * @param element
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<CtVariable> searchVariablesInScope(CtElement element) {
	List<CtVariable> variables = new ArrayList();

	if (element == null) {
		return variables;
	}

	if (element instanceof CtField) {
		return variables;
	}
	// We find the CtClass and returns the fields
	CtClass ctclass = element.getParent(CtClass.class);
	if (ctclass != null) {
		Collection<CtFieldReference<?>> vars = ctclass.getAllFields();
		for (CtFieldReference<?> ctFieldReference : vars) {
			// We dont add private fields from parent classes
			if ((!ctFieldReference.getModifiers().contains(ModifierKind.PRIVATE)
					|| ctclass.getFields().contains(ctFieldReference.getDeclaration()))) {

				// We ignore "serialVersionUID'
				if ((ctFieldReference.getDeclaration() != null)
						&& !"serialVersionUID".equals(ctFieldReference.getDeclaration().getSimpleName()))
					variables.add(ctFieldReference.getDeclaration());
			}
		}

	}

	// We find the parent method and we extract the parameters
	CtMethod method = element.getParent(CtMethod.class);
	if (method != null) {
		List<CtParameter> pars = method.getParameters();
		for (CtParameter ctParameter : pars) {
			variables.add(ctParameter);
		}
	}

	// We find the parent block and we extract the local variables before
	// the element under analysis
	CtBlock parentblock = element.getParent(CtBlock.class);
	if (parentblock != null) {
		int positionEl = parentblock.getStatements().indexOf(element);
		variables.addAll(VariableResolver.retrieveLocalVariables(positionEl, parentblock));
	}

	return variables;

}
 
Example 5
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns all variables in scope, reachable from the ctelement passes as
 * argument
 * 
 * @param element
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<CtVariable> searchVariablesInScope(CtElement element) {
	List<CtVariable> variables = new ArrayList();

	if (element == null) {
		return variables;
	}

	if (element instanceof CtField) {
		return variables;
	}
	// We find the CtClass and returns the fields
	CtClass ctclass = element.getParent(CtClass.class);
	if (ctclass != null) {
		Collection<CtFieldReference<?>> vars = ctclass.getAllFields();
		for (CtFieldReference<?> ctFieldReference : vars) {
			// We dont add private fields from parent classes
			if ((!ctFieldReference.getModifiers().contains(ModifierKind.PRIVATE)
					|| ctclass.getFields().contains(ctFieldReference.getDeclaration()))) {

				// We ignore "serialVersionUID'
				if ((ctFieldReference.getDeclaration() != null)
						&& !"serialVersionUID".equals(ctFieldReference.getDeclaration().getSimpleName()))
					variables.add(ctFieldReference.getDeclaration());
			}
		}

	}

	// We find the parent method and we extract the parameters
	CtMethod method = element.getParent(CtMethod.class);
	if (method != null) {
		List<CtParameter> pars = method.getParameters();
		for (CtParameter ctParameter : pars) {
			variables.add(ctParameter);
		}
	}

	// We find the parent block and we extract the local variables before
	// the element under analysis
	CtBlock parentblock = element.getParent(CtBlock.class);
	CtElement currentElement = element;
	if (parentblock != null) {
		int positionEl = parentblock.getStatements().indexOf(currentElement);
		variables.addAll(VariableResolver.retrieveLocalVariables(positionEl, parentblock));
		if (ConfigurationProperties.getPropertyBool("consideryvarloops")) {
			variables.addAll(getVarsInFor(currentElement));
			variables.addAll(getVarsInForEach(currentElement));
		}

	}

	return variables;

}
 
Example 6
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adapt the ingredient to the destination according to the mapping. We directly
 * manipulate the variables from the ingredient, which are stored in VarMapping
 * 
 * @param varMapping
 * @param destination
 * @return it returns the original variable reference of each converted variable
 */
@SuppressWarnings("unchecked")
public static Map<VarAccessWrapper, CtVariableAccess> convertIngredient(VarMapping varMapping,
		Map<String, CtVariable> mapToFollow) {

	Map<VarAccessWrapper, CtVariableAccess> originalMap = new HashMap<>();

	Map<VarAccessWrapper, List<CtVariable>> mappedVars = varMapping.getMappedVariables();
	for (VarAccessWrapper var : mappedVars.keySet()) {
		CtVariable varNew = mapToFollow.get(var.getVar().getVariable().getSimpleName());
		//
		CtVariableReference newVarReference = varNew.getReference();

		CtVariableAccess originalVarAccessDestination = var.getVar();
		CtVariableAccess newVarAccessDestination = null;

		// if the var to reference is a local or parameter
		if (newVarReference instanceof CtLocalVariableReference
				|| newVarReference instanceof CtParameterReference) {
			// let's check the destination Writes or Reads
			if (originalVarAccessDestination instanceof CtFieldWrite
					|| originalVarAccessDestination instanceof CtVariableWrite) {
				// We replace the Write by a Var writter
				newVarAccessDestination = MutationSupporter.getFactory().Core().createVariableWrite();
				newVarAccessDestination.setVariable(newVarReference);

			} else { // read
				newVarAccessDestination = MutationSupporter.getFactory().Code().createVariableRead(newVarReference,
						varNew.hasModifier(ModifierKind.STATIC));
			}

		} else
		// else, if we want to reference a field
		if (newVarReference instanceof CtFieldReference) {
			// let's check the destination, write or read
			if (originalVarAccessDestination instanceof CtFieldWrite<?>
					|| originalVarAccessDestination instanceof CtFieldRead<?>) {
				newVarAccessDestination = MutationSupporter.getFactory().Core().createFieldWrite();

			} else {
				newVarAccessDestination = MutationSupporter.getFactory().Core().createFieldRead();

			}
			newVarAccessDestination.setVariable(newVarReference);
		}
		// At the end, for all cases:
		if (newVarAccessDestination != null) {
			originalMap.put(new VarAccessWrapper(newVarAccessDestination), originalVarAccessDestination);
			originalVarAccessDestination.replace(newVarAccessDestination);
		} else {
			logger.error("No destination resolved");
		}

	} // end for
	return originalMap;
}
 
Example 7
Source Project: coming   Source File: VariableResolver.java    License: MIT License 3 votes vote down vote up
public static boolean isStatic(CtVariableReference varref) {

		if (!(varref instanceof CtFieldReference)) {
			return false;
		}

		CtFieldReference fieldRef = (CtFieldReference) varref;

		return fieldRef.isStatic();

	}
 
Example 8
Source Project: astor   Source File: VariableResolver.java    License: GNU General Public License v2.0 3 votes vote down vote up
public static boolean isStatic(CtVariableReference varref) {

		if (!(varref instanceof CtFieldReference)) {
			return false;
		}

		CtFieldReference fieldRef = (CtFieldReference) varref;

		return fieldRef.isStatic();

	}