Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Allocator: Jouons avec les generics de java 1.5

1 réponse
Avatar
Nicolas Repiquet
Bonjour à tous.

Je fait en ce moment mumuse avec les nouvelles fonctionnalités de java 1.5.

Voici un des résultat de mes bidouillages.


public interface Allocator<T> {
public T newInstance();
}

public class ClosureAllocator<T> implements Allocator<T> {

private Constructor m_constructor< ? extends T >;
private Object[] m_args;

public ClosureAllocator<T>( Class< ? extends T > clazz, Object ...
args ) {

assert( clazz != null );

Class[] types;

m_args = args.clone();

types = new Class[m_args.length];

for( int i = 0 ; i < m_args.length ; i++ ) {
types[i] = m_args[i].getClass();
}

try {

m_constructor = clazz.getConstructor( types );

assert( Modifier.isPublic(m_constructor.getModifier()) ) :
m_constructor + " is not public.";

} catch( Exception e ) {

assert( false ) : clazz + " has no such a constructor." ;
}
}

public T newInstance() {

try {

return m_constructor.newInstance( m_args );

} catch( Exception e ) {

assert( false ) : e;

}
}
}


// un petit test maintenant

public class AllocatorTest {

public class Test {}
public class Test2 extends Test {}
public class Test3 extends Test {
private String m_string;
public Test3( String s ) {
m_string = s;
}
public String toString() {
return m_string;
}
}

public static void out( Allocator<Test> allocator ) {
for( int i = 0 ; i < 3 ; i++ ) {
System.out.println( allocator.newInstance() );
}
}

public static void main( String[] args ) {

out( new ClosureAllocator<Test>( Test.class ));
out( new ClosureAllocator<Test>( Test2.class ));
out( new ClosureAllocator<Test>( Test3.class, "coucou" ));

}

}

// sortie

AllocatorTest$Test@130c19b
AllocatorTest$Test@1f6a7b9
AllocatorTest$Test@7d772e
AllocatorTest$Test2@35ce36
AllocatorTest$Test2@757aef
AllocatorTest$Test2@d9f9c3
coucou
coucou
coucou

Bon ben voilà c'est tout... enfin ça va me faciliter la factory je crois!

Moi ça me plait bien en tout cas =). Dommage que l'interface Allocator ne
fasse pas partie de l'API ( ou alors je l'ai pas vue ).

-- Nicolas Repiquet

1 réponse

Avatar
ZebX
...Log du <06/07/2004 00:27> ...Post de <Nicolas Repiquet>
:
Salut Nicolas,

public interface Allocator<T> {
public T newInstance();
}

public class ClosureAllocator<T> implements Allocator<T> {

private Constructor m_constructor< ? extends T >;
private Object[] m_args;

public ClosureAllocator<T>( Class< ? extends T > clazz, Object ...
args ) {

assert( clazz != null );
...

assert( Modifier.isPublic(m_constructor.getModifier()) ) :
m_constructor + " is not public.";

...


out( new ClosureAllocator<Test>( Test.class ));
...

Moi ça me plait bien en tout cas =).


Ouaip. Je suis plus réservé.

Ca n'apporte pas grand chose.
C'est moche ! On dirait du C++, c'est pour dire ! ;)

Je ne comprends pas que Sun est pourri son language :(

Dans la version 1.6, le retour de l'assembleur ?
Et dans la v1.7, on code en hexa.
...dans la v1.8, ah non, à ce train Java sera mort :)))

--
ZebX - No comprendo

Pour ma part, je suis hétérosexuel. Mais il faut le reconnaître, le
bisexuel a deux fois plus de chances le samedi soir. -- Woody Allen.