Introduction DSCI Provides Establishment and Processing Components In the Data STEP program, Javaobj is an object that provides a mechanism similar to JNI, which can instantiate Java classes, access object properties, and methods.
Define Java objects to support: Declare Javaobj J ("Class Name"); this will define and store an instance of an object in scalar J, Javaobj will instantiate the SomeJavaclass under Java ClassPath to be typical, to ensure Instantiation J is only once, can do if _n_ = 1 Then DO; DECLARE JAVAOBJ J ("Somejavaclass"); end; javaobj can also use _new_ syntax to instantiate DecLare Javaobj J; j = _new_ javaobj ("Somejavaclass") ;
Texture parameters
Javaobj Constructs the first parameter of an object of an object is the name of the Java class, such as building a HashTable object Declare Javaobj H ("java / util / hashtable"); other corresponding parameters can also be passed over to Java Class itself as established Hashtable objects with a capacity of 100, a coefficient of 0.8, and we should encapsulate the Java / Util / HashTable class and instantiate its import java.util. *;
Public Class Mhash Extends Hashtable {Mhash (Double Size, Double Load) {Super ((int) size, (float) load);}}
Declare Javaobj H ("Mhash", 100, .8); package class is necessary, because Data Types in Data STEP correspond to Double in Java
Access Object Properties Once the object is instantiated, Javaobj is allowed to access and modify the object properties by allowing users to use, if we envisage We are made by such type import java.util. *; Import java.lang. *; Public class ttest {public INT i Public double d; public string s; we build this class, use the SET / GET access object properties in the following programs Data _null_; DCL Javaobj J ("TTEST"); Length Val 8; Length Str $ 20; J.Setintfield ("I", 100); J.SetDoublefield ("D", 3.14159); J.SetStringField ("S", "ABC");
J.Getintfield ("i", val); put val =; j.getdoublefield ("d", val); put val =; j.getstringfield ("s", str); PUT STR =;
The first parameter of the set / get method is the attribute name, the second parameter is the variable or value of Get / SET.
The access object method can transform the above class into such import java.util. *; Import java.lang. *; Public class ttest {public Int i; public double d; public string S;
Public int im () {returni}
Public string sm () {return d;} public double d;} We can call access Java values via Javaobj method, such as DATA _NULL_; DCL Javaobj J ("TTEST"); Length Val 8; Length Str $ 20; J.SetintField ("I", 100); J.Setdoublefield ("D", 3.14159); J.SetstringField ("S", "ABC");
J.CallintMethod ("IM", VAL); PUT VAL =; J.CallDoubleMethod ("DM", VAL); PUT VAL =; J.CallstringMethod ("SM", STR); PUT STR =; RUN;
Note: The return value of the method is always specified as the last parameter, for example, we have a method returned to Double Method PUBLIC DOUBLE M (Double X, Double Y) {Return X * Y;} So call this method in Data STEP Use the following syntax Length Val1 Val2 Ret 8; J.CallDoubleMethod ("M", VAL1, VAL2, RET);
Access the properties and methods of the class Access the static properties and methods of the class, we can use the appropriate Javaobj method, such as import java.util. *; Import java.lang. *; Public class ttestc {public static double d; public static double DM () {RETURN D;}} Data X; DCL Javaobj J ("TTESTC") in Data Step; Length D 8;
J.CallSetStaticDoublefield ("D", 3.14159); J.CallStaticDoubleMethod ("DM", D); PUT D =;
Data type
The Java Data Type Set is a super-collection of Data STEP. The main type of the latter is digital and character types, while Java is included in addition to digital and characters, such as Byte, Char, Short, etc., using javaobj method is all Java. The digital type corresponds to the array type in the Data STEP, and Java String is mapped to the Data STEP's character type (for UTF string), the character type cannot be mapped into the Data STEP, and the object cannot be returned by the Java Class to Data STEP (String object) Except, except that allows the transfer object to Java Class
Some Java classes returns to objects, we can encapsulate change to get the value of the object, for example
Import java.util. *;
Public class mhash {private hashtable table;
Public mhash () {Table = new hashtable ();
Public void Put (double key, double value) {Table.Put (New Double (key), new double (value);}
Public void Put (String Key, String Value) {Table.Put (Key, Value);
Public Double Get {Object Ret = Table.Get (NEW DOUBLE); if (re-required) return ((Double) Ret) .doubleValue (); else returno Double.nan;} public string get (String Key) {return (string) Table.get (key);}}
Data STEP uses Data _null_; Length S $ 20;
/ * * SIMPLE JAVA HASH TABLE TEST. Mhash.class is a wrapper * for Java / Util / Hashtable - So That We can pass java strings * Directly Instead of Generic Objects. * / Declare Javaobj H ("Mhash");
/ * Load Up the table * / h.callvoidMethod ("Put", "Key1", "DATA1"); H.callvoidMethod ("Put", "Key2", "DATA2"); H.callvoidMethod ("Put", "Key3", "DATA3"); H.callvoidMethod ("Put", "Key4", "DATA4"); H.CallvoidMethod ("Put", "Key5", "DATA5");
/ * Retrieve a value * / h.callstringmethod ("get", "key3", s); put s;
Array uses the Data STEP array to pass directly to Java objects, such as import java.util. *; Import java.lang. *; Class Jtest {public void DBL (Double Args []) {for (int i = 0; i < Args.Length; i ) System.out.println (Args [i]);
Public void str (string args []) {for (int i = 0; I Data _null_; DCL Javaobj J ("Jtest"); Array S [3] $ 20 ("ABC", "DEF", "GHI"); Array D [10] (1:10); J.CallvoidMethod ("DBL" , D); J.CallvoidMethod ("Str", S); RUN;