Giter Site home page Giter Site logo

javasymbolsolver-maven-sample's Introduction

JavaParser

Maven Central Build Status Coverage Status Join the chat at https://gitter.im/javaparser/javaparser License LGPL-3/Apache-2.0 DOI

This project contains a set of libraries implementing a Java 1.0 - Java 18 Parser with advanced analysis functionalities.

Our main site is at JavaParser.org

Sponsors

Support this project by becoming a sponsor! Become a sponsor. Your donation will help the project live and grow successfully.

Javaparser uses OpenCollective to gather money.

Thank you to our sponsors!

Setup

The project binaries are available in Maven Central.

We strongly advise users to adopt Maven, Gradle or another build system for their projects. If you are not familiar with them we suggest taking a look at the maven quickstart projects (javaparser-maven-sample, javasymbolsolver-maven-sample).

Just add the following to your maven configuration or tailor to your own dependency management system.

Please refer to the Migration Guide when upgrading from 2.5.1 to 3.0.0+

Maven:

<dependency>
    <groupId>com.github.javaparser</groupId>
    <artifactId>javaparser-symbol-solver-core</artifactId>
    <version>3.25.10</version>
</dependency>

Gradle:

implementation 'com.github.javaparser:javaparser-symbol-solver-core:3.25.10'

Since Version 3.5.10, the JavaParser project includes the JavaSymbolSolver. While JavaParser generates an Abstract Syntax Tree, JavaSymbolSolver analyzes that AST and is able to find the relation between an element and its declaration (e.g. for a variable name it could be a parameter of a method, providing information about its type, position in the AST, ect).

Using the dependency above will add both JavaParser and JavaSymbolSolver to your project. If you only need the core functionality of parsing Java source code in order to traverse and manipulate the generated AST, you can reduce your projects boilerplate by only including JavaParser to your project:

Maven:

<dependency>
    <groupId>com.github.javaparser</groupId>
    <artifactId>javaparser-core</artifactId>
    <version>3.25.10</version>
</dependency>

Gradle:

implementation 'com.github.javaparser:javaparser-core:3.25.10'

Since version 3.6.17 the AST can be serialized to JSON. There is a separate module for this:

Maven:

<dependency>
    <groupId>com.github.javaparser</groupId>
    <artifactId>javaparser-core-serialization</artifactId>
    <version>3.25.10</version>
</dependency>

Gradle:

implementation 'com.github.javaparser:javaparser-core-serialization:3.25.10'

How To Compile Sources

If you checked out the project's source code from GitHub, you can build the project with maven using:

./mvnw clean install

If you want to generate the packaged jar files from the source files, you run the following maven command:

./mvnw package

NOTE the jar files for the two modules can be found in:

  • javaparser/javaparser-core/target/javaparser-core-\<version\>.jar
  • javaparser-symbol-solver-core/target/javaparser-symbol-solver-core-\<version\>.jar

If you checkout the sources and want to view the project in an IDE, it is best to first generate some of the source files; otherwise you will get many compilation complaints in the IDE. (./mvnw clean install already does this for you.)

./mvnw javacc:javacc

If you modify the code of the AST nodes, specifically if you add or remove fields or node classes, the code generators will update a lot of code for you. The run_metamodel_generator.sh script will rebuild the metamodel, which is used by the code generators which are run by run_core_generators.sh Make sure that javaparser-core at least compiles before you run these.

Note: for Eclipse IDE follow the steps described in the wiki: https://github.com/javaparser/javaparser/wiki/Eclipse-Project-Setup-Guide

More information

JavaParser.org is the main information site or see the wiki page https://github.com/javaparser/javaparser/wiki.

License

JavaParser is available either under the terms of the LGPL License or the Apache License. You as the user are entitled to choose the terms under which adopt JavaParser.

For details about the LGPL License please refer to LICENSE.LGPL.

For details about the Apache License please refer to LICENSE.APACHE.

javasymbolsolver-maven-sample's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar ftomassetti avatar jlerbsc avatar matozoid avatar mysteraitch avatar tarilabs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

javasymbolsolver-maven-sample's Issues

getImports performance is worse than eclipse-astparser

javaparser is very comfortable to use.
I want to find all the imports of the class,
I tried it and the performance is worse than Eclipse,
I don’t know if it is the wrong way.

package com.yourorganization.maven_sample;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.expr.Name;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

public class ImportUse {

    private static final String CLASS_PATH = ClassLoader.getSystemClassLoader().getResource("").getPath();
    private static final File PROJECT_FILE = new File(CLASS_PATH).getParentFile().getParentFile();
    private static final File SRC_MAIN_JAVA_FILE = new File(PROJECT_FILE, "/src/main/java");
    private static final String FILE_NAME = ImportUse.class.getName().replace('.', '/') + ".java";
    private static final File FILE = new File(SRC_MAIN_JAVA_FILE, FILE_NAME);

    public static void main(String[] args) throws Exception {
        byte[] bytes = Files.readAllBytes(FILE.getAbsoluteFile().toPath());
        String s = new String(bytes, StandardCharsets.UTF_8);

        int COUNT = 1_000;

        long time1 = System.currentTimeMillis();

        // eclipse-astparser
        for (int i = 0; i < COUNT; i++) {
            ASTParser parser = ASTParser.newParser(AST.JLS8);
            parser.setSource(s.toCharArray());
            parser.setKind(ASTParser.K_COMPILATION_UNIT);
            org.eclipse.jdt.core.dom.CompilationUnit unit =
                    (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null);
            List<org.eclipse.jdt.core.dom.ImportDeclaration> imports = unit.imports();
            imports.forEach(a -> {
                org.eclipse.jdt.core.dom.Name name = a.getName();
            });
        }

        long time2 = System.currentTimeMillis();

        // javaparser
        for (int i = 0; i < COUNT; i++) {
            CompilationUnit cu = StaticJavaParser.parse(s);
            cu.getImports().forEach(a -> {
                Name name = a.getName();
            });
        }

        long time3 = System.currentTimeMillis();

        System.out.println(time2 - time1); // 2581
        System.out.println(time3 - time2); // 4613
    }
}

UnsolvedSymbolException

code like this...

@ApiOperation(value = "注册账户信息", tags = "账户")
public JsonResponseEntity<String> register(@RequestBody @Valid BankAccountDTO dto) {
    UserBasic user = UserUtil.getUserInfo();
    MobileInfo mobileInfo = new MobileInfo(dto.getContactPhone(), dto.getVerificationCode());
    String status = accountServiceApplication.register(null, user.getUid(), user.getUsername());
    return JsonResponseEntity.ok(status);
}

`

    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    TypeSolver reflectionTypeSolver = new ReflectionTypeSolver(false);
    JavaParserTypeSolver javaParserTypeSolver = new JavaParserTypeSolver(new File("D:\\ideawork\\src\\main\\java"));
    combinedTypeSolver.add(reflectionTypeSolver);
    combinedTypeSolver.add(javaParserTypeSolver);

    JavaSymbolSolver symbolSolver = new JavaSymbolSolver(combinedTypeSolver);
    StaticJavaParser.getConfiguration().setSymbolResolver(symbolSolver);

`

Exception:UnsolvedSymbolException{context='null', name='We are unable to find the method declaration corresponding to dto.getContactPhone()', cause='null'}

How to deal with it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.