|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DefaultMetaClassAccessor.java | 80% | 91.7% | 100% | 89.2% |
|
1 |
/*
|
|
2 |
* Copyright (C) The MetaClass Group. All rights reserved.
|
|
3 |
*
|
|
4 |
* This software is published under the terms of the Spice
|
|
5 |
* Software License version 1.1, a copy of which has been included
|
|
6 |
* with this distribution in the LICENSE.txt file.
|
|
7 |
*/
|
|
8 |
package org.codehaus.metaclass.introspector;
|
|
9 |
|
|
10 |
import java.io.File;
|
|
11 |
import java.io.InputStream;
|
|
12 |
import org.codehaus.metaclass.io.MetaClassIO;
|
|
13 |
import org.codehaus.metaclass.io.MetaClassIOASM;
|
|
14 |
import org.codehaus.metaclass.io.MetaClassIOBinary;
|
|
15 |
import org.codehaus.metaclass.io.MetaClassIOXml;
|
|
16 |
import org.codehaus.metaclass.model.ClassDescriptor;
|
|
17 |
|
|
18 |
/**
|
|
19 |
* This is the default mechanism for loading ClassDescriptor objects. This class
|
|
20 |
* follows the following steps to locate the ClassDescriptor.
|
|
21 |
*
|
|
22 |
* <ul> <li>Look for a file with extension "-meta.binary" sitting side-by-side
|
|
23 |
* the class file. ie If class is named com.biz.Foo it has a class file named
|
|
24 |
* <tt>com/biz/Foo.class</tt> and an descriptor file named
|
|
25 |
* <tt>com/biz/Foo-meta.binary</tt>. Assume the descriptor is in binary
|
|
26 |
* format.</li> <li>Look for a file with extension "-meta.xml" sitting
|
|
27 |
* side-by-side the class file. ie If class is named com.biz.Foo it has a class
|
|
28 |
* file named <tt>com/biz/Foo.class</tt> and an descriptor file named
|
|
29 |
* <tt>com/biz/Foo-meta.xml</tt>. Assume the descriptor is in xml format.</li>
|
|
30 |
* <li>If unable to locate descriptor throw a <tt>MetaClassException</tt>.</li>
|
|
31 |
* </ul>
|
|
32 |
*
|
|
33 |
* @author Peter Donald
|
|
34 |
* @version $Revision: 1.12 $ $Date: 2004/01/16 00:55:41 $
|
|
35 |
*/
|
|
36 |
public class DefaultMetaClassAccessor |
|
37 |
implements MetaClassAccessor
|
|
38 |
{ |
|
39 |
/** Class based IO. May be null if the ASM toolkit is not in classloader. */
|
|
40 |
private static final MetaClassIO ASM_IO = getMetaClassIOASM(); |
|
41 |
|
|
42 |
/**
|
|
43 |
* @see MetaClassAccessor#getClassDescriptor
|
|
44 |
*/
|
|
45 |
public ClassDescriptor
|
|
46 | 10 |
getClassDescriptor( final String classname, |
47 |
final ClassLoader classLoader, |
|
48 |
final MetaClassAccessor accessor ) |
|
49 |
throws MetaClassException
|
|
50 |
{ |
|
51 | 10 |
ClassDescriptor descriptor; |
52 |
|
|
53 | 10 |
descriptor = loadClassDescriptor( MetaClassIOXml.IO, |
54 |
classname, |
|
55 |
classLoader ); |
|
56 | 10 |
if( null != descriptor ) |
57 |
{ |
|
58 | 2 |
return descriptor;
|
59 |
} |
|
60 | 8 |
descriptor = loadClassDescriptor( MetaClassIOBinary.IO, |
61 |
classname, |
|
62 |
classLoader ); |
|
63 | 6 |
if( null != descriptor ) |
64 |
{ |
|
65 | 2 |
return descriptor;
|
66 |
} |
|
67 |
|
|
68 | 4 |
if( null != ASM_IO ) |
69 |
{ |
|
70 | 4 |
descriptor = loadClassDescriptor( ASM_IO, |
71 |
classname, |
|
72 |
classLoader ); |
|
73 | 4 |
if( null != descriptor ) |
74 |
{ |
|
75 | 0 |
return descriptor;
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 | 4 |
final String message = |
80 |
"Missing Attributes for " + classname;
|
|
81 | 4 |
throw new MetaClassException( message ); |
82 |
} |
|
83 |
|
|
84 |
/**
|
|
85 |
* Attempt to load ClassDescriptor using specified MetaClassIOXml.
|
|
86 |
*
|
|
87 |
* @param io the MetaClassIOXml
|
|
88 |
* @param classname the classname of class
|
|
89 |
* @param classLoader the classloader to load resources from
|
|
90 |
* @return the ClassDescriptor if descriptor is found, else null
|
|
91 |
* @throws MetaClassException if error loading descriptor
|
|
92 |
*/
|
|
93 | 22 |
private ClassDescriptor loadClassDescriptor( final MetaClassIO io,
|
94 |
final String classname, |
|
95 |
final ClassLoader classLoader ) |
|
96 |
throws MetaClassException
|
|
97 |
{ |
|
98 | 22 |
final String resourceName = |
99 |
io.getResourceName( classname ). |
|
100 |
replace( File.separatorChar, '/' ); |
|
101 | 22 |
final InputStream inputStream = |
102 |
classLoader.getResourceAsStream( resourceName ); |
|
103 | 22 |
if( null == inputStream ) |
104 |
{ |
|
105 | 16 |
return null; |
106 |
} |
|
107 | 6 |
try
|
108 |
{ |
|
109 | 6 |
return io.deserializeClass( inputStream );
|
110 |
} |
|
111 |
catch( final Exception e )
|
|
112 |
{ |
|
113 | 2 |
final String message = |
114 |
"Unable to load Attributes for " + classname;
|
|
115 | 2 |
throw new MetaClassException( message, e ); |
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
/**
|
|
120 |
* Return ASM IO if it can be created (and thus ASM toolkit in
|
|
121 |
* classloader).
|
|
122 |
*/
|
|
123 | 8 |
private static MetaClassIO getMetaClassIOASM() |
124 |
{ |
|
125 | 8 |
try
|
126 |
{ |
|
127 | 8 |
return MetaClassIOASM.IO;
|
128 |
} |
|
129 |
catch( final Exception e )
|
|
130 |
{ |
|
131 |
//ie ClassNotFoundException
|
|
132 | 0 |
return null; |
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
} |
|
137 |
|
|