Search in sources :

Example 1 with Position

use of com.github.javaparser.Position 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;
}
Also used : Position(com.github.javaparser.Position) SimpleName(com.github.javaparser.ast.expr.SimpleName) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 2 with Position

use of com.github.javaparser.Position 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;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Optional(java.util.Optional) Position(com.github.javaparser.Position) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Parameter(com.github.javaparser.ast.body.Parameter) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) List(java.util.List) ArrayList(java.util.ArrayList) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 3 with Position

use of com.github.javaparser.Position in project javaparser by javaparser.

the class PositionUtils method compare.

private static int compare(Node a, Node b, boolean ignoringAnnotations) {
    if (a.getRange().isPresent() && !b.getRange().isPresent()) {
        return -1;
    }
    if (!a.getRange().isPresent() && b.getRange().isPresent()) {
        return 1;
    }
    if (!a.getRange().isPresent() && !b.getRange().isPresent()) {
        return 0;
    }
    if (ignoringAnnotations) {
        int signLine = signum(beginLineWithoutConsideringAnnotation(a) - beginLineWithoutConsideringAnnotation(b));
        if (signLine == 0) {
            return signum(beginColumnWithoutConsideringAnnotation(a) - beginColumnWithoutConsideringAnnotation(b));
        } else {
            return signLine;
        }
    }
    Position aBegin = a.getBegin().get();
    Position bBegin = b.getBegin().get();
    int signLine = signum(aBegin.line - bBegin.line);
    if (signLine == 0) {
        return signum(aBegin.column - bBegin.column);
    } else {
        return signLine;
    }
}
Also used : Position(com.github.javaparser.Position)

Example 4 with Position

use of com.github.javaparser.Position in project javaparser by javaparser.

the class ASTParser method ArrayInitializer.

public final ArrayInitializerExpr ArrayInitializer() {
    List<Expression> values = null;
    Expression val;
    Position begin;
    jj_consume_token(LBRACE);
    begin = tokenBegin();
    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
        case BOOLEAN:
        case BYTE:
        case CHAR:
        case DOUBLE:
        case FALSE:
        case FLOAT:
        case INT:
        case LONG:
        case NEW:
        case NULL:
        case SHORT:
        case SUPER:
        case THIS:
        case TRUE:
        case VOID:
        case LONG_LITERAL:
        case INTEGER_LITERAL:
        case FLOATING_POINT_LITERAL:
        case CHARACTER_LITERAL:
        case STRING_LITERAL:
        case IDENTIFIER:
        case LPAREN:
        case LBRACE:
        case BANG:
        case TILDE:
        case INCR:
        case DECR:
        case PLUS:
        case MINUS:
            {
                val = VariableInitializer();
                values = add(values, val);
                label_14: while (true) {
                    if (jj_2_7(2)) {
                        ;
                    } else {
                        break label_14;
                    }
                    jj_consume_token(COMMA);
                    val = VariableInitializer();
                    values = add(values, val);
                }
                break;
            }
        default:
            jj_la1[35] = jj_gen;
            ;
    }
    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
        case COMMA:
            {
                jj_consume_token(COMMA);
                break;
            }
        default:
            jj_la1[36] = jj_gen;
            ;
    }
    jj_consume_token(RBRACE);
    return new ArrayInitializerExpr(range(begin, tokenEnd()), values);
}
Also used : Position(com.github.javaparser.Position)

Example 5 with Position

use of com.github.javaparser.Position in project javaparser by javaparser.

the class ASTParser method FormalParameter.

public final Parameter FormalParameter() {
    ModifierHolder modifier;
    Type type;
    boolean isVarArg = false;
    VariableDeclaratorId id;
    modifier = Modifiers();
    type = Type();
    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
        case ELLIPSIS:
            {
                jj_consume_token(ELLIPSIS);
                isVarArg = true;
                break;
            }
        default:
            jj_la1[46] = jj_gen;
            ;
    }
    id = VariableDeclaratorId();
    Position begin = modifier.begin.orIfInvalid(type.getBegin());
    Pair<Type, List<ArrayBracketPair>> typeListPair = unwrapArrayTypes(type);
    return new Parameter(range(begin, tokenEnd()), modifier.modifiers, modifier.annotations, typeListPair.a, typeListPair.b, isVarArg, id);
}
Also used : ArrayType(com.github.javaparser.ast.type.ArrayType) Position(com.github.javaparser.Position)

Aggregations

Position (com.github.javaparser.Position)56 ArrayType (com.github.javaparser.ast.type.ArrayType)10 SimpleName (com.github.javaparser.ast.expr.SimpleName)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 PositionUtils.sortByBeginPosition (com.github.javaparser.utils.PositionUtils.sortByBeginPosition)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1