DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3
  • Understanding and Learning NoSQL Databases With Java: Three Key Benefits
  • Achieving Inheritance in NoSQL Databases With Java Using Eclipse JNoSQL
  • From J2EE to Jakarta EE

Trending

  • Monolith: The Good, The Bad and The Ugly
  • AI-Driven Test Automation Techniques for Multimodal Systems
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 1
  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  1. DZone
  2. Coding
  3. Frameworks
  4. Adding Custom Templates in Eclipse for Faster Java Development

Adding Custom Templates in Eclipse for Faster Java Development

Want to learn more about how to add custom templates in Eclipse? Check out this post on how to implement custom templates for faster Java development.

By 
Gaurav Bhardwaj user avatar
Gaurav Bhardwaj
·
Sep. 12, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
23.4K Views

Join the DZone community and get the full member experience.

Join For Free

As we saw in the last tutorial, Eclipse Templates for Faster Java Development,  Eclipse provides inbuilt templates that we can use to expedite the coding. Apart from that, we can create our own custom templates, as well as for the code, which we find repetitive in nature and can save us a lot of time.

So, to create a new custom template, you need to go to Windows->Preferences

First, you will type Java in the search box and then go to Editor ->Templates

You will see the list of inbuilt templates provided by Eclipse already. To add your own template, click on New:

You will provide your name to the shorthand that will be visible when you will press Ctrl + Space.

In Description, you can mention briefly what this shorthand is about — the context is Java.

If checked, it will automatically insert the code snippet for you by writing shorthand word and pressing Ctrl + space, if there is no other matching template is available.

For example, if you write sysout and press CRTL + Space, it will write System.out.println(); for you, when automatically insert is checked. However, if automatically insert is not checked, then a template proposal will be displayed, which you then need to select.

A pattern is a place where your template goes, below I have included four pattern examples:

1. List Iterator  
2. Map Iterator  
3. Logger  
4. NullCheck

List Iterator

Name: listIterator 
Description: iterate over list
Context: Java
Pattern:

Iterator<${type}> itr = listVar.iterator();
while(itr.hasNext()){
${type} str = (${type})itr.next();
}


Map Iterator

Name: mapIterator 
Description: iterate over map
Context: Java
Pattern:

for (Entry<${keyType:argType(map, 0)},${valueType:argType(map, 1)}> entry : ${map:var(java.util.Map)}.entrySet()) {
${keyType} key = entry.getKey();
${valueType} value = entry.getValue();
${cursor}
}


Logger

Name: logger 
Description : logger Entry
Context : Java
Pattern:

private static final Logger logger = Logger.getLogger(${enclosing_type}.class);


Null Check

Name: NullCheck 
Description: check for null value
Context : Java
Pattern:

if (${var} != null) {
${line_selection}${cursor}
}


Java Editor Template Variables

There are two types of variables that can be used while writing patterns for Java code. These variables are resolved to their actual value when a template is evaluated.

Two types of variables:

1) General template variable
2) Java-specific template variable

We can use variables in two ways, as follows:

Simple variables as below :
${array}


This defines a variable with name array, which can be referenced multiple times and will resolve to an array. Also, we can use the following:

Full variables as below :
${array:var(java.util.iterator)}


This defines a variable with a name as an array that will resolve to a local variable of type  java.util.Iterator. It can be referenced multiple times by giving its name without type like  ${array} .

General Template Variable

${cursor}


This template variable specifies the cursor position when the template edit mode is left. This is useful when the cursor should jump to another place rather than to the end of the template on leaving template edit mode.

${date}


This evaluates the current date.

${dollar}


This evaluates the dollar symbol  $. Alternatively, two dollars can be used:  $$.

${enclosing_method}


This evaluates the name of the enclosing method.

${enclosing_method_arguments}


This evaluates a comma-separated list of argument names of the enclosing method. This variable can be useful when generating log statements for many methods.

${enclosing_package}


This evaluates the name of the enclosing package.

${enclosing_project}


This evaluates the name of the enclosing project.

${enclosing_type}


This evaluates the name of the enclosing type.

${file}


This evaluates the name of the file.

${line_selection}


This evaluates the content of all currently selected lines.

${primary_type_name}


This evaluates the name primary type of the current compilation unit.

${return_type}


This evaluates the return type of the enclosing method.

${time}


This evaluates to the current time.

${user}


This evaluates the username.

${word_selection}


This evaluates the content of the current text selection.

${year}


This evaluates the current year.

2) Java-Specific Template Variables

${array}


The above method evaluates the proposal for an array visible in the current scope.

${array_element}


The above evaluates the name for a new local variable for an element of the ${array} variable match.

${array_type}


This evaluates to the element type of the ${array} variable match.


${collection}


This evaluates a proposal for a collection visible in the current scope.

${exception_variable_name}


This is the exception variable name in catch blocks.

${index}


This evaluates a proposal for an undeclared array index.

${iterator}


The above evaluates an unused name for a new local variable of type  java.util.Iterator.

${iterable}


This evaluates the proposal for an iterable or array visible in the current scope.

${iterable_element}


This evaluates the name for a new local variable for an element of the ${iterable} variable match.

s

This evaluates the element type of the ${iterable} variable match.

${todo}


Lastly, this evaluates a proposal for the currently specified default task tag.

Thanks for reading! Please share this post with someone to whom it might be helpful. 

Template Java (programming language) Eclipse

Published at DZone with permission of Gaurav Bhardwaj. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3
  • Understanding and Learning NoSQL Databases With Java: Three Key Benefits
  • Achieving Inheritance in NoSQL Databases With Java Using Eclipse JNoSQL
  • From J2EE to Jakarta EE

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: