Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30964 > unrolled thread
| Started by | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| First post | 2015-01-31 19:02 +0100 |
| Last post | 2015-02-05 04:37 -0800 |
| Articles | 11 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
super object reference Philipp Kraus <philipp.kraus@flashpixx.de> - 2015-01-31 19:02 +0100
Re: super object reference Jeff Higgins <jeff@invalid.invalid> - 2015-01-31 13:14 -0500
Re: super object reference Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2015-01-31 22:47 -0800
Re: super object reference Philipp Kraus <philipp.kraus@flashpixx.de> - 2015-02-01 12:40 +0100
Re: super object reference Arne Vajhøj <arne@vajhoej.dk> - 2015-02-01 09:13 -0500
Re: super object reference Arne Vajhøj <arne@vajhoej.dk> - 2015-02-01 09:21 -0500
Re: super object reference Philipp Kraus <philipp.kraus@flashpixx.de> - 2015-02-01 23:44 +0100
Re: super object reference Jeff Higgins <jeff@invalid.invalid> - 2015-02-01 09:23 -0500
Re: super object reference Arne Vajhøj <arne@vajhoej.dk> - 2015-02-01 09:30 -0500
Re: super object reference Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2015-02-02 19:46 -0800
Re: super object reference Roedy Green <see_website@mindprod.com.invalid> - 2015-02-05 04:37 -0800
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2015-01-31 19:02 +0100 |
| Subject | super object reference |
| Message-ID | <maj5b2$1fos$1@ariadne.rz.tu-clausthal.de> |
Hello,
I have got this class structure:
clas parent
{
int value = 5;
}
class child extends parent
{
public doit()
{
Object x = this.super; **
}
}
I would like to get at ** a reference to the super object of my class,
I need a reference to the full object not to particular methods and
properties.
How can I do this?
Thanks a lot
Phil
[toc] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-01-31 13:14 -0500 |
| Message-ID | <maj67l$mol$1@dont-email.me> |
| In reply to | #30964 |
On 01/31/2015 01:02 PM, Philipp Kraus wrote:
> Hello,
>
> I have got this class structure:
>
> clas parent
> {
> int value = 5;
> }
>
> class child extends parent
> {
>
> public doit()
> {
> Object x = this.super; **
> }
>
> }
>
> I would like to get at ** a reference to the super object of my class, I
> need a reference to the full object not to particular methods and
> properties.
> How can I do this?
>
this is a reference to the full object
there is no "super object" only a super class
[toc] | [prev] | [next] | [standalone]
| From | Kevin McMurtrie <mcmurtrie@pixelmemory.us> |
|---|---|
| Date | 2015-01-31 22:47 -0800 |
| Message-ID | <mcmurtrie-08783E.22470031012015@news.sonic.net> |
| In reply to | #30964 |
In article <maj5b2$1fos$1@ariadne.rz.tu-clausthal.de>,
Philipp Kraus <philipp.kraus@flashpixx.de> wrote:
> Hello,
>
> I have got this class structure:
>
> clas parent
> {
> int value = 5;
> }
>
> class child extends parent
> {
>
> public doit()
> {
> Object x = this.super; **
> }
>
> }
>
> I would like to get at ** a reference to the super object of my class,
> I need a reference to the full object not to particular methods and
> properties.
> How can I do this?
>
> Thanks a lot
>
> Phil
You have two classes but only one instance object. There is no parent
instance.
For proper design, a child class must not contradict the declared API of
the parent class. There's refactoring needed if it does. For example,
LinkedHashMap does not behave in any way that contradicts the declared
API of HashMap.
Maybe you're thinking of inner/outer classes? An outer class is
OuterClassName.this.
--
I will not see posts from astraweb, theremailer, dizum, or google
because they host Usenet flooders.
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2015-02-01 12:40 +0100 |
| Message-ID | <mal3bb$20i5$1@ariadne.rz.tu-clausthal.de> |
| In reply to | #30978 |
On 2015-02-01 06:47:00 +0000, Kevin McMurtrie said:
> In article <maj5b2$1fos$1@ariadne.rz.tu-clausthal.de>,
> Philipp Kraus <philipp.kraus@flashpixx.de> wrote:
>
>> Hello,
>>
>> I have got this class structure:
>>
>> clas parent
>> {
>> int value = 5;
>> }
>>
>> class child extends parent
>> {
>>
>> public doit()
>> {
>> Object x = this.super; **
>> }
>>
>> }
>>
>> I would like to get at ** a reference to the super object of my class,
>> I need a reference to the full object not to particular methods and
>> properties.
>> How can I do this?
>>
>> Thanks a lot
>>
>> Phil
>
>
> You have two classes but only one instance object. There is no parent
> instance.
>
> For proper design, a child class must not contradict the declared API of
> the parent class. There's refactoring needed if it does. For example,
> LinkedHashMap does not behave in any way that contradicts the declared
> API of HashMap.
>
> Maybe you're thinking of inner/outer classes? An outer class is
> OuterClassName.this.
I see this is a conceptional design error at my code. I use my variable x
within a deeper framewok call and this call runs
x.getclass.getDecalredFields(), but
this returns in the example case nothing, ebcause my child class does
not have any fields.
I have got only the x reference but I need all fields of the object,
also the fields of the child & parent
class and fields of the parent-parent class... I would like to create a
field list from my object x over all
fields that are exists within the objects. IMHO I must run from my
object class up to the java.object and collect all fields.
How can I do this?
Phil
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-02-01 09:13 -0500 |
| Message-ID | <54ce348a$0$290$14726298@news.sunsite.dk> |
| In reply to | #30980 |
On 2/1/2015 6:40 AM, Philipp Kraus wrote: > I see this is a conceptional design error at my code. I use my variable x > within a deeper framewok call and this call runs > x.getclass.getDecalredFields(), but > this returns in the example case nothing, ebcause my child class does > not have any fields. > > I have got only the x reference but I need all fields of the object, > also the fields of the child & parent > class and fields of the parent-parent class... I would like to create a > field list from my object x over all > fields that are exists within the objects. IMHO I must run from my > object class up to the java.object and collect all fields. > How can I do this? You recurse. First getDeclaredFields() and then getSuperclass() and process that. Arne
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-02-01 09:21 -0500 |
| Message-ID | <54ce3657$0$292$14726298@news.sunsite.dk> |
| In reply to | #30981 |
On 2/1/2015 9:13 AM, Arne Vajhøj wrote:
> On 2/1/2015 6:40 AM, Philipp Kraus wrote:
>> I see this is a conceptional design error at my code. I use my variable x
>> within a deeper framewok call and this call runs
>> x.getclass.getDecalredFields(), but
>> this returns in the example case nothing, ebcause my child class does
>> not have any fields.
>>
>> I have got only the x reference but I need all fields of the object,
>> also the fields of the child & parent
>> class and fields of the parent-parent class... I would like to create a
>> field list from my object x over all
>> fields that are exists within the objects. IMHO I must run from my
>> object class up to the java.object and collect all fields.
>> How can I do this?
>
> You recurse.
>
> First getDeclaredFields() and then getSuperclass() and process that.
Demo:
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class AllFields {
private static void analyze(Class<?> clz, List<Field> fields) {
for(Field f : clz.getDeclaredFields()) fields.add(f);
if(!clz.getSuperclass().equals(Object.class))
analyze(clz.getSuperclass(), fields);
}
public static void dump(Object o) {
System.out.println(o.getClass().getName());
List<Field> fields = new ArrayList<>();
analyze(o.getClass(), fields);
for(Field f : fields) {
System.out.println(" " + f.getName());
}
}
public static void main(String[] args) {
dump(new A());
dump(new B());
dump(new C());
}
}
class A {
private Object a;
}
class B extends A {
private Object b;
}
class C extends B {
private Object c;
}
Arne
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2015-02-01 23:44 +0100 |
| Message-ID | <mama88$28if$1@ariadne.rz.tu-clausthal.de> |
| In reply to | #30982 |
Gerate, thanks for all the help.
Phil
On 2015-02-01 14:21:04 +0000, Arne Vajhøj said:
> On 2/1/2015 9:13 AM, Arne Vajhøj wrote:
>> On 2/1/2015 6:40 AM, Philipp Kraus wrote:
>>> I see this is a conceptional design error at my code. I use my variable x
>>> within a deeper framewok call and this call runs
>>> x.getclass.getDecalredFields(), but
>>> this returns in the example case nothing, ebcause my child class does
>>> not have any fields.
>>>
>>> I have got only the x reference but I need all fields of the object,
>>> also the fields of the child & parent
>>> class and fields of the parent-parent class... I would like to create a
>>> field list from my object x over all
>>> fields that are exists within the objects. IMHO I must run from my
>>> object class up to the java.object and collect all fields.
>>> How can I do this?
>>
>> You recurse.
>>
>> First getDeclaredFields() and then getSuperclass() and process that.
>
> Demo:
>
> import java.lang.reflect.Field;
> import java.util.ArrayList;
> import java.util.List;
>
> public class AllFields {
> private static void analyze(Class<?> clz, List<Field> fields) {
> for(Field f : clz.getDeclaredFields()) fields.add(f);
> if(!clz.getSuperclass().equals(Object.class))
> analyze(clz.getSuperclass(), fields);
> }
> public static void dump(Object o) {
> System.out.println(o.getClass().getName());
> List<Field> fields = new ArrayList<>();
> analyze(o.getClass(), fields);
> for(Field f : fields) {
> System.out.println(" " + f.getName());
> }
> }
> public static void main(String[] args) {
> dump(new A());
> dump(new B());
> dump(new C());
> }
> }
>
> class A {
> private Object a;
> }
>
> class B extends A {
> private Object b;
> }
>
> class C extends B {
> private Object c;
> }
>
> Arne
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-02-01 09:23 -0500 |
| Message-ID | <mald21$43j$1@dont-email.me> |
| In reply to | #30980 |
On 02/01/2015 06:40 AM, Philipp Kraus wrote: > > I see this is a conceptional design error at my code. I use my variable x > within a deeper framewok call and this call runs > x.getclass.getDecalredFields(), but > this returns in the example case nothing, ebcause my child class does > not have any fields. > > I have got only the x reference but I need all fields of the object, > also the fields of the child & parent > class and fields of the parent-parent class... I would like to create a > field list from my object x over all > fields that are exists within the objects. IMHO I must run from my > object class up to the java.object and collect all fields. > How can I do this? > <https://github.com/ronmamo/reflections> ReflectionUtils.getAllFields(...) get all fields of given type, up the super class hierarchy, optionally filtered by predicates
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2015-02-01 09:30 -0500 |
| Message-ID | <54ce387d$0$281$14726298@news.sunsite.dk> |
| In reply to | #30984 |
On 2/1/2015 9:23 AM, Jeff Higgins wrote: > On 02/01/2015 06:40 AM, Philipp Kraus wrote: >> I see this is a conceptional design error at my code. I use my variable x >> within a deeper framewok call and this call runs >> x.getclass.getDecalredFields(), but >> this returns in the example case nothing, ebcause my child class does >> not have any fields. >> >> I have got only the x reference but I need all fields of the object, >> also the fields of the child & parent >> class and fields of the parent-parent class... I would like to create a >> field list from my object x over all >> fields that are exists within the objects. IMHO I must run from my >> object class up to the java.object and collect all fields. >> How can I do this? >> > <https://github.com/ronmamo/reflections> > ReflectionUtils.getAllFields(...) > get all fields of given type, > up the super class hierarchy, > optionally filtered by predicates It may be a very useful library that can do a lot of interesting stuff, but an external library seems a bit overkill for something as simple as this. Arne
[toc] | [prev] | [next] | [standalone]
| From | Kevin McMurtrie <mcmurtrie@pixelmemory.us> |
|---|---|
| Date | 2015-02-02 19:46 -0800 |
| Message-ID | <mcmurtrie-DDAA5C.19455902022015@news.sonic.net> |
| In reply to | #30980 |
In article <mal3bb$20i5$1@ariadne.rz.tu-clausthal.de>,
Philipp Kraus <philipp.kraus@flashpixx.de> wrote:
> On 2015-02-01 06:47:00 +0000, Kevin McMurtrie said:
>
> > In article <maj5b2$1fos$1@ariadne.rz.tu-clausthal.de>,
> > Philipp Kraus <philipp.kraus@flashpixx.de> wrote:
> >
> >> Hello,
> >>
> >> I have got this class structure:
> >>
> >> clas parent
> >> {
> >> int value = 5;
> >> }
> >>
> >> class child extends parent
> >> {
> >>
> >> public doit()
> >> {
> >> Object x = this.super; **
> >> }
> >>
> >> }
> >>
> >> I would like to get at ** a reference to the super object of my class,
> >> I need a reference to the full object not to particular methods and
> >> properties.
> >> How can I do this?
> >>
> >> Thanks a lot
> >>
> >> Phil
> >
> >
> > You have two classes but only one instance object. There is no parent
> > instance.
> >
> > For proper design, a child class must not contradict the declared API of
> > the parent class. There's refactoring needed if it does. For example,
> > LinkedHashMap does not behave in any way that contradicts the declared
> > API of HashMap.
> >
> > Maybe you're thinking of inner/outer classes? An outer class is
> > OuterClassName.this.
>
> I see this is a conceptional design error at my code. I use my variable x
> within a deeper framewok call and this call runs
> x.getclass.getDecalredFields(), but
> this returns in the example case nothing, ebcause my child class does
> not have any fields.
>
> I have got only the x reference but I need all fields of the object,
> also the fields of the child & parent
> class and fields of the parent-parent class... I would like to create a
> field list from my object x over all
> fields that are exists within the objects. IMHO I must run from my
> object class up to the java.object and collect all fields.
> How can I do this?
>
> Phil
Use getFields() for use within a subclass to self-inspect.
The inspectors like getMethods() return references that don't work for
public methods of a protected class. For that, call
getDeclaredMethods() on the public class/interface that originally
declared them. This is commonly a problem with anonymous inner classes
implementing a public interface. The result of
someAnonObject.getClass().getMethods() is of little use.
--
I will not see posts from astraweb, theremailer, dizum, or google
because they host Usenet flooders.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2015-02-05 04:37 -0800 |
| Message-ID | <gqo6dadffmh5kngqpsta1bcpl63thti3kq@4ax.com> |
| In reply to | #30964 |
On Sat, 31 Jan 2015 19:02:14 +0100, Philipp Kraus
<philipp.kraus@flashpixx.de> wrote, quoted or indirectly quoted
someone who said :
>Hello,
>
>I have got this class structure:
>
>clas parent
>{
> int value = 5;
>}
>
>class child extends parent
>{
>
> public doit()
> {
> Object x = this.super; **
> }
>
>}
>
>I would like to get at ** a reference to the super object of my class,
>I need a reference to the full object not to particular methods and
>properties.
>How can I do this?
>
>Thanks a lot
>
>Phil
When you have a child object, in a single chunk of RAM with both child
and parent fields. So for all practical purposes is already is a
parent object. If you want a stripped down object, you need to build
in field by field. I asked Sun a long time about ways to make this
efficient, but they said I was the only one interested.
I wanted to efficiently convert Dog -> Dalmatian and Dalmatians -> Dog
objects because I only exported the "Dog" fields to the outside world.
--
Roedy Green Canadian Mind Products http://mindprod.com
To solve an interesting problem, start by finding a problem
that is interesting to you.
~ Eric S. Raymond (born: 1957-12-04 age: 57)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web