Giter VIP home page Giter VIP logo

javaparser-visited'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.

javaparser-visited's People

Contributors

dependabot[bot] avatar matozoid avatar shootercheng avatar smiddypence avatar symbolk 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  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  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  avatar

javaparser-visited's Issues

[EDITORIAL] Example uses File; text describes FileInputStream

  1. p 11 has a
CompilationUnit cu = StaticJavaParser.parse(new File(FILE_PATH));

... p 12 describes that invokcation as the StaticJavaParser taking a FileInputStream (which you don't have to close).
... I think the example changed from a FileInputStream to a File without the description changing.

Error in code example (page 18)

On page 18 you have the code line
ModifierVisitorAdapter<?> numericLiteralVisitor = new IntegerLiteralModifier();
it should be
ModifierVisitor<?> numericLiteralVisitor = new IntegerLiteralModifier();
as the ModifierVisitorAdapter class does not exist.

Example text in book is out-dated, e.g. for `ResolveTypeInContext`

I just bought the book and I am trying out the examples.

However I could not compile ResolveTypeInContext because StaticJavaParser.getStaticConfiguration() is not implemented. Using StaticJavaParser.getConfiguration() instead solved the issue.

I just checked with the example code in this repo (https://github.com/javaparser/javaparser-visited/blob/master/src/main/java/org/javaparser/examples/chapter5/ResolveTypeInContext.java) and noticed the bug is already fixed in that source code. However as I copied the example code from the book's PDF I ran into the problem.

According to the book it was last updated 2021-02-05, but the source code was 2019-04-15, i.e. nearly two years before the book was last published.

Please keep the Leanpub books you sell up-to-date with the source code in the repo.

[EDITORIAL] propose clarification of overloading visitor

p11 has:

this means that you can define only the method to handle a certain type of nodes (e.g. field declarations), while your visitor will not do anything with the dozens of other node types you are not interested in.

propose:

this means you only need to defined the methods for nodes you are interested in (e.g. field declarations); the functions inherited from VoidVistorAdaptor will handle the recursion through the rest of the compliation unit's abstract syntax tree.

Non-static usage of the JP API

The coding style within the book seems to be exclusively in favour of using the static parser.

If an edit is considered, perhaps consider adding some examples which show the different API involved in non-static usage.

"com.github.javaparser.symbolsolver.javaparser.Navigator.findNodeOfGivenClass"

I built the project using maven and ran this example. I got the following error.

I only modified FILE_PATH and SRC_PATH.

https://github.com/javaparser/javaparser-visited/blob/master/src/main/java/org/javaparser/examples/chapter5/ResolveTypeInContext.java

Exception in thread "main" java.lang.IllegalArgumentException
	at java.util.Optional.orElseThrow(Optional.java:290)
	at com.github.javaparser.symbolsolver.javaparser.Navigator.findNodeOfGivenClass(Navigator.java:205)
	at cn.ac.iscas.pa.ProjectName.App.main(App.java:43)

This is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.ac.iscas.pa</groupId>
	<artifactId>ProjectName</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>ProjectName</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

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

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

		<dependency>
			<groupId>com.github.javaparser</groupId>
			<artifactId>javaparser-core-serialization</artifactId>
			<version>3.14.3</version>
		</dependency>
	</dependencies>
</project>

Thanks.

Maven warning

[WARNING] Some problems were encountered while building the effective model for com.github.javaparser:javaparser-book:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 12, column 21

(Taken from #1048)

Add manual for setting up target Java edition

Actually I'm looking at the documentation and cannot find any method of setting up the Java version; e.g., I want to parse (maybe) Java 7, then... how to?. I'm beginner, so of course I'm sorry for this annoying issue.

Could you guys put something about that inside the manual?

Some typos in the "JavaParser: Visited" book

JavaParser: Visited
published on 2018-04-07

pg 11, childredNodes -> childrenNodes
pg 21, FielidDeclariton’s -> FieldDeclaration’s
pg 27 it’s -> its
pg 30 each threads personal -> each thread’s personal

Probably an error:
pg 51 reflectionTypeSolver.setParent(reflectionTypeSolver); -> reflectionTypeSolver.setParent(javaParserTypeSolver);

Typo Page 16

There is a little typo in the "JavaParser: Visited" book: on page 16 (pdf version) in the bottom half it says "FielidDeclartions" instead of "FieldDeclarations"

JavaSymbolSolver

I'm having another read through the book and have spotted that the symbol solver is referred to as "JavaSymbolSolver".

My understanding is that it is now the "JavaParser Symbol Solver".

If this understanding is correct, then please consider this minor edit :)

ResolveTypeInContext sample throws an exception

The example for ResolveTypeInContext in the book throws an exception

reflectionTypeSolver.setParent(reflectionTypeSolver);

Exception in thread "main" java.lang.IllegalStateException: The parent of this TypeSolver cannot be itself.
	at com.github.javaparser.symbolsolver.resolution.typesolvers.ClassLoaderTypeSolver.setParent(ClassLoaderTypeSolver.java:60)

Presumably the line should be removed (as the parent is overridden anyway from combinedSolver.add(reflectionTypeSolver)

(also in the same example Navigator.findNodeOfGivenClass(cu, FieldDeclaration.class); is deprecated

Deprecate the wiki "manual"

There's a page called "manual" on the wiki that's always outdated. Better see if it contains anything useful for the book, and if not, delete the thing.

Also delete the wiki code samples contained in the javaparser test code, and the links to the wiki (javaparser.org and README.md?)

Don't use LexicalPreservingPrinter.setup

You have to to do LexicalPreservingPrinter.print on the CompilationUnit that you put into LexicalPreservingPrinter.setup and NOT .toString() on this...I did not know this and the web pages were not stating this at all. The PDF/book though shows this but also uses the deprecated Constructor instead of LexicalPreservingPrinter.setup, which should be fixed.

IllegalStateException when running the ResolveTypeInContext example from the book

When trying to run the ResolveTypeInContext example from the book I get an Exception:

java.lang.IllegalStateException: The parent of this TypeSolver cannot be itself.
    at com.github.javaparser.symbolsolver.resolution.typesolvers.ClassLoaderTypeSolver.setParent(ClassLoaderTypeSolver.java:60)

It occurs for this statement:

reflectionTypeSolver.setParent(reflectionTypeSolver);

(line 23 in https://github.com/javaparser/javaparser-visited/blob/master/src/main/java/org/javaparser/examples/chapter5/ResolveTypeInContext.java)

Removing the statement make the exception go away.

bug in ModifyingVisitorStarter

execution of ModifyingVisitorStarter.main() fails:

Exception in thread "main" com.github.javaparser.ParseProblemException: Parse error. Found "src" , expected one of ";" "@" "\u001a" "abstract" "class" "default" "enum" "final" "import" "interface" "module" "native" "open" "private" "protected" "public" "static" "strictfp" "synchronized" "transient" "transitive" "volatile"

that is, call to StaticJavaParser.parse(FILE_PATH) expects program code as argument, but a file path was passed.
Line 13 should be fixed as follows:

    CompilationUnit cu = StaticJavaParser.parse(new FileInputStream(FILE_PATH));

as in ModifyingVisitorComplete.

Error in code (page 43)

You have the code

class MyClass extends MySuperClass { 
    public void returnSomeValue() {
        return aField.size();
    }
}

This cannot be a void method because it returns a value. If you add the return type this will resolve the data type for aField but your example is to show that the solver may have to see other files, in this case the parent class.
I suggest

class MyClass extends MySuperClass { 
    public void printSomeValue() {
        System.out.println(aField.size();
    }
}

Problem in ResolveMethodCalls.class

If i parse below class with ResolveMethodCalls parser im getting this errror

"package org.javaparser.examples.chapter5;
class A {
public void foo(Object param) {
Bar.aMethod();
System.out.println(1);
System.out.println("hi");
System.out.println(param);
}
}

Error :
Exception in thread "main" UnsolvedSymbolException{context='Bar.aMethod()', name='Bar', cause='UnsolvedSymbolException{context='Bar', name='Solving Bar', cause='null'}'}
at com.github.javaparser.symbolsolver.javaparsermodel.contexts.AbstractJavaParserContext.findTypeDeclarations(AbstractJavaParserContext.java:176)
at com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext.solveMethod(MethodCallExprContext.java:152)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve(JavaParserFacade.java:242)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve(JavaParserFacade.java:125)
at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:142)
at com.github.javaparser.ast.expr.MethodCallExpr.resolve(MethodCallExpr.java:313)
at com.github.javaparser.ast.expr.MethodCallExpr.resolveInvokedMethod(MethodCallExpr.java:321)
at org.javaparser.examples.chapter5.ResolveMethodCalls.lambda$main$0(ResolveMethodCalls.java:27)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.javaparser.examples.chapter5.ResolveMethodCalls.main(ResolveMethodCalls.java:26)
Caused by: UnsolvedSymbolException{context='Bar', name='Solving Bar', cause='null'}
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:279)
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:44)
at com.github.javaparser.ast.expr.NameExpr.accept(NameExpr.java:79)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:447)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:310)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:292)
at com.github.javaparser.symbolsolver.javaparsermodel.contexts.AbstractJavaParserContext.findTypeDeclarations(AbstractJavaParserContext.java:167)
... 9 more

