Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-06

Java equivalent of Pascal's class reference functionality

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

I know this is not a Java forum, but thought maybe somebody here with Object Pascal and Java knowledge could help (seeing that Java-only developers might not know what I'm talking about).

Does Java have an equivalent feature of a Object Pascal language's Class Reference.

For Example:
I want to write Java code that does similar to my Object Pascal code shown below:

TVisClassRef = class of TVisitor ;
procedure ExecuteVisitor(const pData: TVisited; const pVisClass:
TVisClassRef);
var
  lVisitor: TVisitor;
begin
  lVisitor := pVisClass.Create;  // <<-- Important Part
  try
    pData.Iterate(lVisitor);
  finally
    lVisitor.Free;
  end;
end;

This procedure takes care of the tedious business of creating the visitor, calling iterate, then freeing the visitor when done. I can pass in various class references as the second argument (all descendants of TVisitor), and different class instances will be created and used as needed.

I would love to know how I could do this in Java.

Regards,
Graeme

Reply by email

Ryan Gonzalez
to Graeme Geldenhuys

Not sure about your question exactly, but everything in Java is passed by reference, and all objects are covariant, so you shouldn't need to do anything.

On Wed, Jun 8, 2016 at 6:17 PM, Graeme Geldenhuys <

Show quoted text 45 lines
wrote: Hi, I know this is not a Java forum, but thought maybe somebody here with Object Pascal and Java knowledge could help (seeing that Java-only developers might not know what I'm talking about). Does Java have an equivalent feature of a Object Pascal language's Class Reference. For Example: I want to write Java code that does similar to my Object Pascal code shown below: TVisClassRef = class of TVisitor ; procedure ExecuteVisitor(const pData: TVisited; const pVisClass: TVisClassRef); var lVisitor: TVisitor; begin lVisitor := pVisClass.Create; // <<-- Important Part try pData.Iterate(lVisitor); finally lVisitor.Free; end; end; This procedure takes care of the tedious business of creating the visitor, calling iterate, then freeing the visitor when done. I can pass in various class references as the second argument (all descendants of TVisitor), and different class instances will be created and used as needed. I would love to know how I could do this in Java. Regards, Graeme _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Ryan [ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong. http://kirbyfan64.github.io/

Reply by email

Graeme Geldenhuys
to Ryan Gonzalez
Ryan Gonzalez wrote
Not sure about your question exactly, but everything in Java is passed by reference

Not quite what I meant. :) The Object Pascal feature is that my example code - ExecuteVisitor() - doesn't know at compile time what instance (any descendant of TVisitor) it is going to instantiate. That information is only example:

  ExecuteVisitor(MyData, TShowNameVisitor);
or
  ExecuteVisitor(MyData, TShowEmailVisitor);
etc.

The ExecuteVisitor() will create an instance of the class I specify as the second argument, and then use it as normal.

Regards,
Graeme

Reply by email

Maciej Izak
to Graeme Geldenhuys: I know this is not a Java forum, but thought maybe somebody here with Object Pascal and…

2016-06-09 1:17 GMT+02:00 Graeme Geldenhuys :

Does Java have an equivalent feature of a Object Pascal language's Class Reference.

+/- yes, it is called Class and you need no-arg constructor for that:

https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html

public void executeVisitor(TVisited pData, Class<TVisitor> pVisClass) {

    TVisitor lVisitor = null;
    try {
        lVisitor := pVisClass.newInstance();  // <<-- Important Part
    } catch (InstantiationException e) {
        LOG.error("HINT: probably missing no-arg constructor for Entry: " +

pVisClass.getName());

        throw e;
    }

...
}

Best regards, Maciej Izak

Reply by email

Graeme Geldenhuys
to Maciej Izak
Maciej Izak wrote
± yes, it is called Class and you need no-arg constructor for that:

Thank you very much Maciej. I couldn't get your code to work. Eclipse (thus Java Compiler) insisted that I replace Class<TVisitor> with Class<TShowNameVisitor> which completely defeats the exercise - the whole point is that I don't know which TVisitor descendant class will be pass in. Very weird.

But, your information got me on the right track. I read some docs, did some more searching and came up with the following solution which works. Step 1 seems complete. :)

public class ClassReferenceTest {

	public void run() {
		System.out.println("Calling executeVisitor() from ClassReferenceTest...");
		executeVisitor(this, TShowNameVisitor.class);
		executeVisitor(this, TShowEmailVisitor.class);
	}

public void executeVisitor(ClassReferenceTest pData, Class<? extends TVisitor> pVisClass) {

		TVisitor lVisitor = null;
		try {
			lVisitor = pVisClass.newInstance();
			lVisitor.execute(this);
		} catch (IllegalAccessException | InstantiationException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		ClassReferenceTest t = new ClassReferenceTest();
		t.run();
	}

}

Console Output:
================
Calling executeVisitor() from ClassReferenceTest... ShowNameVisitor is executing against ClassReferenceTest ShowEmailVisitor is executing against ClassReferenceTest

Perfect!

Regards,
Graeme

Reply by email

Jonas Maebe
to Graeme Geldenhuys
Show quoted text 6 lines
On 09 Jun 2016, at 02:36, Graeme Geldenhuys wrote: > On 2016-06-09 00:46, Maciej Izak wrote: > ± yes, it is called Class and you need no-arg constructor for that: Thank you very much Maciej. I couldn't get your code to work.

Please take this thread, and future similar ones, to the fpc-other list.

Thanks,

Jonas
Fpc mailing lists admin

Reply by email