KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > Imports


1 /*
2   Copyright (C) 2004 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23 import org.apache.log4j.Logger;
24 import org.objectweb.jac.core.rtti.ClassItem;
25 import org.objectweb.jac.core.rtti.ClassRepository;
26 import org.objectweb.jac.core.rtti.NoSuchClassException;
27
28 public class Imports {
29     Logger logger = Logger.getLogger("imports");
30     Vector JavaDoc imports = new Vector JavaDoc();
31     public void add(String JavaDoc imp) {
32         imports.add(imp);
33     }
34     public ClassItem getClass(String JavaDoc className) {
35         logger.debug("Searching for "+className);
36         Iterator JavaDoc i = imports.iterator();
37         ClassRepository cr = ClassRepository.get();
38         ClassItem cli = null;
39         try {
40             cli = cr.getNonPrimitiveClass(className);
41             logger.debug(" Found "+className+": "+cli.getName());
42         } catch (NoSuchClassException e) {
43             while (i.hasNext()) {
44                 String JavaDoc imp = (String JavaDoc)i.next();
45                 try {
46                     if (imp.endsWith("*")) {
47                         cli = cr.getClass(
48                             imp.substring(0,imp.length()-1)+className);
49                     } else {
50                         int index = imp.lastIndexOf('.');
51                         if (imp.substring(index+1).equals(className)) {
52                             cli = cr.getClass(imp);
53                         }
54                     }
55                     if (cli!=null) {
56                         logger.debug(" Found "+cli.getName());
57                         break;
58                     }
59                 } catch(NoSuchClassException e2) {
60                 }
61             }
62         }
63         if (cli==null) {
64             logger.debug(" Not found in "+imports);
65             throw new NoSuchClassException(className);
66         }
67         return cli;
68     }
69 }
70
Popular Tags