Java Code Examples for spoon.reflect.visitor.filter.TypeFilter

The following examples show how to use spoon.reflect.visitor.filter.TypeFilter. 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
@SuppressWarnings("all")
@Test
public void main() {
    MavenLauncher launcher = new MavenLauncher(
            "./src/test/resources/project/",
            MavenLauncher.SOURCE_TYPE.APP_SOURCE);

    CtModel model = launcher.buildModel();
    List<CtMethod> methodList = model.
            filterChildren(new NamedElementFilter<CtPackage>(CtPackage.class, "ow2con")).
            filterChildren(new NamedElementFilter<CtPackage>(CtPackage.class, "publicapi")).
            filterChildren(new TypeFilter<CtMethod>(CtMethod.class)).
            filterChildren(new Filter<CtMethod>() {
                @Override
                public boolean matches(CtMethod element) {
                    boolean isPublic = element.isPublic();
                    CtTypeReference returnType = element.getType();
                    String privateApiPackage = "ow2con.privateapi";
                    boolean isTypeFromPrivateApi = returnType.getQualifiedName().contains(privateApiPackage);
                    return isPublic && isTypeFromPrivateApi;
                }
            }).list();
}
 
Example 2
@Test
public void testReferenceProcessor() throws Exception {
	final String[] args = {
			"-i", "src/test/resources/factory/",
			"-o", "target/spooned/"
	};

	final Launcher launcher = new Launcher();
	launcher.setArgs(args);
	launcher.run();

	final Factory factory = launcher.getFactory();
	final ProcessingManager processingManager = new QueueProcessingManager(factory);
	List<CtInterface> listFactoryItf = factory.getModel().getElements(new NamedElementFilter<>(CtInterface.class, "Factory"));
	assertThat(listFactoryItf.size(), is(1));

	final FactoryProcessor processor = new FactoryProcessor(listFactoryItf.get(0).getReference());
	processingManager.addProcessor(processor);

	List<CtConstructorCall> ctNewClasses = factory.getModel().getElements(new TypeFilter<>(CtConstructorCall.class));
	processingManager.process(ctNewClasses);

	// implicit constructor is also counted
	assertThat(processor.listWrongUses.size(), is(2));
}
 
Example 3
Source Project: coming   Source File: AbstractCodeAnalyzer.java    License: MIT License 6 votes vote down vote up
/**
 * Return if the Condition is a guard
 * 
 * @param condition
 * @return
 */
// we want the condition not to be null related check
public boolean checkNormalGuardCondition(CtExpression condition) {
	if (condition != null) {
		List<CtBinaryOperator> binOp = condition.getElements(new TypeFilter<>(CtBinaryOperator.class));
		if (binOp != null && binOp.size() > 0) {

			for (CtBinaryOperator ctBinaryOperator : binOp) {
				if (ctBinaryOperator.getRightHandOperand().toString().equals("null")
						|| ctBinaryOperator.getLeftHandOperand().toString().equals("null")) {

					return false;
				}
			}
		}

		return true;
	}
	return false;
}
 
Example 4
Source Project: coming   Source File: AbstractCodeAnalyzer.java    License: MIT License 6 votes vote down vote up
public boolean checkNullCheckGuardCondition(CtExpression condition) {
	if (condition != null) {
		List<CtBinaryOperator> binOp = condition.getElements(new TypeFilter<>(CtBinaryOperator.class));
		if (binOp != null && binOp.size() > 0) {

			for (CtBinaryOperator ctBinaryOperator : binOp) {
				if (!ctBinaryOperator.getRightHandOperand().toString().equals("null")
						&& !ctBinaryOperator.getLeftHandOperand().toString().equals("null")) {

					return false;
				}
			}

			return true;
		}

		return false;
	}
	return false;
}
 
Example 5
Source Project: coming   Source File: EnhancedRepairAnalyzer.java    License: MIT License 6 votes vote down vote up
public List<CtInvocation> getCandidateCalleeFunction(CtInvocation CE) {
    List<CtInvocation> ret = new ArrayList<>();

    CtMethod ownedMethod = CE.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        List<CtInvocation> invocations = ownedMethod.getElements(new TypeFilter<>(CtInvocation.class));
        for (CtInvocation invocation : invocations) {
            if (CE == invocation) {
                continue;
            }
            if (CE.getExecutable() != invocation.getExecutable()) {
                continue;
            }
            if (CE.getArguments().size() != invocation.getArguments().size()) {
                continue;
            }
            ret.add(invocation);
        }
    }
    return ret;
}
 
