2

I have a file named Objects.dat and I want to read the Class name and objects name from that file using Java Reflection. I'm able to read from another Java class but not from the file. How can I solve this?

Java Class

public class EmployeeInfo {
    private String username = "John";
    private int userage = 23;
}

Objects.dat contains the same text as Java class.

Class Reader

public class FileRd {
    public static void main(String[] args)  {
            try {
                Class cls = Class.forName("EmployeeInfo");
                Object obj = cls.newInstance();

                System.out.println("Class Name-->"+obj.getClass());
                Field[] fields = cls.getDeclaredFields();
                for( int i = 0 ; i < fields.length ; i++ ) {
                    fields[i].setAccessible(true);
                    System.out.println("Name-->"+fields[i].getName());
                }
            }
            catch( Exception e ) { e.printStackTrace(); }
    }
}

The above code works for the Java class but I want to input the file like and read -

FileInputStream fin = new FileInputStream("D:\\Objects.bat");

and perform the above functionally but I failed to do that.

6
  • Are you sure you want reflection? I think serialization would be the way to go here. Commented Apr 26, 2018 at 16:22
  • Sorry actually I don't have any idea about JAVA Reflection so i searched for last couple of hours and i just got this idea. If you can suggest this will be helpful for me . Thanks. Commented Apr 26, 2018 at 16:25
  • Some basic info is here: oracle.com/technetwork/articles/java/javaserial-1536170.html (Sorry for that previous link, this one is actually on point.) Commented Apr 26, 2018 at 16:32
  • Isn't it possible to read class and object name from the input file instead of reading from a JAVA class ? Commented Apr 26, 2018 at 16:40
  • I think it might be "possible" but I don't see how that's useful or a good idea. Commented Apr 26, 2018 at 16:44

2 Answers 2

1

You need a Custom classloader for such, if the file is already compiled (if not you can use javac tool to compile it with classpath or an Bytecode tool like ASM or Javassist).

Then you use your ClassLoader to load the file(.class) and findClass.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe Serialization / Deserialization is the better way than save Java code at the file.

5 Comments

Isn't it possible to read class and object name from the input file instead of reading from a JAVA class ?
You can parse it with regex or substring: If you read file line by line: try parse with substring: string className = line.subString(line.indexOf("class"), line.indexOf("}")).trim();
but how could i got the objects name and type in this way ?
Only for class name: String input = "public class Test { " + "\ncode;" + "\ncode;" + "\n}"; String className = input.substring(input.indexOf("class") + "class".length(), input.indexOf("{")).trim(); System.out.println(className);
But I also need the Objects name and type from that file. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.