spoon.reflect.visitor.filter.TypeFilter Java Examples

The following examples show how to use spoon.reflect.visitor.filter.TypeFilter. 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: CloneIngredientSearchStrategy4Exhaustive.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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 #2
Source File: EnhancedRepairAnalyzer.java    From coming with 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 #3
Source File: S4RORepairAnalyzer.java    From coming with 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 #4
Source File: S4RORepairGenerator.java    From coming with 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 #5
Source File: EnhancedRepairGenerator.java    From coming with 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 #6
Source File: OriginalRepairGenerator.java    From coming with 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 File: AbstractCodeAnalyzer.java    From coming with 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 #8
Source File: AbstractCodeAnalyzer.java    From coming with 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 #9
Source File: OriginalRepairAnalyzer.java    From coming with 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 File: ExtendedRepairAnalyzer.java    From coming with 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 #11
Source File: ExtendedRepairGenerator.java    From coming with 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 #12
Source File: FactoryProcessorTest.java    From spoon-examples with GNU General Public License v2.0 6 votes vote down vote up
@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 #13
Source File: QueryExampleTest.java    From spoon-examples with GNU General Public License v2.0 6 votes vote down vote up
@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 #14
Source File: LibParser.java    From astor with 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 #15
Source File: OriginalRepairAnalyzer.java    From coming with 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 #16
Source File: OriginalRepairAnalyzer.java    From coming with 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 #17
Source File: S4RORepairAnalyzer.java    From coming with 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 #18
Source File: OriginalRepairAnalyzer.java    From coming with 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 #19
Source File: S4RORepairAnalyzer.java    From coming with 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 File: S4RORepairAnalyzer.java    From coming with 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 File: S4RORepairAnalyzer.java    From coming with 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 File: ExtendedRepairAnalyzer.java    From coming with 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 File: ExtendedRepairAnalyzer.java    From coming with 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 File: ExtendedRepairAnalyzer.java    From coming with 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 File: ExtendedRepairAnalyzer.java    From coming with 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 File: ReturnExpresionMutOp.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean canBeAppliedToPoint(ModificationPoint point) {

	return (point.getCodeElement() instanceof CtReturn)
			&& (point.getCodeElement().getElements(new TypeFilter(CtBinaryOperator.class)).size() > 0
					|| point.getCodeElement().getElements(new TypeFilter(CtUnaryOperator.class)).size() > 0);

}
 
Example #27
Source File: LogicRedOperator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean canBeAppliedToPoint(ModificationPoint point) {

	// See that the modification points are statements
	return (point.getCodeElement() instanceof CtStatement
			// Let's check we have a binary expression
			&& (point.getCodeElement() instanceof CtBinaryOperator
					// the element has a binary expression
					|| point.getCodeElement().getElements(new TypeFilter<>(CtBinaryOperator.class)).size() > 0));
}
 
Example #28
Source File: Analyzer.java    From spoon-examples with 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 #29
Source File: OriginalRepairAnalyzer.java    From coming with 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 #30
Source File: EnhancedRepairAnalyzer.java    From coming with 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;
}