Example 6
Source Project: coming   Source File: EnhancedRepairGenerator.java    License: MIT License 6 votes vote down vote up
private Set<CtElement> fuzzyLocator(CtElement statement) {
    Set<CtElement> locations = new HashSet<>();
    if (statement instanceof CtMethod || statement instanceof CtClass || statement instanceof CtIf || statement instanceof CtStatementList) {
        locations.add(statement);
    } else {
        // "int a;" is not CtStatement?
        CtElement parent = statement.getParent();
        if (parent != null) {
            List<CtElement> statements = parent.getElements(new TypeFilter<>(CtElement.class));
            if (parent instanceof CtStatement) {
                statements = statements.subList(1, statements.size());
            }
            int idx = statements.indexOf(statement);
            if (idx >= 0) {
                if (idx > 0)
                    locations.add(statements.get(idx - 1));
                locations.add(statements.get(idx));
                if (idx < statements.size() - 1)
                    locations.add(statements.get(idx + 1));
            }
        }
    }
    return locations;
}
 
Example 7
Source Project: coming   Source File: OriginalRepairGenerator.java    License: MIT License 6 votes vote down vote up
private Set<CtElement> fuzzyLocator(CtElement statement) {
    Set<CtElement> locations = new HashSet<>();
    if (statement instanceof CtMethod || statement instanceof CtClass || statement instanceof CtIf || statement instanceof CtStatementList) {
        locations.add(statement);
    } else {
        // "int a;" is not CtStatement?
        CtElement parent = statement.getParent();
        if (parent != null) {
            List<CtElement> statements = parent.getElements(new TypeFilter<>(CtElement.class));
            if (parent instanceof CtStatement) {
                statements = statements.subList(1, statements.size());
            }
            int idx = statements.indexOf(statement);
            if (idx >= 0) {
                if (idx > 0)
                    locations.add(statements.get(idx - 1));
                locations.add(statements.get(idx));
                if (idx < statements.size() - 1)
                    locations.add(statements.get(idx + 1));
            }
        }
    }
    return locations;
}
 
Example 8
Source Project: coming   Source File: OriginalRepairAnalyzer.java    License: MIT License 6 votes vote down vote up
public List<CtInvocation> getCandidateCalleeFunction(CtInvocation CE) {
    List<CtInvocation> ret = new ArrayList<>();

    CtMethod ownedMethod = CE.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        List<CtInvocation> invocations = ownedMethod.getElements(new TypeFilter<>(CtInvocation.class));
        for (CtInvocation invocation : invocations) {
            if (CE == invocation) {
                continue;
            }
            if (CE.getExecutable() != invocation.getExecutable()) {
                continue;
            }
            if (CE.getArguments().size() != invocation.getArguments().size()) {
                continue;
            }
            ret.add(invocation);
        }
    }
    return ret;
}
 
Example 9
Source Project: coming   Source File: S4RORepairAnalyzer.java    License: MIT License 6 votes vote down vote up
public List<CtInvocation> getCandidateCalleeFunction(CtInvocation CE) {
    List<CtInvocation> ret = new ArrayList<>();

    CtMethod ownedMethod = CE.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        List<CtInvocation> invocations = ownedMethod.getElements(new TypeFilter<>(CtInvocation.class));
        for (CtInvocation invocation : invocations) {
            if (CE == invocation) {
                continue;
            }
            if (CE.getExecutable() != invocation.getExecutable()) {
                continue;
            }
            if (CE.getArguments().size() != invocation.getArguments().size()) {
                continue;
            }
            ret.add(invocation);
        }
    }
    return ret;
}
 