Update chapter "Using Lexical-Preserving Printing"

I report a message I received from a user (Udo):

you may want to update chapter "Using Lexical-Preserving Printing" in the book.
The current code leads to "deprecated" warnings and is much more complicated as the correct implementation.

Here a possible example...

File javaFile = ...;
CompilationUnit cu = JavaParser.parse(javaFile);
LexicalPreservingPrinter.setup(cu);
   
// ... add code here to rewrite the CompilationUnit cu
 
// Print the source code of the modified compilation unit
System.out.println(LexicalPreservingPrinter.print(cu));

Feedback from gitter channel

Geert Vancompernolle @GeVanCo Aug 04 18:43

Remark in book: page 11 (real page 16) -> package name incomplete. Mentioned: org.javaparser.examples.VoidV, should be (I guess) org.javaparser.examples.VoidVisitorstarter
Page 12 (real page 17):
...but not operate on... => ...but will not operate on...
Text "Appendix B": suggest to put a link to Appendix B itself. This way, it's much more comfortable to jump between two locations in a PDF document
Page 14 (real page 19):
...create another visitor class... => ...create another inner visitor class...
Page 15 (real page 20)
...we've named ours ModifyingVisitorExample => ...we've named ours ModifyingVisitorStarter (should be same as file name)
Page 16 (real page 21)
...The first two visitors...: Mention that we're creating a new file 'ModifyingVisitorComplete.java' with a 'main' method. It's otherwise a bit confusing since just above you're still talking about 'ModifyingVisitorStarter'
Page 19 (real page 24)
...Within our class create... => Mention that you've created a new class with 'main' in it: 'CommentReporterComplete.java'
Page 23 (real page 28)
...aside from visitors in the getAllContainedComments... => ...aside from visitors is the getAllContainedComments...: change 'in' to 'is'
...comments are themselves are... => ...comments themselves are...
...comment parsing there a couple of... => ...comment parsing there are a couple of...
Page 24 (real page 29)
...with single source files is worth remembering... => ...with single source files but is worth remembering...: 'but' is missing
...remembering if you looking to... => ...remembering if you are looking to... or ...remembering if you look to...
In this instance, providing comment attribution is enabelled the comment // Blah will be attributed to the method f. => In this instance the comment // Blah will be attributed to the method f providing comment attribution is enabled.

Using deprecated method in sample code (JarTypeSolver.getJarTypeSolver)

In the chapter "Using the CombinedTypeSolver" of the book the sample code contains lines like this:

JarTypeSolver.getJarTypeSolver("jars/library1.jar"),

However, JarTypeSolver.getJarTypeSolver is deprecated, so the sample code should probably look like this:

new JarTypeSolver("jars/library1.jar"),

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.