Eclipse JDT tutorial – Java Search Engine – A Simple Working Example

This article belongs to Eclipse JDT Tutorial Series. JDT Java Search Engine provides a function to quick search Java projects in the workspace for Java elements, such as method references, field declarations, implementors of an interface, etc. There are mainly 4 steps involved with a search: Create a search pattern Create a search scope Define … Read more

Eclipse JDT tutorial – Java Model – Concept and Design

This article belongs to Eclipse JDT Tutorial Series. What is Java Model? The Java model is the set of classes that model the objects associated with creating, editing, and building a Java program. It is defined in org.eclipse.jdt.core package. Accordingly, non-API classes are defined in package org.eclipse.jdt.internal.core. You will see their relation in next section. … Read more

Java: Find all callers of a method – get all methods that call a particular method

This tutorial belongs to Eclipse JDT Tutorial Series.

I will explore how we can compute fan in i.e. find all the methods that call a particular method (all callers of a method).

In Eclipse, one can right click on a method and choose “open call hierarchy” and eclipse shows all the methods that call the selected method and all the methods that are called from the selected method (callers and callees of the selected method).

We can programtically find callers and callees and I will explain how I have done it.

There are a couple of different ways that I know:

  1. Use the eclipse JDT internal classes : these are the same classes used by eclipse to show the callers and callees of a method
  2. Use the eclipse ast visitor

Read more

Count Number of Statements in a Java Method By Using Eclipse JDT ASTParser

This tutorial belongs to Eclipse JDT Tutorial Series.

Given a method, we may want a count of the number of statements in the method (for ex: to gauge the complexity of the method). We can use eclipse JDT ASTParser to get this job done.

One approximate way to count the number of statements in a method is to count the number of lines ending with the semi-colon along with lines that contain the words “if/for/while/”.

Read more

java.lang.IllegalStateException: Workspace is closed.

This post belongs to Eclipse JDT Tutorial Series. This error message starts the whole JDT ASTParser tutorial series. Exception in thread “main” java.lang.IllegalStateException: Workspace is closed. at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:340) at Main.main(Main.java:20) In brief, this is caused by simply adding dependent jar files to regular java project. To use JDT, you need to have the program running … Read more

Eclipse JDT tutorial: Parse a Java method by using ASTParser

This tutorial belongs to Eclipse JDT Tutorial Series. ASTParser can use its setKind() method to set source to be parsed as a compilation unit, single expression, a sequence of statements, or a sequence of class body declarations. parser.setKind(ASTParser.K_COMPILATION_UNIT);parser.setKind(ASTParser.K_COMPILATION_UNIT); Any of those four can not handle a single independent method. This is not pleasant. Following the … Read more

Eclipse JDT Tutorials

JDT is supported by 3 pillars: Java Model, Search Engine, and AST. It is a handy tool for manipulating Java source code. However, the handy tool comes with a steep learning curve even for an experienced Java developer and there are just not enough proper code examples. This page summarizes the code examples for using … Read more

A complete standalone example of ASTParser

This example belongs to Eclipse JDT Tutorial Series. Normally, Eclipse JDT is used in a plug-in environment. This is the normal situation in which we deal with each project in the workspace. However, if we want to parse a large amount of java file, this approach may not be good, since it is not easy … Read more