Example 10
Source Project: coming   Source File: S4RORepairGenerator.java    License: MIT License 6 votes vote down vote up
private Set<CtElement> fuzzyLocator(CtElement statement) {
    Set<CtElement> locations = new HashSet<>();
    if (statement instanceof CtMethod || statement instanceof CtClass || statement instanceof CtIf || statement instanceof CtStatementList) {
        locations.add(statement);
    } else {
        // "int a;" is not CtStatement?
        CtElement parent = statement.getParent();
        if (parent != null) {
            List<CtElement> statements = parent.getElements(new TypeFilter<>(CtElement.class));
            if (parent instanceof CtStatement) {
                statements = statements.subList(1, statements.size());
            }
            int idx = statements.indexOf(statement);
            if (idx >= 0) {
                if (idx > 0)
                    locations.add(statements.get(idx - 1));
                locations.add(statements.get(idx));
                if (idx < statements.size() - 1)
                    locations.add(statements.get(idx + 1));
            }
        }
    }
    return locations;
}
 
Example 11
Source Project: coming   Source File: ExtendedRepairAnalyzer.java    License: MIT License 6 votes vote down vote up
public List<CtInvocation> getCandidateCalleeFunction(CtInvocation CE) {
    List<CtInvocation> ret = new ArrayList<>();

    CtMethod ownedMethod = CE.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        List<CtInvocation> invocations = ownedMethod.getElements(new TypeFilter<>(CtInvocation.class));
        for (CtInvocation invocation : invocations) {
            if (CE == invocation) {
                continue;
            }
            if (CE.getExecutable() != invocation.getExecutable()) {
                continue;
            }
            if (CE.getArguments().size() != invocation.getArguments().size()) {
                continue;
            }
            ret.add(invocation);
        }
    }
    return ret;
}
 
Example 12
Source Project: coming   Source File: ExtendedRepairGenerator.java    License: MIT License 6 votes vote down vote up
private Set<CtElement> fuzzyLocator(CtElement statement) {
    Set<CtElement> locations = new HashSet<>();
    if (statement instanceof CtMethod || statement instanceof CtClass || statement instanceof CtIf || statement instanceof CtStatementList) {
        locations.add(statement);
    } else {
        // "int a;" is not CtStatement?
        CtElement parent = statement.getParent();
        if (parent != null) {
            List<CtElement> statements = parent.getElements(new TypeFilter<>(CtElement.class));
            if (parent instanceof CtStatement) {
                statements = statements.subList(1, statements.size());
            }
            int idx = statements.indexOf(statement);
            if (idx >= 0) {
                if (idx > 0)
                    locations.add(statements.get(idx - 1));
                locations.add(statements.get(idx));
                if (idx < statements.size() - 1)
                    locations.add(statements.get(idx + 1));
            }
        }
    }
    return locations;
}
 
Example 13
Source Project: astor   Source File: LibParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void parseFile(File name) {
   	
   	if(!parsedfile.contains(name.getAbsolutePath())) {
   	  parsedfile.add(name.getAbsolutePath());
   	  
	  for (CtClass<?> klass : this.factory.getModel().getElements(new TypeFilter<CtClass>(CtClass.class))) {
		if(klass.getPosition().getFile().getAbsolutePath().equals(name.getAbsolutePath())) {
			@SuppressWarnings("rawtypes")
			CtClass founded=klass;
			if (!classMap.containsKey(founded.getQualifiedName())) {
				classMap.put(founded.getQualifiedName(), founded);
				List<CtEnum> enumArray = founded.getElements(new TypeFilter<>(CtEnum.class));
                   for(int index=0; index<enumArray.size(); index++) {
                   	enumMap.put(enumArray.get(index).getQualifiedName(), enumArray.get(index));
                   }
			}			
			break;
		}
	  }
      }
}
 
Example 14
private void setfilter() {
	if (cls.equals(CtType.class)) {
		typeFilter = new TypeFilter<CtType>(CtType.class) {
			@Override
			public boolean matches(CtType element) {
				// Definition of "top level" CtType.
				return element.getParent(CtType.class) == null
						&& !element.isImplicit();
			}
		};
	} else if (cls.equals(CtExecutable.class)) {
		typeFilter = new TypeFilter<CtExecutable>(CtExecutable.class) {
			@Override
			public boolean matches(CtExecutable element) {
				// Definition of "top level" CtExecutable.
				return element.getParent(CtExecutable.class) == null
						&& !element.isImplicit()
						&& !(element instanceof CtAnonymousExecutable);
			}
		};
	} else {
		log.error("Invalid clonegranularity");
		throw new IllegalArgumentException();
	}
	log.debug("clonegranularity: " + cls.getName());
}
 
