KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > convertor > InstanceUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.convertor;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Properties JavaDoc;
25 import org.netbeans.api.convertor.ConvertorException;
26 import org.openide.ErrorManager;
27 import org.openide.util.Lookup;
28 import org.openide.util.Utilities;
29
30 /**
31  *
32  * @author David Konecny
33  */

34 public class InstanceUtils {
35
36     private InstanceUtils() {
37     }
38
39     public static Object JavaDoc newValue(String JavaDoc val, Properties JavaDoc properties) {
40         val = Utilities.translate(val);
41         try {
42             Class JavaDoc c = findClass(val);
43             if (properties == null) {
44                 return c.newInstance();
45             } else {
46                 Constructor JavaDoc con = c.getConstructor(new Class JavaDoc[]{Properties JavaDoc.class});
47                 return con.newInstance(new Object JavaDoc[]{properties});
48             }
49         } catch (Exception JavaDoc e) {
50             ConvertorException e2 = new ConvertorException("Cannot instantiate class "+val); // NOI18N
51
ErrorManager.getDefault().annotate(e2, e);
52             throw e2;
53         }
54     }
55     
56     public static Object JavaDoc methodValue(String JavaDoc val, Properties JavaDoc properties) {
57         int sepIdx = val.lastIndexOf('.');
58         if (sepIdx == -1) {
59             throw new ConvertorException("Cannot call method "+val+", because it is not fully qualified."); // NOI18N
60
}
61         
62         String JavaDoc methodName = val.substring(sepIdx+1);
63         String JavaDoc className = val.substring(0,sepIdx);
64         
65         try {
66             Class JavaDoc cls = findClass(className);
67             if (properties == null) {
68                 Method JavaDoc method = cls.getMethod(methodName, new Class JavaDoc[]{});
69                 return method.invoke(null, new Object JavaDoc[]{});
70             } else {
71                 Method JavaDoc method = cls.getMethod(methodName, new Class JavaDoc[]{Properties JavaDoc.class});
72                 return method.invoke(null, new Object JavaDoc[]{properties});
73             }
74         } catch (Exception JavaDoc e) {
75             ConvertorException e2 = new ConvertorException("Cannot instantiate method "+val); // NOI18N
76
ErrorManager.getDefault().annotate(e2, e);
77             throw e2;
78         }
79     }
80
81     public static Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
82         ClassLoader JavaDoc c = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
83         if (c == null) {
84             return Class.forName(name, true, null);
85         } else {
86             return Class.forName(name, true, c);
87         }
88     }
89
90     
91 }
92
Popular Tags