Generics: How to implement custom generic List?

Hello,

As we know, we can use follow:

 list = new ArrayList;
E e = new E();
list.add(e);
E e = list.get(0);
]]>

I want to create my List for similar purpose:

 genericList = new GenericList();
genericList.add("Hello Word!");
]]>

I implement sample test case for this, but i have SecurityException:


Does anybody know some expertise about generics lists?

Thanks!

--
Alexey Efimov, Software Engineer
Sputnik Labs,
http://www.spklabs.com



Attachment(s):
intellijTestCases.zip
0
11 comments
Avatar
Permanently deleted user

Ops, forums gateway to NNTP does not attach Jive Attachments, so this test
cases here:

http://www.intellij.net/servlet/JiveServlet/download/22-36779-521010-1753/intellijTestCases.zip

Thanks!

--
Alexey Efimov, Software Engineer
Sputnik Labs,
http://www.spklabs.com
"Alexey Efimov" <aefimov@spklabs.com> wrote in message
news:7546368.1059575863237.JavaMail.javamailuser@localhost...

Hello,

>

As we know, we can use follow:

 List list = new ArrayList;
> E e = new E();
> list.add(e);
> E e = list.get(0);
> ]]>

I want to create my List for similar purpose:

 GenericList genericList = new GenericList();
> genericList.add("Hello Word!");
> ]]>

I implement sample test case for this, but i have SecurityException:
java.lang.SecurityException: Prohibited package name: java.lang > at java.lang.ClassLoader.defineClass(ClassLoader.java:524) > at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123) > at java.net.URLClassLoader.defineClass(URLClassLoader.java:251) > at java.net.URLClassLoader.access$100(URLClassLoader.java:55) > at java.net.URLClassLoader$1.run(URLClassLoader.java:194) > at java.security.AccessController.doPrivileged(Native Method) > at java.net.URLClassLoader.findClass(URLClassLoader.java:187) > at java.lang.ClassLoader.loadClass(ClassLoader.java:289) > at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274) > at java.lang.ClassLoader.loadClass(ClassLoader.java:235) > at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302) > at test.GenericTestCase.testGenericWrapperInGenericList(GenericTestCase.java:15 ) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) > at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:25) > at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:29) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39 ) > at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:25) > at com.intellij.rt.execution.application.AppMain.main(AppMain.java:75) > ]]>

>

Does anybody know some expertise about generics lists?

>

Thanks!

>

--
Alexey Efimov, Software Engineer
Sputnik Labs,
http://www.spklabs.com




0
Avatar
Permanently deleted user

Alexey Efimov wrote:

I implement sample test case for this, but i have SecurityException:
java.lang.SecurityException: Prohibited package name: java.lang > ]]>


Have you placed your implementation in java.lang? Why?

This probably doesn't have anything to do with generics -- it sounds
like you should just move your class to an ordinary package instead.

0
Avatar
Permanently deleted user

Jonas Kvarnström wrote:

Alexey Efimov wrote:

>> I implement sample test case for this, but i have SecurityException:
>> > java.lang.SecurityException: Prohibited package name: java.lang > > > ]]>


Have you placed your implementation in java.lang? Why?

This probably doesn't have anything to do with generics -- it sounds
like you should just move your class to an ordinary package instead.


Sorry, ignore that. I saw you attachment now and you didn't place them
in java.lang.

Your program is run as a test case. I don't use JUnit myself, but
apparently it adds a security manager. This security manager forbids
you from loading classes in java.lang except those classes that are
loaded locally (those that are loaded from your bootclasspath, I think).

IDEA places collect.jar (with new replacement classes for java.lang.*)
in your ordinary class path, where the security manager refuses to load
them.

If you run your program from an ordinary main() method, it will most
likely work correctly. I don't know the correct workaround to get
collect.jar into the bootclasspath instead of the classpath for JUnit tests.

BTW, you might want to upgrade to generics 2.2 where variance (such as
E[=] arrays) has been removed, so you don't learn to rely on them...

0
Avatar
Permanently deleted user

Thank you very much,

I downloaded generics, now it say that:

at code:


Is it normal?

I see at source of ArrayList and in this code array allocation perform by:


Do you know some think about it?

Thanks!

--
Alexey Efimov, Software Engineer
Sputnik Labs,
http://www.spklabs.com
"Jonas Kvarnstr?m" <jonkv@ida.liu.se> wrote in message
news:bg8mtg$rq1$1@is.intellij.net...

Jonas Kvarnstr?m wrote:

>

Alexey Efimov wrote:

>
>> I implement sample test case for this, but i have SecurityException:
>> >> java.lang.SecurityException: Prohibited package name: java.lang > > > > > ]]>
>

Have you placed your implementation in java.lang? Why?