Example 15
Source Project: spoon-examples   Source File: Analyzer.java    License: GNU General Public License v2.0 5 votes vote down vote up
List<CtLocalVariable> analyze(CtMethod testMethod) {
	TypeFilter filterLocalVar =
			new TypeFilter(CtLocalVariable.class) {
				public boolean matches(CtLocalVariable localVariable) {
					return !localVariable.getType().isPrimitive();
				}
			};
	return testMethod.getElements(filterLocalVar);
}
 
Example 16
Source Project: coming   Source File: TypeaccessAnalyzer.java    License: MIT License 5 votes vote down vote up
private void analyzeC3C4_SimilarTypeAccessActualVar(CtElement element, Cntx<Object> context, 
		List<CtTypeAccess> typeaccessaaffected, CtClass parentClass) {
	
	try {
		List<CtTypeAccess> typeaccesss = new ArrayList();
		if(parentClass!=null)
		    typeaccesss = parentClass.getElements(new TypeFilter<>(CtTypeAccess.class));

		for (CtTypeAccess virtualtypeaccess : typeaccessaaffected) {
			
			boolean c3CurrentOtherTypeAccessActualVar = false;
			boolean c4CurrentOtherSimilarTypeAccessActualVar = false;
			
			if(isTypeAccessActualVar(virtualtypeaccess)) {
				c3CurrentOtherTypeAccessActualVar=true;
				
				for(CtTypeAccess certaintypeaccess: typeaccesss) {
					if(isTypeAccessActualVar(certaintypeaccess)) {
						if(whetherSimilarTypeAccessActualVar(virtualtypeaccess, certaintypeaccess)) {
							c4CurrentOtherSimilarTypeAccessActualVar=true;
							break;
						}
					}
				}
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(virtualtypeaccess), CodeFeatures.C3_TYPEACCESS_ACTUAL_VAR,
					c3CurrentOtherTypeAccessActualVar, "FEATURES_TYPEACCESS");
			
			writeGroupedInfo(context, adjustIdentifyInJson(virtualtypeaccess), CodeFeatures.C4_SIMILAR_TYPEACCESS_ACTUAL_VAR,
					c4CurrentOtherSimilarTypeAccessActualVar, "FEATURES_TYPEACCESS");
		}

	} catch (Throwable e) {
		e.printStackTrace();
	}	
}
 
Example 17
Source Project: coming   Source File: VariableAnalyzer.java    License: MIT License 5 votes vote down vote up
private void analyzeV17_IsEnum (List<CtVariableAccess> varsAffected, Cntx<Object> context, CtClass parentClass) {
	try {
		
		if (parentClass == null)
			return;
		// Get all enums
		List<CtEnum> enums = parentClass.getElements(new TypeFilter<>(CtEnum.class));

		// For each var access
		for (CtVariableAccess varAccess : varsAffected) {
			
			boolean isVarAccessTypeEnum = false;
			
			if (varAccess.getVariable().getType() != null
					&& enums.contains(varAccess.getVariable().getType().getDeclaration())) {
				isVarAccessTypeEnum = true;
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(varAccess), 
					CodeFeatures.V17_VAR_IS_ENUMERATION,
					(isVarAccessTypeEnum), "FEATURES_VARS");
		}

	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 18
Source Project: coming   Source File: EnhancedRepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public List<CtElement> getCondCandidateVars(CtElement element) {
    List<CtElement> ret = new ArrayList<>();
    // Global variables
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtVariableAccess.class)));
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtArrayAccess.class)));
    }
    // Local variables
    CtMethod ownedMethod = element.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        ret.addAll(ownedMethod.getElements(new TypeFilter<>(CtLocalVariable.class)));
    }
    return ret;
}
 
Example 19
Source Project: coming   Source File: EnhancedRepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public List<CtElement> getCandidateLocalVars(CtElement element, Type type) {
    List<CtElement> ret = new ArrayList<>();
    CtMethod ownedMethod = element.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        for (CtLocalVariable tmp : ownedMethod.getElements(new TypeFilter<>(CtLocalVariable.class))) {
            if (tmp.getClass().getGenericSuperclass() == type) {
                ret.add(tmp);
            }
        }
    }
    return ret;
}
 
