This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
Showing posts with label remove. Show all posts
Showing posts with label remove. Show all posts
Saturday, 9 May 2020
Sunday, 3 May 2020
Javassist: Remove a method from an existing class
Follow below steps to remove a method from an existing class.
Step 1: Get ClassPool instance.
ClassPool classPool = ClassPool.getDefault();
Step 2: Get CtClass instace for the class that you want to remove a method.
CtClass testClass = classPool.get("com.sample.app.Test");
Step 3: Get CtMethod instance of the class that you want to remove.
CtMethod sayHelloMethod = testClass.getDeclaredMethod("sayHello");
Step 4: Use removeMethod function to remove the method.
testClass.removeMethod(sayHelloMethod);
Step 5: Publish byte code changes.
testClass.toClass();
Find below working application.
package com.sample.app;
public class Test {
public void sayHello() {
System.out.println("Hello");
}
public void sayHi() {
System.out.println("Hi");
}
}
App.java
package com.sample.app;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
public class App {
public static void main(String args[]) throws Exception {
ClassPool classPool = ClassPool.getDefault();
CtClass testClass = classPool.get("com.sample.app.Test");
CtMethod sayHelloMethod = testClass.getDeclaredMethod("sayHello");
testClass.removeMethod(sayHelloMethod);
testClass.toClass();
Test test = new Test();
test.sayHi();
test.sayHello();
}
}
Run App.java, you will see 'NoSuchMethodError' in console.
Hi
Exception in thread "main" java.lang.NoSuchMethodError: com.sample.app.Test.sayHello()V
at com.sample.app.App.main(App.java:22)
Saturday, 2 May 2020
Javassist: Remove CtClass object from ClassPool
If number of CtClass objects handled by ClassPool object are large, then you may end up in high menory memory consumption problems. To solve this, whenever you do not need CtClass objects, remove them from the pool explicitly.
How to remove a CtClass from ClassPool?
Using dtach method of CtClass, you can remove the CtClass object from pool.
Example
CtClass ctClass = ... ;
ctClass.detach();
package com.sample.app;
import javassist.ClassPool;
import javassist.CtClass;
public class App {
public static void main(String args[]) throws Exception {
ClassPool classPool = ClassPool.getDefault();
classPool.makeClass("com.sample.app.model.Point");
CtClass ctClass1 = classPool.get("com.sample.app.model.Point");
System.out.println("Class Name : " + ctClass1.getName());
System.out.println("Detaching the class from pool");
ctClass1.detach();
System.out.println("Retrieving the ctClass from pool");
CtClass ctClass2 = classPool.get("com.sample.app.model.Point");
System.out.println("ctClass2 : " + ctClass2);
}
}
Run App.java, you will get below messages in console.
Class Name : com.sample.app.model.Point
Detaching the class from pool
Retrieving the ctClass from pool
Exception in thread "main" javassist.NotFoundException: com.sample.app.model.Point
at javassist.ClassPool.get(ClassPool.java:430)
at com.sample.app.App.main(App.java:21)
Saturday, 11 April 2020
Java: Remove first character of a string
Using ‘subString’ method, you can extract the string except the first character.
Example
public static String removeFirstChar(String str) {
if (str == null || str.isEmpty()) {
throw new IllegalArgumentException("Input must not be null or empty");
}
return str.substring(1);
}
StringUtil.java
package com.smaple.app.utils;
public class StringUtil {
public static String removeFirstChar(String str) {
if (str == null || str.isEmpty()) {
throw new IllegalArgumentException("Input must not be null or empty");
}
return str.substring(1);
}
}
StringUtilTest.java
package com.sample.app.utils;
import static com.smaple.app.utils.StringUtil.removeFirstChar;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringUtilTest {
@Test(expected = IllegalArgumentException.class)
public void inputIsNull() {
String text = null;
removeFirstChar(text);
}
@Test(expected = IllegalArgumentException.class)
public void inputIsEmpty() {
String text = "";
removeFirstChar(text);
}
@Test
public void inputLengthIs1() {
String text = "a";
String expected = "";
String result = removeFirstChar(text);
assertEquals(expected, result);
}
@Test
public void inputLengthIsGreaterThan1() {
String text = "hello";
String expected = "ello";
String result = removeFirstChar(text);
assertEquals(expected, result);
}
}
You may
like
Monday, 6 April 2020
TableSaw: Remove columns with missing values
‘table.removeColumnsWithMissingValues’ method remove the columns with missing values.
App.java
package com.sample.app;
import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;
public class App {
public static void main(String args[]) {
int[] empIds = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
String[] firstNames = { "Hari", null, null, null, "Harini", "Lahari", "Rama", "Lakshman", "Sandya" };
String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev", "Gurram", "Ram", "Sen", "Grandi", "Neelam" };
Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds))
.addColumns(StringColumn.create("FirstName", firstNames))
.addColumns(StringColumn.create("LastName", lastNames));
System.out.println(table.print());
table.removeColumnsWithMissingValues();
System.out.println("\n\n" + table.print());
}
}
Output
Employee Ids | FirstName | LastName |
-------------------------------------------
1 | Hari | Krishna |
2 | | Gurram |
3 | | Maj |
4 | | Dev |
5 | Harini | Gurram |
6 | Lahari | Ram |
7 | Rama | Sen |
8 | Lakshman | Grandi |
9 | Sandya | Neelam |
Employee Ids | LastName |
-----------------------------
1 | Krishna |
2 | Gurram |
3 | Maj |
4 | Dev |
5 | Gurram |
6 | Ram |
7 | Sen |
8 | Grandi |
9 | Neelam |
As you see in the above example, Column FirstName has missing values for the rows 2, 3, and 4. When I call the method ‘removeColumnsWithMissingValues’ it removes the column FirstName.
Sunday, 5 April 2020
Table: Remove specific columns from a table
TableSaw provides below methods to remove columns from a table.
public Table removeColumns(Column<?>... columns)
public Relation removeColumns(int... columnIndexes)
public Relation removeColumns(String... columnName)
package com.sample.app;
import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;
public class App {
public static void main(String args[]) {
int[] empIds = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
String[] firstNames = { "Hari", "Ram", "Chamu", "Sowmya", "Harini", "Lahari", "Rama", "Lakshman", "Sandya" };
String[] lastNames = {"Krishna", "Gurram", "Maj", "Dev", "Gurram", "Ram", "Sen", "Grandi", "Neelam"};
Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds))
.addColumns(StringColumn.create("FirstName", firstNames)).addColumns(StringColumn.create("LastName", lastNames));
System.out.println(table.print());
table.removeColumns("FirstName");
System.out.println("\nRemoving the column FirstName\n");
System.out.println(table.print());
}
}
Output
Employee Ids | FirstName | LastName |
-------------------------------------------
1 | Hari | Krishna |
2 | Ram | Gurram |
3 | Chamu | Maj |
4 | Sowmya | Dev |
5 | Harini | Gurram |
6 | Lahari | Ram |
7 | Rama | Sen |
8 | Lakshman | Grandi |
9 | Sandya | Neelam |
Removing the column FirstName
Employee Ids | LastName |
-----------------------------
1 | Krishna |
2 | Gurram |
3 | Maj |
4 | Dev |
5 | Gurram |
6 | Ram |
7 | Sen |
8 | Grandi |
9 | Neelam |
Subscribe to:
Posts (Atom)