Jump to content
Science Forums

Java Language


Recommended Posts

I wanted to start a general Java thread to talk about all things Java. There's one for C++, but I have yet to read/write and compute in the C/C++ language fluently.

 

To start, I have something of a head scratcher, which I am sure the Hypographytes will be able to answer in a heartbeat.

 

public <T> T get(String varname, T x){	
for(Field f:this.getClass().getSuperclass().getDeclaredFields()){			
	if (f.getName().equals(varname)){
			try{
				return (T) f.get(this);
			}catch(IllegalAccessException iae0){
				System.out.println("Illegal Access");
			}catch(IllegalArgumentException iae1){
				System.out.println("Illegal Argument");
			}
		}
}

for(Field f:this.getClass().getDeclaredFields()){
		if (f.getName().equals(varname)){
				try{
					return (T) f.get(this);
				}catch(IllegalAccessException iae0){
					System.out.println("Illegal Access");
				}catch(IllegalArgumentException iae1){
					System.out.println("Illegal Argument");
				}
			}
	}
return null;
}

 

The above is the function definition for these calls:

 

public class Test{

public static void main(String[] args){
	Item i = new Item();
	
	String pain = "yo mama";
	double wo = 0.0;
	
	System.out.println(i.get("name", pain));
	i.set("name", "Item");
	System.out.println(i.get("name", wo));
	}

}

 

One would expect the above would fail, but I will tell you: both calls work perfectly and return the value of a String variable named "name" in the declared Item instance. Do you know why? If so, share with the classroom because at least one of the student instructors is baffled.

 

Full code sample is available as attachments. it will compile and function as is. Ignore the warning it gives during compilation. The sample is technically type unsafe.

Link to comment
Share on other sites

that code is so pretty, i wanna strangle you ;)

 

not really, but i mean comments..... even quick code i write for db maintenance in php is commented, not super commented, but at least gives a general direction of thought...

 

one of the student instructors is baffled.

It's a bird, its a plane, get's is a templated method :eek:

Link to comment
Share on other sites

that code is so pretty, i wanna strangle you :)

 

not really, but i mean comments..... even quick code i write for db maintenance in php is commented, not super commented, but at least gives a general direction of thought...

 

I took the comments out of the posted code to reduce clutter. The zip contains full commenting for the code.

 

It's a bird, its a plane, get's is a templated method :xmas_rudolph:

 

It would look like it wouldn't it? However, it's generic code. Unlike C++ Templates, which uses macros, generics in java provide an easy implementation of RTTI.

 

/**
	get(String varname ,T x) returns the value of varname of type T contained in this object.
	<p>
	T can be any type. Typically to use this method you pass this object's field into the parameter to return it's value provided it is accessible.
	</p>
	<p>
	Example usage:<br>
	<code>
		Item i = new Item();<br>
		double x = 0.0;<br>
		i.get("dollars", x); //returns a double. if dollars is an invalid field, it will return null.<br>
	</code>
	</p>
	@param x the variable contained in this which you wish to retrieve.
	@return varname of type T reflected from x of type T.
*/

public <T> T get(String varname, T x){	

//The first for loop checks this object's Superclass for the Declared Fields/Variables within the scope of the Super. 
//Fields/Variables inherited from the Super's Super are ignored.
for(Field f:this.getClass().getSuperclass().getDeclaredFields()){			

//This if checks to see if the String name of the field is the same as the String varname passed into the method.
//If the two are the same, the method proceeds to try to return an Object representation of the field you are looking for and type cast it to the type which was passed in to the method via the x variable in the parameters. 

	if (f.getName().equals(varname)){
			try{
				return (T) f.get(this);
			}catch(IllegalAccessException iae0){
				System.out.println("Illegal Access");
			}catch(IllegalArgumentException iae1){
				System.out.println("Illegal Argument");
			}
		}
}

//The second for loop checks this object's Declared Fields for varname matches.
//Fields inherited from the Super of this object are ignored. 

for(Field f:this.getClass().getDeclaredFields()){
		if (f.getName().equals(varname)){
				try{
					return (T) f.get(this);
				}catch(IllegalAccessException iae0){
					System.out.println("Illegal Access");
				}catch(IllegalArgumentException iae1){
					System.out.println("Illegal Argument");
				}
			}
	}

//if nothing is found that matches in either this object's Super or this object, the method returns null.
return null;
}

 

I hope that helps.

Link to comment
Share on other sites

It would look like it wouldn't it? However, it's generic code.

Lol they are the same thing, templated code is generic code, how different languages achieve it is of no concern, its the same concept.

 

Its a classic generic code aka templated class declaration.

 

Let's split up the declaration onto several lines:

 

1. public <T>

2. T

3. get

4. (String varname,

5. T x)

6... { ...

 

So, line 1 designates this method as a public generic

method, with one generic type named "T". The type that

instantiates T will be figured out by the compiler for

each usage of this method in the code.

 

Line 2 indicates that the method returns an object of type

T. That simply means that whatever object type the body

code returns, it has better be T or a subclass of T.

 

Line 3 is the method name.

 

Line 4 is a parameter. In this case you are passing a string

 

Line 5 is another argument, but this one is of a generic nature which coincides with what type of a value the function will return as an end result

 

it works using a different method achieved to get there, but it's exactly the same thing as a templated class in C++... you gotta remember, Java is like 90% C++...

Link to comment
Share on other sites

Note: The type parameter, T, is grabbed from the variable x and passed into the method at runtime--I believe--when it's called.

 

If you are familiar with C++'s template mechanism, you might think that generics are similar, but the similarity is superficial. Generics do not generate a new class for each specialization, nor do they permit “template metaprogramming.

 

You may be familiar with similar constructs from other languages, most notably C++ templates. If so, you’ll soon see that there are both similarities and important differences. If you are not familiar with look-a-alike constructs from elsewhere, all the better; you can start afresh, without unlearning any misconceptions.

 

While reading through to find the quotes I found a section which explains what's going on in my code. ^^

 

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf]7.1[/url] A Generic Class is Shared by all its Invocations

What does the following code fragment print?

List <String> l1 = new ArrayList<String>();
List<Integer> l2 = new ArrayList<Integer>();
System.out.println(l1.getClass() == l2.getClass());

You might be tempted to say false, but you’d be wrong. It prints true, because all instances of a generic class have the same run-time class, regardless of their actual type parameters.

Indeed, what makes a class generic is the fact that it has the same behavior for all of its possible type parameters; the same class can be viewed as having many different types.

As consequence, the static variables and methods of a class are also shared among all the instances. That is why it is illegal to refer to the type parameters of a type declaration in a static method or initializer, or in the declaration or initializer of a static variable.

 

7.2 Casts and InstanceOf

Another implication of the fact that a generic class is shared among all its instances,

is that it usually makes no sense to ask an instance if it is an instance of a particular

invocation of a generic type:

Collection cs = new ArrayList<String>();
if (cs instanceof Collection<String>) { ...} // illegal

similarly, a cast such as

Collection<String> cstr = (Collection<String>) cs; // unchecked warning

gives an unchecked warning, since this isn’t something the run time system is going

to check for you.

The same is true of type variables

<T> T badCast(T t, Object o) {return (T) o; // unchecked warning
}

 

How do you suppose I could fix my bad cast?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...