use of com.github.javaparser.ast.expr.SimpleName in project meghanada-server by mopemope.
the class LocationSearcher method getFieldLocation.
private static Location getFieldLocation(SearchContext context, File targetFile, FieldDeclaration declaration) throws IOException {
final List<VariableDeclarator> variables = declaration.getVariables();
for (final VariableDeclarator variable : variables) {
final SimpleName simpleName = variable.getName();
final String name = simpleName.getIdentifier();
final Optional<Position> begin = simpleName.getBegin();
if (name.equals(context.name) && begin.isPresent()) {
final Position position = begin.get();
return new Location(targetFile.getCanonicalPath(), position.line, position.column);
}
}
return null;
}
use of com.github.javaparser.ast.expr.SimpleName in project meghanada-server by mopemope.
the class LocationSearcher method searchLocationFromFile.
private static Location searchLocationFromFile(final SearchContext ctx, final String fqcn, final File targetFile) throws IOException {
final CompilationUnit compilationUnit = JavaParser.parse(targetFile, StandardCharsets.UTF_8);
final List<TypeDeclaration<?>> types = compilationUnit.getTypes();
for (final TypeDeclaration<?> type : types) {
if (ctx.kind.equals(SearchKind.CLASS)) {
final SimpleName simpleName = type.getName();
final String typeName = simpleName.getIdentifier();
final String name = ClassNameUtils.getSimpleName(fqcn);
final Optional<Position> begin = simpleName.getBegin();
if (typeName.equals(name) && begin.isPresent()) {
final Position position = begin.get();
return new Location(targetFile.getCanonicalPath(), position.line, position.column);
}
}
final List<BodyDeclaration<?>> members = type.getMembers();
ConstructorDeclaration constructor = null;
MethodDeclaration method = null;
for (final BodyDeclaration<?> member : members) {
if (member instanceof FieldDeclaration && ctx.name != null && ctx.kind.equals(SearchKind.FIELD)) {
final Location variable = getFieldLocation(ctx, targetFile, (FieldDeclaration) member);
if (variable != null) {
return variable;
}
} else if (member instanceof ConstructorDeclaration && ctx.name != null && ctx.kind.equals(SearchKind.METHOD)) {
final ConstructorDeclaration declaration = (ConstructorDeclaration) member;
final SimpleName simpleName = declaration.getName();
final String name = simpleName.getIdentifier();
final Optional<Position> begin = simpleName.getBegin();
if (name.equals(ctx.name) && begin.isPresent()) {
final Position position = begin.get();
final List<Parameter> parameters = declaration.getParameters();
// TODO check FQCN types
if (ctx.arguments.size() == parameters.size()) {
return new Location(targetFile.getCanonicalPath(), position.line, position.column);
} else {
if (constructor == null) {
constructor = declaration;
}
}
}
} else if (member instanceof MethodDeclaration && ctx.name != null && ctx.kind.equals(SearchKind.METHOD)) {
final MethodDeclaration declaration = (MethodDeclaration) member;
final SimpleName simpleName = declaration.getName();
final String name = simpleName.getIdentifier();
final Optional<Position> begin = simpleName.getBegin();
if (name.equals(ctx.name) && begin.isPresent()) {
final Position position = begin.get();
final List<Parameter> parameters = declaration.getParameters();
if (ctx.arguments.size() == parameters.size()) {
return new Location(targetFile.getCanonicalPath(), position.line, position.column);
} else {
if (method == null) {
method = declaration;
}
}
}
}
}
if (constructor != null) {
final Position pos = constructor.getName().getBegin().get();
return new Location(targetFile.getCanonicalPath(), pos.line, pos.column);
}
if (method != null) {
final Position pos = method.getName().getBegin().get();
return new Location(targetFile.getCanonicalPath(), pos.line, pos.column);
}
}
return null;
}
use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class CommonCodegenUtilsTest method getParamMethodDeclaration.
@Test
public void getParamMethodDeclaration() {
String methodName = "METHOD_NAME";
final Map<String, ClassOrInterfaceType> parameterNameTypeMap = new HashMap<>();
parameterNameTypeMap.put("stringParam", parseClassOrInterfaceType(String.class.getName()));
parameterNameTypeMap.put("kiePMMLNameValueParam", parseClassOrInterfaceType(KiePMMLNameValue.class.getName()));
parameterNameTypeMap.put("listParam", new ClassOrInterfaceType(null, new SimpleName(List.class.getName()), NodeList.nodeList(parseClassOrInterfaceType(KiePMMLNameValue.class.getName()))));
MethodDeclaration retrieved = CommonCodegenUtils.getMethodDeclaration(methodName, parameterNameTypeMap);
commonValidateMethodDeclaration(retrieved, methodName);
commonValidateMethodDeclarationParams(retrieved, parameterNameTypeMap);
}
use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class ConstraintParser method compileToJavaRecursive.
private DrlxParseResult compileToJavaRecursive(Class<?> patternType, String bindingId, ConstraintExpression constraint, Expression drlxExpr, boolean hasBind, boolean isPositional) {
boolean isEnclosed = false;
SimpleName bind = null;
if (drlxExpr instanceof FullyQualifiedInlineCastExpr) {
drlxExpr = transformFullyQualifiedInlineCastExpr(context.getTypeResolver(), (FullyQualifiedInlineCastExpr) drlxExpr);
}
while (drlxExpr instanceof EnclosedExpr) {
drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
isEnclosed = true;
}
if (drlxExpr instanceof DrlxExpression) {
bind = ((DrlxExpression) drlxExpr).getBind();
drlxExpr = ((DrlxExpression) drlxExpr).getExpr();
}
if (drlxExpr instanceof MethodCallExpr && !((MethodCallExpr) drlxExpr).getScope().isPresent() && ((MethodCallExpr) drlxExpr).getNameAsString().equals("eval")) {
drlxExpr = ((MethodCallExpr) drlxExpr).getArgument(0);
}
if (drlxExpr instanceof BinaryExpr) {
DrlxParseResult result = parseBinaryExpr((BinaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional, isEnclosed);
if (result instanceof SingleDrlxParseSuccess && bind != null) {
((SingleDrlxParseSuccess) result).setExprBinding(bind.asString());
}
return result;
}
if (drlxExpr instanceof UnaryExpr) {
return parseUnaryExpr((UnaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional);
}
if (drlxExpr instanceof PointFreeExpr) {
return parsePointFreeExpr((PointFreeExpr) drlxExpr, patternType, bindingId, isPositional);
}
if (patternType == null && drlxExpr instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) drlxExpr;
Optional<MethodDeclaration> functionCall = packageModel.getFunctions().stream().filter(m -> m.getName().equals(methodCallExpr.getName())).findFirst();
if (functionCall.isPresent()) {
return parseFunctionInEval(methodCallExpr, patternType, bindingId, isPositional, functionCall);
}
}
if (drlxExpr instanceof FieldAccessExpr) {
return parseFieldAccessExpr((FieldAccessExpr) drlxExpr, patternType, bindingId);
}
String expression = constraint.getExpression();
if (drlxExpr instanceof DrlNameExpr) {
return parseNameExpr((DrlNameExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
}
if (drlxExpr instanceof OOPathExpr) {
return parseOOPathExpr((OOPathExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
}
if (drlxExpr instanceof LiteralExpr) {
Class<?> literalExpressionType = getLiteralExpressionType(((LiteralExpr) drlxExpr));
return new SingleDrlxParseSuccess(patternType, bindingId, drlxExpr, literalExpressionType).setIsPredicate(isBooleanBoxedUnboxed(literalExpressionType));
}
if (patternType != null) {
ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
if (!optLeft.isPresent()) {
return new DrlxParseFail();
}
TypedExpression left = optLeft.get();
Expression combo = left.getExpression();
Type exprType = left.getType();
boolean isPredicate = isBooleanBoxedUnboxed(exprType);
if (isPredicate) {
combo = combineExpressions(leftTypedExpressionResult, combo);
}
return new SingleDrlxParseSuccess(patternType, bindingId, combo, exprType).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setImplicitCastExpression(expressionTyperContext.getInlineCastExpression()).setNullSafeExpressions(expressionTyperContext.getNullSafeExpressions()).setIsPredicate(isPredicate);
} else {
final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
final ExpressionTyper expressionTyper = new ExpressionTyper(context, null, bindingId, isPositional, expressionTyperContext);
TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
if (!optLeft.isPresent()) {
return new DrlxParseFail();
}
TypedExpression left = optLeft.get();
return new SingleDrlxParseSuccess(null, bindingId, drlxExpr, left.getType()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setIsPredicate(true);
}
}
use of com.github.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class ExpressionTyperTest method pointFreeTest.
@Test
public void pointFreeTest() {
final PointFreeExpr expression = new PointFreeExpr(null, new NameExpr("name"), NodeList.nodeList(new StringLiteralExpr("[A-Z]")), new SimpleName("matches"), false, null, null, null, null);
TypedExpressionResult typedExpressionResult = new ExpressionTyper(ruleContext, Person.class, null, true).toTypedExpression(expression);
final TypedExpression actual = typedExpressionResult.getTypedExpression().get();
final TypedExpression expected = typedResult("D.eval(org.drools.model.operators.MatchesOperator.INSTANCE, _this.getName(), \"[A-Z]\")", String.class);
assertEquals(expected, actual);
}
Aggregations