How can I, using ANTLR, populate a class of mine while I'm parsing the input message?
For example, if my input message is: name = Paul AND age = 16 AND country = china;
While I'm parsing this message, can I perform, in the Grammar file, something like:
Person p = new Person();
p.setName("Paul");
p.setAge("16");
p.setCountry("china");
Where Person.java is a class that I built. Or this can only be made in the main Java class where I print the AST?
If you want to create instance of your class from name = Paul AND age = 16 AND country = china that kind of script, then you could create yourself method
public <T> T builder(Class<T> clazz, String line)
throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
T instance = clazz.newInstance();
String[] exps = line.split("AND");
for (String exp : exps) {
String[] tokens = exp.split("=", 2);
// TODO check if token has length==2
tokens[0] = tokens[0].trim();
tokens[1] = tokens[1].trim();
String methodName = "set"
+ (("" + tokens[0].charAt(0)).toUpperCase())
+ tokens[0].substring(1);
Method m1 = instance.getClass().getMethod(methodName, String.class);
m1.invoke(instance, tokens[1]);
}
return instance;
}
and when you call it builder(Person.class,"name = Paul AND age = 16 AND country = china") you will get instance of Person class with populated fields name, age and country.
Is that what you are looking for?
Related
currently, I'm doing an assignment that deals with the ArrayList class.
at some point, I need to check of the id of the instructor and make sure that the instructor is not added twice to the ArrayList, so I made a for loop to go through all the id that has been registered and get the id and check if it exists already
the problem is when I use the method " .size()" in the loop, the JVM throws NullPointerException
and I don't know why.
==========================================================================
what I need to read is this:
\\name - id - dateOfBirth - gender - degree - speciality - city - availability
Amanda Smith, 102020, 320101200000, M, PhD, Software Engineering, NewYork, true
=======================================================================
this is the code:
public static void main(String[] args) {
/* NOTE: I HAVE A CLASS CALLED "UniversityMember" THAT IS A SUPERCLASS FOR "Instructor" CLASS */
//declare what I need
ArrayList<UniversityMember> membersList;
Scanner read = new Scanner("inputFile.txt");//the file contains the text above
//First: Split the line everytime the sign ", " shows
String[] line = read.nextLine().split(", ");
//Second: Assign each valuse to its correspondeding variable
String name = line[0];
String id = line[1];
long date = Long.parseLong(line[2]);
Date birthDate = new Date(date);
char gender = line[3].charAt(0);
String degree = line[4];
String specialization = line[5];
String address = line[6];
boolean availability = Boolean.parseBoolean(line[7]);
//check if the Id is registered already
for (int i = 0; i < membersList.size(); i++) { //ERROR OCCURE
if (membersList.get(i) == null) {
break;
}
if (membersList.get(i).id.equals(id)) {
System.out.println("The instructor is registered already, the ID is found in the system.");
System.exit(0);
}
}
//add and make a new object for the constructor
membersList.add(new Instructor(name, id, birthDate, gender, degree, specialization, address, availability));
System.out.println("The instructor is successfully added.");
}//end main
The problem is membersList doesn't exist when you call .size() on it
instead of
ArrayList<UniversityMember> membersList;
you need to initialize it
ArrayList<UniversityMember> membersList = new ArrayList<UniversityMember>();
You need to initialize the ArrayList.
Like that ArrayList membersList = new ArrayList();
After that, in the first size() returns 0 and not null. Remember all data structure must be initialize in java.
You haven't added anything to the membersList then asking for the size for something that has nothing in it.
Example of whats going on
String str;
for(int i = 0; i < str.length(); i++){
System.out.println("hey");
}
also you need to declare the array list like this
ArrayList<Method name> membersList = new ArrayList<Method name>();
also don't forget to import the ArrayList class
import java.util.ArrayList;
nvm I figured out that I haven't initialized my array ( ╥ω╥ )
I'll keep the question for others to be carefull
==================================================
The code after fixing it:
public static void main(String[] args) {
/* NOTE: I HAVE A CLASS CALLED "UniversityMember" THAT IS A SUPERCLASS FOR "Instructor" CLASS */
//declare what I need
ArrayList<UniversityMember> membersList;
Scanner read = new Scanner("inputFile.txt");//the file contains the text above
/* ===== FIXING THE ERROR ======*/
membersList = new ArrayList();
//First: Split the line everytime the sign ", " shows
String[] line = read.nextLine().split(", ");
//Second: Assign each valuse to its correspondeding variable
String name = line[0];
String id = line[1];
long date = Long.parseLong(line[2]);
Date birthDate = new Date(date);
char gender = line[3].charAt(0);
String degree = line[4];
String specialization = line[5];
String address = line[6];
boolean availability = Boolean.parseBoolean(line[7]);
//check if the Id is registered already
for (int i = 0; i < membersList.size(); i++) {
if (membersList.get(i) == null) {
break;
}
if (membersList.get(i).id.equals(id)) {
System.out.println("The instructor is registered already, the ID is found in the system.");
System.exit(0);
}
}
//add and make a new object for the constructor
membersList.add(new Instructor(name, id, birthDate, gender, degree, specialization, address, availability));
System.out.println("The instructor is successfully added.");
}//end main
I am trying to solve this problem for an UDF I am creating for hiveql environment.
public ObjectInspector initialize(ObjectInspector[] arguments)
throws UDFArgumentException {
if (arguments.length != 1) {
throw new UDFArgumentException("Usage : multiple_prop(primitive var) ");
}
// This will be an string
moi = (PrimitiveObjectInspector) arguments[0];
ArrayList structFieldNames = new ArrayList();
ArrayList structFieldObjectInspectors = new ArrayList();
structFieldNames.add("fields name"); <-- Issue is here
How could I do to get the field name in there? It can be easily done for structObjectInspectors, but how do we manage this in PrimitiveObjectInspectors?
Complete code would be this one
public class prop_step2 extends GenericUDF {
private PrimitiveObjectInspector moi;
#Override
public ObjectInspector initialize(ObjectInspector[] arguments)
throws UDFArgumentException {
if (arguments.length != 1) {
throw new UDFArgumentException("Usage : multiple_prop(primitive var) ");
}
// This will be an string
moi = (PrimitiveObjectInspector) arguments[0];
ArrayList structFieldNames = new ArrayList();
ArrayList structFieldObjectInspectors = new ArrayList();
// Change this to get the input variable name, and not the type name
structFieldNames.add(moi.getTypeName());<-- Change this to field name
structFieldObjectInspectors.add( PrimitiveObjectInspectorFactory.writableStringObjectInspector );
return ObjectInspectorFactory.getStandardStructObjectInspector(structFieldNames, structFieldObjectInspectors);
}
#Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
Object[] result;
result = new Object[1];
Text elem1 = new Text((String) moi.getPrimitiveJavaObject(arguments[0].get()));
result[0]= elem1;
return result;
}
#Override
public String getDisplayString(String[] children) {
return "stop";
}}
When this would be finished, i would like to call this udf from hive:
CREATE TEMPORARY FUNCTION step AS 'UDFpack.prop_step2';
select
step(bit) as sd
from my_table
And i would expect that if in an upper select i did this : sd.bit i would obtain the value of 'bit'.
It's simply not possible. The information passed to the UDF - the ObjectInspectors - do not contain their name. That's why you can see the output column names being changed to _col0, _col1 .. in the intermediary stages of a Hive explain plan. I am also quite annoyed by this and think this is an oversight by Hive.
A workaround would be to put your input into a struct and parse that.
i.e step(named_struct('bit',bit)) and then you can get the field name of the struct in your UDF. But it's not nearly as nice
So I'm working on a program to interface with a file based database. Mostly I'm trying to figure out how to work with it so that I can make objects and store their information in the database so that I can pull the data later.
IE Object Taylor
Name = Taylor
Age = 20
School = Whatever
So that I can get back on and call that information up when queried.
This is an example of an object I want to store. I may be doing this part wrong.
package com.catalyse.db;
public class Taylor implements java.io.Serializable
{
public String name = "Taylor M May";
public int age = 20;
public String school = "UC Boulder";
}
The DB structure I'm using is based on RandomAccessFile and I didn't make it, I'm just trying to figure out how to implement it.
package com.catalyse.db;
import java.util.*;
import java.util.Scanner;
/**
* Simple test class for the RecordsFile example. To run the test,
* set you CLASSPATH and then type "java hamner.dbtest.TestRecords"
*/
public class Run {
static void log(String s) {
System.out.println(s);
}
private static String name()
{
Scanner name = new Scanner(System.in);
String name1 = name.next();
return name1;
}
public static void main (String[] args) throws Exception {
System.out.println(new Date());
Scanner SC = new Scanner(System.in);
log("What would you like to name the database?");
String filename = SC.next();
log("creating records file...");
RecordsFile recordsFile = new RecordsFile(filename+".records", 64);
log("adding a record...");
RecordWriter rw = new RecordWriter("foo.username");
rw.writeObject(new Taylor());
recordsFile.insertRecord(rw);
log("reading record...");
RecordReader rr = recordsFile.readRecord("foo.username");
Taylor name = (Taylor)rr.readObject();
System.out.println("\tlast access was at: " + name.toString());
log("test completed.");
}
}
And here is what I get back from it,
Wed Nov 20 11:56:04 MST 2013
What would you like to name the database?
save3
creating records file...
adding a record...
reading record...
last access was at: com.catalyse.db.Taylor#50aed564
test completed.
My problem is that I want it to return information about the class, not just its name and location in the DB.
You need to override the toString method.
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("Name: ");
sb.append(this.name);
//rest of fields
return sb.toString();
}
As a matter of clarity, you are not returning its location in the database. You are getting back the object hashValue + the class name.
At this point
Taylor name = (Taylor)rr.readObject();
You can access whatever information you like in the object, e.g.
Taylor name = (Taylor)rr.readObject();
System.out.println(name.age + ", " + name.name + ", " + name.school);
Alternatively, just add a
public String toString()
{
return name + ", " + age + ", " + school;
}
method in Taylor and then output it like so
Taylor name = (Taylor)rr.readObject();
System.out.println(name);
Now, concerning...
System.out.println("\tlast access was at: " + name.toString());
name.toString() isn't really required. If you append an object to a String then it automatically calls that objects toString() method to get a value.
Lastly, I'd like to note that generally we don't access object members like name, school and age by just accessing them. We generally make them private members then add methods to get and set them, so that we control and can track how they are manipulated.
I have some value which I get in string format from a file.
For example, in the File A I have:
id = 342
name = Jonatan
country = USA
In addition, I have class: person with the next fields:
String id;
String name;
String country;
String grades;
String location;
and I have getter and setter for all the fields.
Now, I want to create a new instance of person, which represents Jonatan.
But - I don't want to update all the field, only the fields I need.
So, what I want to do is the next: get the details from the file, and then for every one do set, and update the correct value. For example, setName(Jonatan). The problems is that my name is in a String format. so I cant do setName - because name is in a string format, and Java doesn't give me the option of call a method in a string format.
There is easy way to it?
You might have a look at Apache BeanUtils
The application is pretty simple - to call setId("42") of a person call:
PropertyUtils.setSimpleProperty(person, "id", "42");
I like #michael_s's answer with BeanUtils. If you want to do it without, you can write:
Person person = new Person();
Properties properties = new Properties();
properties.load(new FileInputStream("the.properties"));
for (Object key : properties.keySet()) {
String field = (String) key;
String setter = "set" + field.substring(0, 1).toUpperCase() + field.substring(1);
Method method = Person.class.getMethod(setter, String.class);
method.invoke(person, properties.get(key));
}
Not that the stream should be closed after using it, and that this short example only works for String properties.
Using java reflection you can determine what methods/fields are available.
You can use this example to pass your key/value pairs into the doReflection method to set the new values of your properties in the instance of the Bean-class.
public class Bean {
private String id = "abc";
public void setId(String s) {
id = s;
}
/**
* Find a method with the given field-name (prepend it with "set") and
* invoke it with the given new value.
*
* #param b The object to set the new value onto
* #param field The name of the field (excluding "set")
* #param newValue The new value
*/
public static void doReflection(Bean b, String field, String newValue) throws NoSuchMethodException,
SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<? extends Bean> c = b.getClass();
Method id = c.getMethod("set" + field, String.class);
id.invoke(b, newValue);
}
public static void main(String... args) throws NoSuchMethodException, SecurityException, IllegalArgumentException,
InvocationTargetException, IllegalAccessException {
Bean bean = new Bean();
System.out.println("ID before: " + bean.id);
doReflection(bean, "Id", "newValue");
System.out.println("ID after: " + bean.id);
// If you have a map of key/value pairs:
Map<String, String> values = new HashMap<String, String>();
for(Entry<String, String> entry : values.entrySet())
doReflection(bean, entry.getKey(), entry.getValue());
}
I need to get private fields from one class and set them to another class.
This code works perfectly well for test Integer fields (100500 value gets written):
//get objects, class firs...
for(int i =0; i<fields1.length; i++) {
Field field1 = fields1 [i];
Field field = fields [i];
field.setAccessible(true);
field.set(app, new Integer(100500));
}
Nontheless, when I switch to undefined type (the fields are of DIFFERENT types: Dates, Integers, Strings...
Eg class one has Date and class two has Date, I need to copy one value from another, but next field is going to be String in both classes)
for(int i =0; i<fields1.length; i++) {
Field field1 = fields1 [i];
Field field = fields [i];
field.setAccessible(true);
field.set(app, field1);
}
I receive an IllegalArgumentException, e.g. I am not really able to get the values from one class and set it into another.
Please, dear community, give me a hint - what am I doing wrong?
to me, it looks like you hand over the field-reference instead of the field-value:
for(int i =0; i<fields1.length; i++) {
Field field1 = fields1 [i];
Field field = fields [i];
field.setAccessible(true);
field.set(app, field1.get(app1)); //or whatever object field1 is from
}
though i would not do it that way because order is a fragile thing...
you could use beanutils to copy your beans, if it is, what you want to achieve
This should suit your needs:
public static <T> void copyDeclaredFields(T from, T to) throws Exception {
Class<?> clazz = from.getClass();
if (!clazz.equals(to.getClass())) {
throw new IllegalArgumentException();
}
for (Field field : clazz.getDeclaredFields()) {
Object value = field.get(from);
field.set(to, value);
}
}
To call:
Item item1 = new Item();
// item1.set...
Item item2 = new Item();
copyDeclaredFields(item1, item2);
Your error seems to be related to the fact that you are trying to set Field1 as parameter of Field, instead of the value of Field1.
field.set(app, field1);
should instead be
field.set(app, field1.get(app1));
Take a look at a small working example;
If you change
fieldDest.set(destination, fieldSrc.get(source));
to
fieldDest.set(destination, fieldSrc);
you will get the same error of this question.
Hope that helps.
Code of the example:
import java.util.*;
import java.lang.*;
import java.lang.reflect.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Source source = new Source();
Destination destination = new Destination();
Class sourceClassObject = source.getClass();
Class destClassObject = destination.getClass();
Field[] sourceFields = sourceClassObject.getDeclaredFields();
Field[] destFields = destClassObject.getDeclaredFields();
for (Field fieldSrc : sourceFields) {
int mod = fieldSrc.getModifiers(); // get modifiers
System.out.print("Source Field: " + Modifier.toString(mod) + " "
+ fieldSrc.getType() + " " + fieldSrc.getName());
fieldSrc.setAccessible(true);
System.out.println(" [" + fieldSrc.get(source) + "]");
for (Field fieldDest : destFields){
if (fieldDest.getType().equals(fieldSrc.getType()) &&
fieldDest.getName().equals(fieldSrc.getName())){
fieldDest.setAccessible(true);
fieldDest.set(destination, fieldSrc.get(source));
}
}
}
destination.printValues();
}
static class Source{
public Source(){
strField = "This is a String";
intField = 42;
dateField = new Date();
}
private String strField;
private Integer intField;
private Date dateField;
}
static class Destination{
private String strField;
private Integer intField;
private Date dateField;
public void printValues(){
System.out.println("Destination Field values: ");
System.out.println("strField: " + strField);
System.out.println("intField: " + intField);
System.out.println("dateField: " + dateField);
}
}
}