-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pointcut for filtering by constructors
- Loading branch information
1 parent
38e8ae1
commit 11a6815
Showing
8 changed files
with
196 additions
and
161 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
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
68 changes: 68 additions & 0 deletions
68
...roovy.eclipse.dsl/src/org/codehaus/groovy/eclipse/dsl/pointcuts/impl/FindASTPointcut.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,68 @@ | ||
/* | ||
* Copyright 2009-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.codehaus.groovy.eclipse.dsl.pointcuts.impl; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
import org.codehaus.groovy.ast.ASTNode; | ||
import org.codehaus.groovy.ast.ClassNode; | ||
import org.codehaus.groovy.eclipse.dsl.pointcuts.GroovyDSLDContext; | ||
import org.eclipse.core.resources.IStorage; | ||
import org.eclipse.jdt.groovy.core.util.GroovyUtils; | ||
|
||
public abstract class FindASTPointcut<T extends ASTNode> extends FilteringPointcut<T> { | ||
|
||
private final Function<ClassNode, ? extends Collection<T>> exploder; | ||
private final Function<T, String> stringify; | ||
|
||
public FindASTPointcut(IStorage containerIdentifier, String pointcutName, Class<T> filterBy, | ||
Function<ClassNode, ? extends Collection<T>> exploder, Function<T, String> stringify) { | ||
super(containerIdentifier, pointcutName, filterBy); | ||
this.exploder = Objects.requireNonNull(exploder); | ||
this.stringify = Objects.requireNonNull(stringify); | ||
} | ||
|
||
@Override | ||
protected final Collection<T> explodeObject(Object object) { | ||
Collection<T> result = new ArrayList<>(); | ||
explodeObject(object, result); | ||
return result; | ||
} | ||
|
||
protected void explodeObject(Object object, Collection<T> result) { | ||
if (filterBy.isInstance(object)) { | ||
@SuppressWarnings("unchecked") | ||
T node = (T) object; | ||
result.add(node); | ||
} else if (object instanceof ClassNode) { | ||
result.addAll(exploder.apply( | ||
GroovyUtils.getWrapperTypeIfPrimitive((ClassNode) object))); | ||
} else if (object instanceof Collection) { | ||
((Collection<?>) object).forEach(item -> explodeObject(item, result)); | ||
} | ||
} | ||
|
||
@Override | ||
protected T filterObject(T result, GroovyDSLDContext context, String firstArgAsString) { | ||
if (firstArgAsString == null || firstArgAsString.equals(stringify.apply(result))) { | ||
return result; | ||
} | ||
return null; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...oovy.eclipse.dsl/src/org/codehaus/groovy/eclipse/dsl/pointcuts/impl/FindCtorPointcut.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 2009-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.codehaus.groovy.eclipse.dsl.pointcuts.impl; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.codehaus.groovy.ast.ClassNode; | ||
import org.codehaus.groovy.ast.ConstructorNode; | ||
import org.eclipse.core.resources.IStorage; | ||
import org.eclipse.jdt.groovy.core.util.GroovyUtils; | ||
|
||
/** | ||
* Matches if the input has a constructor with the supplied characteristics -- | ||
* either a signature (comma-separated simple type names) or another pointcut | ||
* like {@code hasAnnotation}. | ||
*/ | ||
public class FindCtorPointcut extends FindASTPointcut<ConstructorNode> { | ||
|
||
public FindCtorPointcut(IStorage containerIdentifier, String pointcutName) { | ||
super(containerIdentifier, pointcutName, ConstructorNode.class, ClassNode::getDeclaredConstructors, ctor -> { | ||
List<ClassNode> paramTypes = GroovyUtils.getParameterTypes(ctor.getParameters()); | ||
return paramTypes.stream().map(FindCtorPointcut::getSimpleTypeName) | ||
.map(name -> name.substring(name.lastIndexOf('$') + 1)) | ||
.collect(Collectors.joining(",")); | ||
}); | ||
} | ||
|
||
private static String getSimpleTypeName(ClassNode type) { | ||
int dims = 0; | ||
while (type.isArray()) { | ||
dims += 1; | ||
type = type.getComponentType(); | ||
} | ||
|
||
String name = type.getNameWithoutPackage(); | ||
while (dims > 0) { | ||
name += "[]"; | ||
dims -= 1; | ||
} | ||
return name; | ||
} | ||
} |
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
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
Oops, something went wrong.