>

This probably doesn't have anything to do with generics -- it sounds
like you should just move your class to an ordinary package instead.

>

Sorry, ignore that. I saw you attachment now and you didn't place them
in java.lang.

>

Your program is run as a test case. I don't use JUnit myself, but
apparently it adds a security manager. This security manager forbids
you from loading classes in java.lang except those classes that are
loaded locally (those that are loaded from your bootclasspath, I think).

>

IDEA places collect.jar (with new replacement classes for java.lang.*)
in your ordinary class path, where the security manager refuses to load
them.

>

If you run your program from an ordinary main() method, it will most
likely work correctly. I don't know the correct workaround to get
collect.jar into the bootclasspath instead of the classpath for JUnit

tests.
>

BTW, you might want to upgrade to generics 2.2 where variance (such as
E[=] arrays) has been removed, so you don't learn to rely on them...

>


0
Avatar
Permanently deleted user

About class loader for java.lang...

Why one of test is work ok?
This test is work even from JUnit:

 genericWrapper = new
GenericWrapper("Hello Word!");
    List list = new ArrayList();
    list.add(genericWrapper);
    // Recreate
    genericWrapper = new GenericWrapper("Hello Word!");
    // Check contains
    assertTrue(list.contains(genericWrapper));
  }
]]>


Thanks!

--
Alexey Efimov, Software Engineer
Sputnik Labs,
http://www.spklabs.com


0
Avatar
Permanently deleted user

Alexey Efimov wrote:

Thank you very much,

I downloaded generics, now it say that:
error: generic array creation > ]]>
at code:

 E[] buffer = new E[items.length + increaseCount];
> ]]>


Is it normal?


Yes. Apparently too many people thought variance was confusing (I
thought it was a perfectly clear concept although I admit I never had a
chance to use it before it was removed).

Unfortunately plain arrays (such as E[] and List[]) arrays have a
problem with covariance (I think that's what it's called). You can have
code such as this:


The code looks fine but despite the fact that the compiler will accept
it you'll get an ArrayStoreException (or whatever it's called) when you
run the code. Normally you only get this kind of problem when you
explicitly cast an object to another class.

Variance was one way of solving this, but since variance was removed and
since they didn't want to have this type structure problem for generic
types, they simply removed a lot of functionality, like the ability to
have arrays of generic classes (no List]]>[] anymore), and the
ability to create arrays of generic parameter types (no new E[10] anymore).

What you have to do is to use (E[]) new Object[...] instead, as you saw
in ArrayList. You lose some runtime type checking but that seems to be
the only way you can use arrays in the new version of generics.

(Please correct me if I'm wrong somewhere -- I'm in a hurry right now...)

0
Avatar
Permanently deleted user

I can't clearly understand you, can you please describe more...
And this code work fine:


Thanks!

0
Avatar
Permanently deleted user

Instead of:

List[] listArray = new List[100];



List[] listArray = new ArrayList[100];

listArray[0] = new ArrayList();
listArray[1] = new LinkedList();



It will break at runtime with a ClassCastException.


0
Avatar
Permanently deleted user

you can try running the unit tests with ant script
instead of IDEA (see ant junit task).

I experienced strange exceptions with UnitTests
from IDEA, that worked perfectly from ant task
- but the problem I had with IDEA 3.0 disappeared with
the new EAP versions.
But this shows that some strange things CAN happen,
probably regarding class loading, when running unit
tests via IDEA.

0
Avatar
Permanently deleted user

Carlos,
Not a ClassCast, but ArrayStoreException will thrown in this case.

But it is notmal becose LinkedList is not relative to super class to ArrayList.
You can not cast LinkedList to ArrayList.

But if you define array of List, you now can use in elements all implementations of List

Thanks!

0
Avatar
Permanently deleted user

Alexey Efimov wrote:

Carlos,
Not a ClassCast, but ArrayStoreException will thrown in this case.

But it is notmal becose LinkedList is not relative to super class to ArrayList.
You can not cast LinkedList to ArrayList.


The point is that this cannot be detected at compile time, only at
runtime, despite the fact that you are not using any casts.

In all other cases, a program without explicit cast operators is
typesafe. It cannot result in a ClassCastException because the type
system is sound and it was already proven at compile time (and when the
class was loaded) that types satisfy the required constraints, such as
bar being assignable to foo if you have "foo = bar;" in your program:


Once explicit casts are involved, you can get ClassCastExceptions -- but
you can see this just by glancing at your program because there is
an explicit cast and you know such a cast can fail.


But once arrays are involved, there are no such guarantees. You can
have a program with no explicit casts and there is no way the type
analysis system can determine whether the program is correct or not.


0

Please sign in to leave a comment.