Example 20
Source Project: coming   Source File: EnhancedRepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public Set<CtElement> getGlobalCandidateExprs(CtElement element) {
    Set<CtElement> ret = new HashSet<>();
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtExpression.class)));
    }
    return ret;
}
 
Example 21
Source Project: coming   Source File: EnhancedRepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public Set<CtElement> getGlobalCandidateIfStmts(CtElement element) {
    Set<CtElement> ret = new HashSet<>();
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtIf.class)));
    }
    return ret;
}
 
Example 22
Source Project: coming   Source File: OriginalRepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public List<CtElement> getCondCandidateVars(CtElement element) {
    List<CtElement> ret = new ArrayList<>();
    // Global variables
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtVariableAccess.class)));
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtArrayAccess.class)));
    }
    // Local variables
    CtMethod ownedMethod = element.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        ret.addAll(ownedMethod.getElements(new TypeFilter<>(CtLocalVariable.class)));
    }
    return ret;
}
 
Example 23
Source Project: coming   Source File: OriginalRepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public List<CtElement> getCandidateLocalVars(CtElement element, Type type) {
    List<CtElement> ret = new ArrayList<>();
    CtMethod ownedMethod = element.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        for (CtLocalVariable tmp : ownedMethod.getElements(new TypeFilter<>(CtLocalVariable.class))) {
            if (tmp.getClass().getGenericSuperclass() == type) {
                ret.add(tmp);
            }
        }
    }
    return ret;
}
 
Example 24
Source Project: coming   Source File: OriginalRepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public Set<CtElement> getGlobalCandidateExprs(CtElement element) {
    Set<CtElement> ret = new HashSet<>();
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtExpression.class)));
    }
    return ret;
}
 
Example 25
Source Project: coming   Source File: OriginalRepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public Set<CtElement> getGlobalCandidateIfStmts(CtElement element) {
    Set<CtElement> ret = new HashSet<>();
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtIf.class)));
    }
    return ret;
}
 
Example 26
Source Project: coming   Source File: S4RORepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public List<CtElement> getCondCandidateVars(CtElement element) {
    List<CtElement> ret = new ArrayList<>();
    // Global variables
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtVariableAccess.class)));
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtArrayAccess.class)));
    }
    // Local variables
    CtMethod ownedMethod = element.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        ret.addAll(ownedMethod.getElements(new TypeFilter<>(CtLocalVariable.class)));
    }
    return ret;
}
 
Example 27
Source Project: coming   Source File: S4RORepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public List<CtElement> getCandidateLocalVars(CtElement element, Type type) {
    List<CtElement> ret = new ArrayList<>();
    CtMethod ownedMethod = element.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        for (CtLocalVariable tmp : ownedMethod.getElements(new TypeFilter<>(CtLocalVariable.class))) {
            if (tmp.getClass().getGenericSuperclass() == type) {
                ret.add(tmp);
            }
        }
    }
    return ret;
}
 
Example 28
Source Project: coming   Source File: S4RORepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public Set<CtElement> getGlobalCandidateExprs(CtElement element) {
    Set<CtElement> ret = new HashSet<>();
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtExpression.class)));
    }
    return ret;
}
 
Example 29
Source Project: coming   Source File: S4RORepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public Set<CtElement> getGlobalCandidateIfStmts(CtElement element) {
    Set<CtElement> ret = new HashSet<>();
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtIf.class)));
    }
    return ret;
}
 
Example 30
Source Project: coming   Source File: ExtendedRepairAnalyzer.java    License: MIT License 5 votes vote down vote up
public List<CtElement> getCondCandidateVars(CtElement element) {
    List<CtElement> ret = new ArrayList<>();
    // Global variables
    CtClass ownedClass = element.getParent(new TypeFilter<>(CtClass.class));
    if (ownedClass != null) {
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtVariableAccess.class)));
        ret.addAll(ownedClass.getElements(new TypeFilter<>(CtArrayAccess.class)));
    }
    // Local variables
    CtMethod ownedMethod = element.getParent(new TypeFilter<>(CtMethod.class));
    if (ownedMethod != null) {
        ret.addAll(ownedMethod.getElements(new TypeFilter<>(CtLocalVariable.class)));
    }
    return ret;
}