summaryrefslogtreecommitdiff
path: root/sem_5/HLIN505_Java/HLIN505/src/tp7/Ex1.java
blob: 1a6116d007851cf73a5cdb2eb869b0cae139f4ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package tp7;

import java.util.ArrayList;

import javax.swing.JTextField;

import java.io.File;
import java.lang.reflect.*;

public class Ex1 {
	private ArrayList Liste;
	public Ex1 (ArrayList a) {
		Liste=a;
	}
	public Method[] methodesdei (int i) {
		Class cl = Liste.get(i).getClass();
		Method[] m = cl.getMethods();
		return m;
	}
	public Class superclasse() {
		Class ret;
		ArrayList<Class> cl= new ArrayList<Class>();
		for (int i = 0; i < Liste.size(); i++) {
			cl.add(Liste.get(i).getClass());
		}
		ArrayList<Class> cl2 = new ArrayList<Class>();
		while (cl2.add(cl.get(0).getSuperclass()));
		Object tmp2=new Object();
		ret = tmp2.getClass(); 
		for (Class tmp : cl2) {
			Boolean fornow = true;
		for (int i = 1; i < Liste.size(); i ++) {
				if (!tmp.isInstance(Liste.get(i))){
					fornow = false;
				}
			}
		if (fornow) {
			ret = tmp;
			break;
		}
		}
		return ret;
	}
	
	public <T> void add (T obj) {
		Liste.add(obj);
	}
	
	public static void main (String[] args) {
		ArrayList<Object> list = new ArrayList<Object>();
		list.add(new Integer(12));
		list.add(new String("allo"));
		list.add(new Double(12.2311));
		list.add(new File("."));
		list.add(new JTextField());
		Ex1 test = new Ex1(list);
		System.out.println(test.superclasse());
	}
}