Showing posts with label type. Show all posts
Showing posts with label type. Show all posts

Tuesday, 6 April 2021

Php: is_array(): Chek whether a variable is an array

Signature

is_array ( mixed $value ) : bool

 

Description

Finds whether the given variable is an array. Returns true if value is an array, false otherwise.

 

Example

$is_arr1_array = is_array($arr1);

 

is_array_demo.php

#!/usr/bin/php

<?php

    $arr1 = array(22, 31, -5, 77, -98, 18);
    $var1 = 10;
    
    $is_arr1_array = is_array($arr1);
    $is_var1_array = is_array($var1);

    echo "is \$arr1 array ? ";
    var_dump($is_arr1_array);
    echo "\nis \$var1 array ? ";
    var_dump($is_var1_array);
?>

 

Output

$./is_array_demo.php 

is $arr1 array ? bool(true)

is $var1 array ? bool(false)

 

 

 

  

Previous                                                    Next                                                    Home

Wednesday, 29 April 2020

Javassist: Modify type of instance field

It is a two-step process.
a.   Remove the field
b.   Create a filed with same name with different type

Follow below step-by-step procedure to modify type of a field.

Step 1: Get ClassPool instance.
ClassPool classPool = ClassPool.getDefault();

Step 2: Get CtClass instance.
CtClass pointClass = classPool.get("com.sample.app.model.Point");

Step 3: Remove the field from CtClass instance.
CtField toBeDeleted = pointClass.getField("x");
pointClass.removeField(toBeDeleted);

Step 4: Create field with same with different data type.
CtField xField = new CtField(CtClass.doubleType, "x", pointClass);
xField.setModifiers(Modifier.PUBLIC);

Step 5: Add new field to the class.
pointClass.addField(xField);

Step 6: Publish modified byte code
pointClass.toClass();

Find the below working application.

Point.java
package com.sample.app.model;

public class Point {
    public int x;
    public int y;

}

App.java
package com.sample.app;

import java.lang.reflect.Field;

import com.sample.app.model.Point;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.Modifier;

public class App {

    public static boolean set(Object object, String fieldName, Object fieldValue) {
        Class<?> clazz = object.getClass();
        while (clazz != null) {
            try {
                Field field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                field.set(object, fieldValue);
                return true;
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    public static <V> V get(Object object, String fieldName) {
        Class<?> clazz = object.getClass();
        while (clazz != null) {
            try {
                Field field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                return (V) field.get(object);
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }
        return null;
    }

    public static void main(String args[]) throws Exception {
        ClassPool classPool = ClassPool.getDefault();

        CtClass pointClass = classPool.get("com.sample.app.model.Point");

        CtField toBeDeleted = pointClass.getField("x");
        pointClass.removeField(toBeDeleted);

        CtField xField = new CtField(CtClass.doubleType, "x", pointClass);
        xField.setModifiers(Modifier.PUBLIC);
        pointClass.addField(xField);

        // Publish modified byte code
        pointClass.toClass();

        pointClass.writeFile("/Users/Shared/javassist");
        Point point = new Point();

        set(point, "x", 1.2345);
        Object value = get(point, "x");

        System.out.println("Value of x is " + value);
    }

}

Run App.java, you will see below message in console.
Value of x is 1.2345

You can observe that Point.class file is created at /Users/Shared/javassist.

$tree /Users/Shared/javassist
/Users/Shared/javassist
└── com
    └── sample
        └── app
            └── model
                └── Point.class

4 directories, 1 file

Open Point.class in any java decompiler to see the source code.



Previous                                                    Next                                                    Home

Friday, 10 April 2020

TableSaw: Get all the columns of specific type

‘table.columnsOfType’ method returns all the columns of specific type.

Example
table.columnsOfType(ColumnType.STRING).forEach(column -> System.out.println(column.print()));

Above statement print all the columns of type string.

App.java
package com.sample.app;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

import tech.tablesaw.api.ColumnType;
import tech.tablesaw.api.DateColumn;
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 };
            String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu" };
            String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev" };

            List<LocalDate> dateOfBirths = Arrays.asList(LocalDate.of(1988, 5, 1), LocalDate.of(1976, 8, 1),
                        LocalDate.of(1948, 5, 11), LocalDate.of(1968, 5, 21));

            Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds))
                        .addColumns(StringColumn.create("FirstName", firstNames))
                        .addColumns(StringColumn.create("LastName", lastNames))
                        .addColumns(DateColumn.create("Date Of Birth", dateOfBirths));

            System.out.println(table.print() + "\n");

            table.columnsOfType(ColumnType.STRING).forEach(column -> System.out.println(column.print()));

      }
}

Output
Employee Ids  |  FirstName  |  LastName  |  Date Of Birth  |
-------------------------------------------------------------
            1  |       Hari  |   Krishna  |     1988-05-01  |
            2  |        Ram  |    Gurram  |     1976-08-01  |
            3  |     Sowmya  |       Maj  |     1948-05-11  |
            4  |      Chamu  |       Dev  |     1968-05-21  |

Column: FirstName
Hari
Ram
Sowmya
Chamu

Column: LastName
Krishna
Gurram
Maj
Dev




Previous                                                    Next                                                    Home

TableSaw: Table: Get columns of desired type

There are two ways to get the column of desired type.
a.   By doing explicit casting
b.   By using the methods that return columns of desired type

By doing explicit casting
DateColumn dateColumn = (DateColumn) table.column(3);

App.java
package com.sample.app;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

import tech.tablesaw.api.DateColumn;
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 };
            String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu" };
            String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev" };

            List<LocalDate> dateOfBirths = Arrays.asList(LocalDate.of(1988, 5, 1), LocalDate.of(1976, 8, 1),
                        LocalDate.of(1948, 5, 11), LocalDate.of(1968, 5, 21));

            Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds))
                        .addColumns(StringColumn.create("FirstName", firstNames))
                        .addColumns(StringColumn.create("LastName", lastNames))
                        .addColumns(DateColumn.create("Date Of Birth", dateOfBirths));

            System.out.println(table.print());

            DateColumn dateColumn = (DateColumn) table.column(3);

            System.out.println("\n\n" + dateColumn.print());

      }
}


Output
Employee Ids  |  FirstName  |  LastName  |  Date Of Birth  |
-------------------------------------------------------------
            1  |       Hari  |   Krishna  |     1988-05-01  |
            2  |        Ram  |    Gurram  |     1976-08-01  |
            3  |     Sowmya  |       Maj  |     1948-05-11  |
            4  |      Chamu  |       Dev  |     1968-05-21  |


Column: Date Of Birth
1988-05-01
1976-08-01
1948-05-11
1968-05-21

b. By using the methods that return columns of desired type
DateColumn dateColumn1 = table.dateColumn(3);
DateColumn dateColumn2 = table.dateColumn("Date Of Birth");

App.java
package com.sample.app;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

import tech.tablesaw.api.DateColumn;
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 };
            String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu" };
            String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev" };

            List<LocalDate> dateOfBirths = Arrays.asList(LocalDate.of(1988, 5, 1), LocalDate.of(1976, 8, 1),
                        LocalDate.of(1948, 5, 11), LocalDate.of(1968, 5, 21));

            Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds))
                        .addColumns(StringColumn.create("FirstName", firstNames))
                        .addColumns(StringColumn.create("LastName", lastNames))
                        .addColumns(DateColumn.create("Date Of Birth", dateOfBirths));

            System.out.println(table.print());

            DateColumn dateColumn1 = table.dateColumn(3);
            DateColumn dateColumn2 = table.dateColumn("Date Of Birth");

            System.out.println("\n\n" + dateColumn1.print());
            System.out.println("\n\n" + dateColumn2.print());

      }
}

Output

Employee Ids  |  FirstName  |  LastName  |  Date Of Birth  |
-------------------------------------------------------------
            1  |       Hari  |   Krishna  |     1988-05-01  |
            2  |        Ram  |    Gurram  |     1976-08-01  |
            3  |     Sowmya  |       Maj  |     1948-05-11  |
            4  |      Chamu  |       Dev  |     1968-05-21  |


Column: Date Of Birth
1988-05-01
1976-08-01
1948-05-11
1968-05-21



Column: Date Of Birth
1988-05-01
1976-08-01
1948-05-11
1968-05-21





Previous                                                    Next                                                    Home