-
-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add helper for query variables (VariableReferenceFunction, V…
…ariableScopeFunction) (#1114) * feature: add VariableReferenceFunction * test VariableReferenceFunction * fix comments of VariableReferenceFunction * feature: VariableScopeFunction * test VariableScopeFunction * add missing license header
- Loading branch information
1 parent
e514e17
commit 8c35c98
Showing
3 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/main/java/spoon/reflect/visitor/filter/VariableReferenceFunction.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,95 @@ | ||
/** | ||
* Copyright (C) 2006-2017 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.SpoonException; | ||
import spoon.reflect.code.CtCatchVariable; | ||
import spoon.reflect.code.CtLocalVariable; | ||
import spoon.reflect.declaration.CtElement; | ||
import spoon.reflect.declaration.CtField; | ||
import spoon.reflect.declaration.CtParameter; | ||
import spoon.reflect.declaration.CtVariable; | ||
import spoon.reflect.reference.CtVariableReference; | ||
import spoon.reflect.visitor.CtScanner; | ||
import spoon.reflect.visitor.chain.CtConsumableFunction; | ||
import spoon.reflect.visitor.chain.CtConsumer; | ||
|
||
/** | ||
* The mapping function, accepting {@link CtVariable} | ||
* <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> | ||
* and returning all the {@link CtVariableReference}, which refers this variable | ||
*/ | ||
public class VariableReferenceFunction implements CtConsumableFunction<CtVariable<?>> { | ||
|
||
protected final Visitor visitor = new Visitor(); | ||
protected CtConsumer<Object> outputConsumer; | ||
|
||
@Override | ||
public void apply(CtVariable<?> variable, CtConsumer<Object> outputConsumer) { | ||
this.outputConsumer = outputConsumer; | ||
variable.accept(visitor); | ||
} | ||
|
||
private static final FieldReferenceFunction fieldReferenceFunction = new FieldReferenceFunction(); | ||
private static final LocalVariableReferenceFunction localVariableReferenceFunction = new LocalVariableReferenceFunction(); | ||
private static final ParameterReferenceFunction parameterReferenceFunction = new ParameterReferenceFunction(); | ||
private static final CatchVariableReferenceFunction catchVariableReferenceFunction = new CatchVariableReferenceFunction(); | ||
|
||
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) { | ||
fieldReferenceFunction.apply(field, outputConsumer); | ||
} | ||
|
||
/** | ||
* calls outputConsumer for each reference of the local variable | ||
*/ | ||
@Override | ||
public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) { | ||
localVariableReferenceFunction.apply(localVariable, outputConsumer); | ||
} | ||
|
||
/** | ||
* calls outputConsumer for each reference of the parameter | ||
*/ | ||
@Override | ||
public <T> void visitCtParameter(CtParameter<T> parameter) { | ||
parameterReferenceFunction.apply(parameter, outputConsumer); | ||
} | ||
|
||
/** | ||
* calls outputConsumer for each reference of the catch variable | ||
*/ | ||
@Override | ||
public <T> void visitCtCatchVariable(CtCatchVariable<T> catchVariable) { | ||
catchVariableReferenceFunction.apply(catchVariable, outputConsumer); | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/spoon/reflect/visitor/filter/VariableScopeFunction.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,94 @@ | ||
/** | ||
* Copyright (C) 2006-2017 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.SpoonException; | ||
import spoon.reflect.code.CtCatchVariable; | ||
import spoon.reflect.code.CtLocalVariable; | ||
import spoon.reflect.declaration.CtElement; | ||
import spoon.reflect.declaration.CtField; | ||
import spoon.reflect.declaration.CtParameter; | ||
import spoon.reflect.declaration.CtVariable; | ||
import spoon.reflect.visitor.CtScanner; | ||
import spoon.reflect.visitor.chain.CtConsumableFunction; | ||
import spoon.reflect.visitor.chain.CtConsumer; | ||
|
||
/** | ||
* The mapping function, accepting {@link CtVariable} | ||
* <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> | ||
* and returning all the CtElements, which exists in visibility scope of this variable. | ||
*/ | ||
public class VariableScopeFunction implements CtConsumableFunction<CtVariable<?>> { | ||
|
||
protected final Visitor visitor = new Visitor(); | ||
protected CtConsumer<Object> outputConsumer; | ||
|
||
@Override | ||
public void apply(CtVariable<?> variable, CtConsumer<Object> outputConsumer) { | ||
this.outputConsumer = outputConsumer; | ||
variable.accept(visitor); | ||
} | ||
|
||
private static final FieldScopeFunction fieldScopeFunction = new FieldScopeFunction(); | ||
private static final LocalVariableScopeFunction localVariableScopeFunction = new LocalVariableScopeFunction(); | ||
private static final ParameterScopeFunction parameterScopeFunction = new ParameterScopeFunction(); | ||
private static final CatchVariableScopeFunction catchVariableScopeFunction = new CatchVariableScopeFunction(); | ||
|
||
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) { | ||
fieldScopeFunction.apply(field, outputConsumer); | ||
} | ||
|
||
/** | ||
* calls outputConsumer for each reference of the local variable | ||
*/ | ||
@Override | ||
public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) { | ||
localVariableScopeFunction.apply(localVariable, outputConsumer); | ||
} | ||
|
||
/** | ||
* calls outputConsumer for each reference of the parameter | ||
*/ | ||
@Override | ||
public <T> void visitCtParameter(CtParameter<T> parameter) { | ||
parameterScopeFunction.apply(parameter, outputConsumer); | ||
} | ||
|
||
/** | ||
* calls outputConsumer for each reference of the catch variable | ||
*/ | ||
@Override | ||
public <T> void visitCtCatchVariable(CtCatchVariable<T> catchVariable) { | ||
catchVariableScopeFunction.apply(catchVariable, outputConsumer); | ||
} | ||
} | ||
} |
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