In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. Extension methods are features of some object-oriented programming languages. There is no syntactic difference between calling an extension method and calling a method declared in the type definition. Extension methods are features of numerous languages including C#, Java via Manifold or Lombok, JavaScript, Ruby, Smalltalk, Kotlin, Visual Basic.NET, etc. But in this article, we will only focus on Java and will learn how we can implement extension methods in Java. In Java we can add extension methods via Manifold, a jar file (Manifold is a Java compiler plugin) we add to our project's classpath. A Java extension method is declared static in an @Extension class where the first argument has the same type as the extended class and is annotated with @This.
Let's understand how we can implement the Extension method in java with the help of one scenario. Suppose we have one JSON file (student-list.json) which contains the list of students and we want to read the content and store it in a String variable. As a developer, we hope for something like this:
student-list.json
{
{
"name":"Bishal Dubey",
"degree":"Bachelor of Technology",
"specialization":"Computer Science & Engineering",
"college":"College Of Engineering, Bhubaneswar",
"batch":"2013-2017",
"dob":"11-11-1994"
},
{
"name":"Tanu Jain",
"degree":"Bachelor of Technology",
"specialization":"Computer Science & Engineering",
"college":"Jaypee Institute of Engineering and Technology, Guna",
"batch":"2015-2019",
"dob":"27-07-1996"
}
}
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
File file = new File("student-list.json");
String content = file.readText();
// But readText() method is not
// available for file object
}
}
Regrettably, when we will type the file. in our IDE then we will get to know that no such method i.e. readText() exists. Next, we will implement the logic to read the text/context of the file. As a good developers, We want to save ourselves and others from duplicating this effort, so we will move this code to a Util library i.e. a separate class like below:
Java
package com.gfg.technicalscripter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class FileReaderUtility {
private static String readText(File file)
throws FileNotFoundException
{
InputStream targetStream
= new FileInputStream(file);
StringBuffer sb = new StringBuffer();
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(targetStream));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
Now, we can write the code as below :
Java
package com.gfg.technicalscripter;
import java.io.File;
import java.io.FileNotFoundException;
public class App {
public static void main(String[] args)
throws FileNotFoundException
{
File file
= new File("src/main/java/student-list.json");
String content = FileReaderUtility.readText(file);
System.out.print(content);
// Now we will get the correct output
}
}
Is this as good as it gets? We deserve a more user-friendly method i.e. how we will get to know that we have one separate utility class which has the readText() method and it returns the content in string format. We should have readText() method as part of the file only. So we can easily call file.readText() to get the content of the file. This readText() is better suited and more easily discoverable as an instance method directly on File. This is where a feature commonly known in the language community as Extension Methods comes into the picture. Before implementing the Extension Methods, we have to add the Manifold extension library to our project.
XML
<dependency>
<groupId>systems.manifold</groupId>
<artifactId>manifold-ext</artifactId>
<version>2022.1.21</version>
<scope>provided</scope>
</dependency>
Manifold fully implements Extension Methods for Java via Extension Classes:
Java
package extensions.java.io.File;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;
@Extension
public class FileExtension {
public static String readText(@This File file)
throws FileNotFoundException
{
InputStream targetStream
= new FileInputStream(file);
StringBuffer sb = new StringBuffer();
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(targetStream));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
Now, we can call readText() method directly on the file instance like below and we will get the same output as via FileReaderUtility readText() method.
Java
package com.gfg.technicalscripter;
import java.io.File;
import java.io.FileNotFoundException;
public class App {
public static void main(String[] args)
throws FileNotFoundException
{
File file
= new File("src/main/java/student-list.json");
String content = file.readText();
System.out.println(content);
}
}
Project Structure:
Extensions eliminate/reduce a lot of intermediate/boiler-plate code such as “Util” and “Manager” libraries as well as Factory classes. Extension methods naturally promote higher levels of object orientation, which result in more readable and maintainable code.
Similar Reads
Java Methods
Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
8 min read
Instance Methods in Java
Instance Methods are the group of codes that performs a particular task. Sometimes the program grows in size, and we want to separate the logic of the main method from other methods. A method is a function written inside the class. Since java is an object-oriented programming language, we need to wr
5 min read
Month get() method in Java
The get() method is a built-in method of the Month ENUM which is used to get the corresponding integral value of the month-of-year values specified by this Month instance. Syntax: public int get(TemporalField field) Parameters: This method accepts a single parameter which is a temporal object and ca
1 min read
Java Interface Methods
In Java, an interface is an abstract type used to specify the behaviour of a class. One of the most important rules when working with interfaces is understanding how methods are declared and implemented. In this article, we are going to learn the interface methods in detail.Note: Interface methods a
3 min read
Static Method in Java With Examples
In Java, the static keyword is used to create methods that belongs to the class rather than any specific instance of the class. Any method that uses the static keyword is referred to as a static method.Features of Static Method:A static method in Java is associated with the class, not with any objec
3 min read
Month getValue() method in Java
The getValue() method is a built-in method of the Month ENUM which is used to get the value of the month-of-year from this Month instance as an integer. The value returned by this method is in the range of 1-12, representing months from January to December. Syntax: public int getValue() Parameters:
1 min read
Month plus() method in Java
The plus() method is a built-in method of the Month ENUM which is used to get a month after the current month by a specific number of months. That is, this method returns the month after the specified number of months from this month. Syntax: public Month plus(long months) Parameters: This method ac
1 min read
Field set() method in Java with Examples
The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is s
4 min read
List size() method in Java with Examples
The size() method of the List interface in Java is used to get the number of elements in this list. That is, the list size() method returns the count of elements present in this list container.Example:Java// Java Program to demonstrate // List size() Method import java.util.*; class GFG { public sta
2 min read
YearMonth getYear() method in Java
The getYear() method of YearMonth class in Java is used to get the value of the Year field from this YearMonth instance with which it is used. It returns the value of the year field as an integer from MIN_YEAR to MAX_YEAR. Syntax: public int getYear() Parameter: This method does not accepts any para
1 min read