-
-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VariableReferenceQuery and SubtypeFilter
- Loading branch information
1 parent
d6199c3
commit f2b18ff
Showing
5 changed files
with
633 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/spoon/reflect/visitor/filter/SubtypeFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright (C) 2006-2016 INRIA and contributors | ||
* Spoon - http://spoon.gforge.inria.fr/ | ||
* | ||
* This software is governed by the CeCILL-C License under French law and | ||
* abiding by the rules of distribution of free software. You can use, modify | ||
* and/or redistribute the software under the terms of the CeCILL-C license as | ||
* circulated by CEA, CNRS and INRIA at http://www.cecill.info. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. | ||
* | ||
* The fact that you are presently reading this means that you have had | ||
* knowledge of the CeCILL-C license and that you accept its terms. | ||
*/ | ||
package spoon.reflect.visitor.filter; | ||
|
||
import spoon.reflect.declaration.CtType; | ||
import spoon.reflect.reference.CtTypeReference; | ||
import spoon.reflect.visitor.Filter; | ||
|
||
/** | ||
* Matches all CtType elements which are sub type of {@link #superType} | ||
* Call {@link #includingSelf(boolean)} with value false, if instance of {@link #superType} should no match this {@link Filter} | ||
*/ | ||
public class SubtypeFilter implements Filter<CtType<?>> { | ||
|
||
private CtTypeReference<?> superType; | ||
private String superTypeQualifiedName; | ||
|
||
public SubtypeFilter(CtTypeReference<?> superType) { | ||
this.superType = superType; | ||
} | ||
|
||
/** | ||
* @param includingSelf if false then element which is equal to to #superType is not matching | ||
*/ | ||
public SubtypeFilter includingSelf(boolean includingSelf) { | ||
if (includingSelf) { | ||
superTypeQualifiedName = null; | ||
} else { | ||
superTypeQualifiedName = superType.getQualifiedName(); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean matches(CtType<?> mayBeSubType) { | ||
if (superTypeQualifiedName != null && superTypeQualifiedName.equals(mayBeSubType.getQualifiedName())) { | ||
//we should not accept superType | ||
return false; | ||
} | ||
return mayBeSubType.isSubtypeOf(superType); | ||
} | ||
} |
158 changes: 158 additions & 0 deletions
158
src/main/java/spoon/reflect/visitor/query/VariableReferenceQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/** | ||
* Copyright (C) 2006-2016 INRIA and contributors | ||
* Spoon - http://spoon.gforge.inria.fr/ | ||
* | ||
* This software is governed by the CeCILL-C License under French law and | ||
* abiding by the rules of distribution of free software. You can use, modify | ||
* and/or redistribute the software under the terms of the CeCILL-C license as | ||
* circulated by CEA, CNRS and INRIA at http://www.cecill.info. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. | ||
* | ||
* The fact that you are presently reading this means that you have had | ||
* knowledge of the CeCILL-C license and that you accept its terms. | ||
*/ | ||
package spoon.reflect.visitor.query; | ||
|
||
import java.util.List; | ||
|
||
import spoon.SpoonException; | ||
import spoon.reflect.code.CtCatch; | ||
import spoon.reflect.code.CtCatchVariable; | ||
import spoon.reflect.code.CtLocalVariable; | ||
import spoon.reflect.code.CtStatement; | ||
import spoon.reflect.code.CtStatementList; | ||
import spoon.reflect.declaration.CtElement; | ||
import spoon.reflect.declaration.CtExecutable; | ||
import spoon.reflect.declaration.CtField; | ||
import spoon.reflect.declaration.CtParameter; | ||
import spoon.reflect.declaration.CtVariable; | ||
import spoon.reflect.declaration.ModifierKind; | ||
import spoon.reflect.reference.CtCatchVariableReference; | ||
import spoon.reflect.reference.CtFieldReference; | ||
import spoon.reflect.reference.CtLocalVariableReference; | ||
import spoon.reflect.reference.CtParameterReference; | ||
import spoon.reflect.reference.CtVariableReference; | ||
import spoon.reflect.visitor.CtScanner; | ||
import spoon.reflect.visitor.chain.CtConsumableFunction; | ||
import spoon.reflect.visitor.chain.CtConsumer; | ||
import spoon.reflect.visitor.chain.CtQueryImpl; | ||
import spoon.reflect.visitor.filter.DirectReferenceFilter; | ||
import spoon.reflect.visitor.filter.SubtypeFilter; | ||
|
||
/** | ||
* The mapping function, accepting {@link CtVariable} | ||
* and returning all the references {@link CtVariableReference} to that variable. | ||
* Supports all variable types: | ||
* <ul> | ||
* <li>CtLocalVariable - local variable declared in body | ||
* <li>CtField - member field of an type | ||
* <li>CtParameter - method parameter | ||
* <li>CtCatchVariable - try - catch variable | ||
* </ul> | ||
*/ | ||
public class VariableReferenceQuery implements CtConsumableFunction<CtVariable<?>> { | ||
|
||
Visitor visitor = new Visitor(); | ||
CtConsumer<Object> outputConsumer; | ||
|
||
@Override | ||
public void apply(CtVariable<?> variable, CtConsumer<Object> outputConsumer) { | ||
this.outputConsumer = outputConsumer; | ||
variable.accept(visitor); | ||
} | ||
|
||
protected class Visitor extends CtScanner { | ||
@Override | ||
protected void enter(CtElement e) { | ||
throw new SpoonException("Unsupported variable of type " + e.getClass().getName()); | ||
} | ||
/** | ||
* calls outputConsumer for each reference of the field | ||
*/ | ||
@Override | ||
public <T> void visitCtField(CtField<T> field) { | ||
if (field.hasModifier(ModifierKind.PRIVATE)) { | ||
searchForPrivateField(field, outputConsumer); | ||
} else if (field.hasModifier(ModifierKind.PUBLIC)) { | ||
searchForPublicField(field, outputConsumer); | ||
} else if (field.hasModifier(ModifierKind.PROTECTED)) { | ||
searchForProtectedField(field, outputConsumer); | ||
} else { | ||
searchForPackageProtectedField(field, outputConsumer); | ||
} | ||
} | ||
|
||
/** | ||
* calls outputConsumer for each reference of the localVariable | ||
*/ | ||
@Override | ||
public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) { | ||
CtStatementList statements = localVariable.getParent(CtStatementList.class); | ||
if (statements == null) { | ||
//cannot search for parameter references of parameter which has no executable | ||
return; | ||
} | ||
//create query which will be evaluated on each statement after local variable declaration | ||
CtQueryImpl l_query = (CtQueryImpl) localVariable.getFactory().Query().createQuery() | ||
.filterChildren(new DirectReferenceFilter<CtLocalVariableReference<T>>(localVariable.getReference())); | ||
List<CtStatement> stats = statements.getStatements(); | ||
int idxOfVar = stats.indexOf(localVariable); | ||
if (idxOfVar < 0) { | ||
throw new SpoonException("Cannot found index of local variable declaration " + localVariable + " in statement list " + statements); | ||
} | ||
for (int i = idxOfVar + 1; i < stats.size(); i++) { | ||
l_query.evaluate(stats.get(i), outputConsumer); | ||
} | ||
} | ||
|
||
/** | ||
* calls outputConsumer for each reference of the parameter | ||
*/ | ||
@Override | ||
public <T> void visitCtParameter(CtParameter<T> parameter) { | ||
CtExecutable<?> exec = parameter.getParent(CtExecutable.class); | ||
if (exec == null) { | ||
//cannot search for parameter references of parameter which has no executable | ||
return; | ||
} | ||
exec.filterChildren(new DirectReferenceFilter<CtParameterReference<?>>(parameter.getReference())) | ||
.forEach(outputConsumer); | ||
} | ||
|
||
/** | ||
* calls outputConsumer for each reference of the catch parameter | ||
*/ | ||
@Override | ||
public <T> void visitCtCatchVariable(CtCatchVariable<T> catchVariable) { | ||
catchVariable.getParent(CtCatch.class).getBody().filterChildren(new DirectReferenceFilter<CtCatchVariableReference<?>>(catchVariable.getReference())) | ||
.forEach(outputConsumer); | ||
} | ||
} | ||
|
||
private void searchForPrivateField(CtField<?> field, CtConsumer<Object> outputConsumer) { | ||
//private field can be referred from the scope of current top level type only | ||
field.getTopLevelType() | ||
.filterChildren(new DirectReferenceFilter<CtFieldReference<?>>(field.getReference())) | ||
.forEach(outputConsumer); | ||
} | ||
private void searchForProtectedField(CtField<?> field, CtConsumer<Object> outputConsumer) { | ||
//protected field can be referred from the scope of current top level type only | ||
field.getFactory().getModel().getRootPackage() | ||
.filterChildren(new SubtypeFilter(field.getDeclaringType().getReference())) | ||
.filterChildren(new DirectReferenceFilter<CtFieldReference<?>>(field.getReference())) | ||
.forEach(outputConsumer); | ||
} | ||
private void searchForPublicField(CtField<?> field, CtConsumer<Object> outputConsumer) { | ||
field.getFactory().getModel().getRootPackage() | ||
.filterChildren(new DirectReferenceFilter<CtFieldReference<?>>(field.getReference())) | ||
.forEach(outputConsumer); | ||
} | ||
private void searchForPackageProtectedField(CtField<?> field, CtConsumer<Object> outputConsumer) { | ||
field.getTopLevelType().getPackage() | ||
.filterChildren(new DirectReferenceFilter<CtFieldReference<?>>(field.getReference())) | ||
.forEach(outputConsumer); | ||
} | ||
} |
Oops, something went wrong.