/
JavaClass.java
116 lines (102 loc) · 4.11 KB
/
JavaClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.jboss.windup.rules.apps.java.config;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.jboss.forge.furnace.util.Assert;
import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.condition.GraphCondition;
import org.jboss.windup.config.operation.Iteration;
import org.jboss.windup.config.query.Query;
import org.jboss.windup.config.query.QueryBuilderFind;
import org.jboss.windup.config.query.QueryGremlinCriterion;
import org.jboss.windup.config.query.QueryPropertyComparisonType;
import org.jboss.windup.reporting.model.FileReferenceModel;
import org.jboss.windup.rules.apps.java.model.JavaClassModel;
import org.jboss.windup.rules.apps.java.model.JavaSourceFileModel;
import org.jboss.windup.rules.apps.java.scan.ast.JavaTypeReferenceModel;
import org.jboss.windup.rules.apps.java.scan.ast.TypeInterestFactory;
import org.jboss.windup.rules.apps.java.scan.ast.TypeReferenceLocation;
import org.ocpsoft.rewrite.config.Condition;
import org.ocpsoft.rewrite.config.ConditionBuilder;
import org.ocpsoft.rewrite.context.EvaluationContext;
import com.tinkerpop.blueprints.Predicate;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.gremlin.java.GremlinPipeline;
/**
* {@link GraphCondition} that matches Vertices in the graph based upon the provided parameters.
*/
public class JavaClass extends GraphCondition implements JavaClassBuilder, JavaClassBuilderAt, JavaClassBuilderInFile
{
private final String regex;
private List<TypeReferenceLocation> locations = Collections.emptyList();
private String variable = Iteration.DEFAULT_VARIABLE_LIST_STRING;
private String fileRegex;
private JavaClass(String regex)
{
this.regex = regex;
TypeInterestFactory.registerInterest(regex);
}
/**
* Create a new {@link JavaClass} {@link Condition} based upon the provided Java regular expression.
*/
public static JavaClassBuilder references(String regex)
{
return new JavaClass(regex);
}
public JavaClassBuilderInFile inFile(String regex)
{
this.fileRegex = regex;
return this;
}
/**
* Only match if the TypeReference is at the specified location within the file.
*/
@Override
public JavaClassBuilderAt at(TypeReferenceLocation... locations)
{
if (locations != null)
this.locations = Arrays.asList(locations);
return this;
}
/**
* Optionally specify the variable name to use for the output of this condition
*/
@Override
public ConditionBuilder as(String variable)
{
Assert.notNull(variable, "Variable name must not be null.");
this.variable = variable;
return this;
}
@Override
public boolean evaluate(GraphRewrite event, EvaluationContext context)
{
QueryBuilderFind query = Query.find(JavaTypeReferenceModel.class);
query.withProperty(JavaTypeReferenceModel.SOURCE_SNIPPIT, QueryPropertyComparisonType.REGEX, regex);
if (fileRegex != null)
{
QueryGremlinCriterion inFileWithName = new QueryGremlinCriterion()
{
@Override
public void query(GraphRewrite event, GremlinPipeline<Vertex, Vertex> pipeline)
{
Predicate regexPredicate = new Predicate()
{
@Override
public boolean evaluate(Object first, Object second)
{
return ((String) first).matches((String) second);
}
};
pipeline.as("result").out(FileReferenceModel.FILE_MODEL)
.out(JavaSourceFileModel.JAVA_CLASS_MODEL)
.has(JavaClassModel.PROPERTY_QUALIFIED_NAME, regexPredicate, fileRegex).back("result");
}
};
query.piped(inFileWithName);
}
if (!locations.isEmpty())
query.withProperty(JavaTypeReferenceModel.REFERENCE_TYPE, locations);
return query.as(variable).evaluate(event, context);
}
}