Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package zad2;
- import java.lang.reflect.Array;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationTargetException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Kamil
- * @param <T>
- */
- public class GenericSample<T> {
- Class t,t2;
- public GenericSample(Class<T> c) {
- t=c;
- t2=this.getClass();
- }
- public T getTNewInstance(){
- try { //Class c = Class.forName(this.getClass().getTypeParameters()[0].getTypeName());
- return (T) t.newInstance();
- } catch (InstantiationException | IllegalAccessException ex) {
- Logger.getLogger(GenericSample.class.getName()).log(Level.SEVERE, null, ex);
- }
- return null;
- }
- public T[] getTArray(int size){
- return (T[]) Array.newInstance(t, size);
- }
- public GenericSample<T> getGSTNewInstance(){
- try {
- return (GenericSample<T>) t2.getConstructor(Class.class).newInstance(t);
- } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
- Logger.getLogger(GenericSample.class.getName()).log(Level.SEVERE, null, ex);
- }
- return null;
- }
- public GenericSample<T>[] getGSTArray(int size){
- return (GenericSample<T>[]) Array.newInstance(t2, size);
- }
- @Override
- public String toString() {
- return "GS<"+t.getSimpleName()+">@"+this.hashCode();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement