Added: commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelMethod.java URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelMethod.java?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelMethod.java (added) +++ commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelMethod.java Thu Jul 5 02:48:34 2012 @@ -0,0 +1,105 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.classscan.bcel; + +import java.util.Set; + +import org.apache.bcel.classfile.Method; +import org.apache.bcel.classfile.ParameterAnnotationEntry; +import org.apache.bcel.generic.Type; +import org.apache.commons.classscan.model.MetaAnnotation; +import org.apache.commons.classscan.model.MetaParameter; +import org.apache.commons.classscan.model.MetaType; +import org.apache.commons.classscan.spi.model.SpiMetaClassLoader; +import org.apache.commons.classscan.spi.model.SpiMetaMethod; +import org.apache.commons.classscan.spi.model.SpiMetaParameter; +import org.apache.commons.classscan.util.ResolveSet; + +public class BcelMethod implements SpiMetaMethod { + + private final String methodName; + private final boolean isStatic; + private final AnnotationMap annotations; + private MetaType returnType; + private String returnTypeDescriptor; + private final ResolveSet<SpiMetaParameter> paramTypes; + + public BcelMethod(Method method) { + methodName = method.getName(); + isStatic = method.isStatic(); + annotations = AnnotationMap.createAnnotations(method.getAnnotationEntries()); + returnTypeDescriptor = method.getReturnType().getSignature(); + paramTypes = createParameters(method); + } + + @Override + public boolean resolve(SpiMetaClassLoader classLoader) { + returnType = classLoader.resolveTypeForDescriptor(returnTypeDescriptor); + if(returnType==null) { + return false; + } + returnTypeDescriptor = null; + + return annotations.resolve(classLoader) + && paramTypes.resolve(classLoader); + } + + @Override + public String getName() { + return methodName; + } + + @Override + public Set<? extends MetaAnnotation> getAnnotations() { + return annotations; + } + + @Override + public MetaAnnotation getAnnotation(String annotationName) { + return annotations.getValue(annotationName); + } + + @Override + public boolean isStatic() { + return isStatic; + } + + @Override + public MetaType getType() { + return returnType; + } + + @Override + public Set<? extends MetaParameter> getParameters() { + return paramTypes; + } + + private ResolveSet<SpiMetaParameter> createParameters(Method method) { + final Type[] types = method.getArgumentTypes(); + if (types.length == 0) { + return ResolveSet.emptyResolveSet(); + } + + final ParameterAnnotationEntry[] parameterAnnotations = method.getParameterAnnotationEntries(); + SpiMetaParameter[] metaParameterArray = new SpiMetaParameter[types.length]; + for (int i = 0; i < metaParameterArray.length; ++i) { + // there may be less annotations than parameters + ParameterAnnotationEntry arg = i < parameterAnnotations.length ? parameterAnnotations[i] : null; + metaParameterArray[i] = new BcelParameter(types[i], arg); + } + + ResolveSet<SpiMetaParameter> parameters = new ResolveSet<SpiMetaParameter>(metaParameterArray); + return parameters; + } +}
Added: commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelParameter.java URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelParameter.java?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelParameter.java (added) +++ commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelParameter.java Thu Jul 5 02:48:34 2012 @@ -0,0 +1,68 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.classscan.bcel; + +import java.util.Set; + +import org.apache.bcel.classfile.ParameterAnnotationEntry; +import org.apache.bcel.generic.Type; +import org.apache.commons.classscan.model.MetaAnnotation; +import org.apache.commons.classscan.model.MetaType; +import org.apache.commons.classscan.spi.model.SpiMetaAnnotation; +import org.apache.commons.classscan.spi.model.SpiMetaClassLoader; +import org.apache.commons.classscan.spi.model.SpiMetaParameter; +import org.apache.commons.classscan.util.NameSet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BcelParameter implements SpiMetaParameter { + + private static final Logger logger = LoggerFactory.getLogger(BcelParameter.class); + + private MetaType type; + private String typeName; + private final NameSet<? extends SpiMetaAnnotation> annotations; + + public BcelParameter(Type argType, ParameterAnnotationEntry annotationEntry) { + typeName = argType.getSignature(); + annotations = AnnotationMap.createAnnotations(annotationEntry != null + ?annotationEntry.getAnnotationEntries() :null); + } + + @Override + public boolean resolve(SpiMetaClassLoader classLoader) { + type = classLoader.resolveTypeForDescriptor(typeName); + if(type==null) { + logger.info("cannot resolve "+typeName+" as a parameter type"); + return false; + } + typeName= null; + return annotations.resolve(classLoader); + } + + @Override + public Set<? extends MetaAnnotation> getAnnotations() { + return annotations; + } + + @Override + public MetaAnnotation getAnnotation(String annotationName) { + return annotations.getValue(annotationName); + } + + @Override + public MetaType getType() { + return type; + } +} Added: commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelProperty.java URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelProperty.java?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelProperty.java (added) +++ commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/BcelProperty.java Thu Jul 5 02:48:34 2012 @@ -0,0 +1,142 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.classscan.bcel; + +import org.apache.bcel.classfile.AnnotationElementValue; +import org.apache.bcel.classfile.AnnotationEntry; +import org.apache.bcel.classfile.ArrayElementValue; +import org.apache.bcel.classfile.ClassElementValue; +import org.apache.bcel.classfile.ElementValue; +import org.apache.bcel.classfile.ElementValuePair; +import org.apache.bcel.classfile.EnumElementValue; +import org.apache.bcel.classfile.SimpleElementValue; +import org.apache.commons.classscan.builtin.ClassNameHelper; +import org.apache.commons.classscan.model.MetaClass; +import org.apache.commons.classscan.spi.model.SpiMetaAnnotation.SpiProperty; +import org.apache.commons.classscan.spi.model.SpiMetaClassLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BcelProperty implements SpiProperty { + + private static final Logger logger = LoggerFactory.getLogger(BcelProperty.class); + + private final String propertyName; + private Object value; + + public BcelProperty(ElementValuePair evPair) { + propertyName = evPair.getNameString(); + value = evPair.getValue(); + } + + @Override + public boolean resolve(SpiMetaClassLoader classLoader) { + value = createObject(classLoader, value); + return value!=null; + } + + @Override + public String getName() { + return propertyName; + } + + @Override + public Object getValue() { + return value; + } + + private Object createObject(SpiMetaClassLoader classLoader, Object ev) { + if (ev instanceof SimpleElementValue) { + return createWrapper((SimpleElementValue) ev); + } + if (ev instanceof EnumElementValue) { + return createEnum(classLoader, (EnumElementValue) ev); + } + if (ev instanceof ClassElementValue) { + return createMetaClass(classLoader, (ClassElementValue) ev); + } + if (ev instanceof ArrayElementValue) { + return createArray(classLoader, (ArrayElementValue) ev); + } + if (ev instanceof AnnotationElementValue) { + return createAnnotation((AnnotationElementValue) ev); + } + logger.info("unknown annotation value type "+ev.getClass().getCanonicalName()+" for annotation property "+propertyName); + return null; + } + + private Object createWrapper(SimpleElementValue sev) { + char primitiveTypeCode = (char) sev.getElementValueType(); + + switch (primitiveTypeCode) { + case 'B': + return sev.getValueByte(); + case 'C': + return sev.getValueChar(); + case 'D': + return sev.getValueDouble(); + case 'F': + return sev.getValueFloat(); + case 'I': + return sev.getValueInt(); + case 'J': + return sev.getValueLong(); + case 'S': + return sev.getValueShort(); + case 'Z': + return sev.getValueBoolean(); + case 's': + return sev.getValueString(); + default: + return null; + } + } + + private MetaClass createMetaClass(SpiMetaClassLoader classLoader, ClassElementValue ev) { + String internalClassName = ev.getClassString(); + String canonicalName = ClassNameHelper.internalToCanonicalName(internalClassName); + return classLoader.resolveMetaClass(canonicalName); + } + + private Object createAnnotation(AnnotationElementValue aev) { + AnnotationEntry ae = ((AnnotationElementValue) aev).getAnnotationEntry(); + return new BcelAnnotation(ae); + } + + private Object createEnum(SpiMetaClassLoader classLoader, EnumElementValue eev) { + String enumTypeString = ClassNameHelper.internalToCanonicalName(eev.getEnumTypeString()); + MetaClass dmc = classLoader.resolveMetaClass(enumTypeString); + if(dmc==null) { + logger.info("unknown enum type "+enumTypeString+" for annotation property "+propertyName); + return null; + } + String enumInstanceName = eev.getEnumValueString(); + return new BcelEnumProperty(dmc, enumInstanceName); + } + + private Object[] createArray(SpiMetaClassLoader classLoader, ArrayElementValue ev) { + ArrayElementValue aev = (ArrayElementValue) ev; + ElementValue[] elementValues = aev.getElementValuesArray(); + Object[] returnValues = new Object[elementValues.length]; + int i = 0; + for (ElementValue elementValue : elementValues) { + Object obj = createObject(classLoader, elementValue); + if(obj==null) { + return null; + } + returnValues[i++] = obj; + } + return returnValues; + } +} Added: commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/package-info.java URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/package-info.java?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/package-info.java (added) +++ commons/sandbox/classscan/trunk/bcel/src/main/java/org/apache/commons/classscan/bcel/package-info.java Thu Jul 5 02:48:34 2012 @@ -0,0 +1,20 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * <p>This package is an implementation of the {@link org.apache.commons.classscan} interfaces. + * The implementation uses the BCEL commons library to introspect class files.</p> + * <p><b>The non-public methods in these classes are subject to change in any release.</b></p> + */ +package org.apache.commons.classscan.bcel; + Added: commons/sandbox/classscan/trunk/bcel/src/main/resources/META-INF/services/org.apache.commons.classscan.spi.ClassDigesterFactory URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/main/resources/META-INF/services/org.apache.commons.classscan.spi.ClassDigesterFactory?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/main/resources/META-INF/services/org.apache.commons.classscan.spi.ClassDigesterFactory (added) +++ commons/sandbox/classscan/trunk/bcel/src/main/resources/META-INF/services/org.apache.commons.classscan.spi.ClassDigesterFactory Thu Jul 5 02:48:34 2012 @@ -0,0 +1,15 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +org.apache.commons.classscan.bcel.BcelClassDigesterFactory Added: commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/bcel/BcelClassTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/bcel/BcelClassTest.java?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/bcel/BcelClassTest.java (added) +++ commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/bcel/BcelClassTest.java Thu Jul 5 02:48:34 2012 @@ -0,0 +1,56 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.classscan.bcel; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; + +import org.apache.commons.classscan.MetaClassLoader; +import org.apache.commons.classscan.MetaRegistry; +import org.apache.commons.classscan.model.MetaClass; +import org.junit.Before; +import org.junit.Test; + +public class BcelClassTest { + + MetaClassLoader classLoader; + MetaClass thisClass; + + @Before + public void loadLocation() throws URISyntaxException { + classLoader = MetaRegistry.DEFAULT_REGISTRY.getMetaClassLoader(getClass().getClassLoader()); + thisClass = classLoader.findMetaClass(getClass().getCanonicalName()); + } + + @Test + public void testGetClassLocation() throws MalformedURLException { + String testClassLocation = thisClass.getClassLocation().getName(); + assertEquals(getClass().getProtectionDomain().getCodeSource().getLocation(), new URL(testClassLocation)); + } + + @Test + public void testGetParent() { + MetaClass pmc = thisClass.getParent(); + assertEquals(Object.class.getCanonicalName(), pmc.getName()); + } + + @Test + public void testGetInterfaces() { + assertFalse(thisClass.getInterfaces().iterator().hasNext()); + } +} Added: commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/bcel/PrimitiveClassTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/bcel/PrimitiveClassTest.java?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/bcel/PrimitiveClassTest.java (added) +++ commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/bcel/PrimitiveClassTest.java Thu Jul 5 02:48:34 2012 @@ -0,0 +1,72 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.classscan.bcel; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.net.URISyntaxException; + +import org.apache.commons.classscan.MetaClassLoader; +import org.apache.commons.classscan.MetaRegistry; +import org.apache.commons.classscan.model.MetaClass; +import org.junit.Before; +import org.junit.Test; + +public class PrimitiveClassTest { + + MetaClassLoader classLoader; + MetaClass byteClass; + + @Before + public void loadLocation() throws URISyntaxException { + classLoader = MetaRegistry.DEFAULT_REGISTRY.getMetaClassLoader(Byte.TYPE.getClassLoader()); + byteClass = classLoader.findMetaClass(Byte.TYPE.getCanonicalName()); + } + + @Test + public void testGetClassLocation() throws URISyntaxException { + assertNull(byteClass.getClassLocation()); + } + + @Test + public void testGetCanonicalName() { + assertEquals(Byte.TYPE.getCanonicalName(), byteClass.getName()); + } + + @Test + public void testGetParent() throws URISyntaxException { + assertNull(byteClass.getParent()); + } + + @Test + public void testGetInterfaces() { + assertEquals(0, byteClass.getInterfaces().size()); + } + + @Test + public void testGetAnnotations() { + assertEquals(0, byteClass.getAnnotations().size()); + } + + @Test + public void testGetMethods() { + assertEquals(0, byteClass.getMethods().size()); + } + + @Test + public void testGetFields() { + assertEquals(0, byteClass.getFields().size()); + } +} Added: commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/DefaultMetaClassPathElementTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/DefaultMetaClassPathElementTest.java?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/DefaultMetaClassPathElementTest.java (added) +++ commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/DefaultMetaClassPathElementTest.java Thu Jul 5 02:48:34 2012 @@ -0,0 +1,59 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.classscan.builtin; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.net.URISyntaxException; +import java.util.Collection; + +import org.apache.commons.classscan.MetaClassLoader; +import org.apache.commons.classscan.MetaClassPathElement; +import org.apache.commons.classscan.MetaRegistry; +import org.apache.commons.classscan.model.MetaClass; +import org.junit.Before; +import org.junit.Test; + +public class DefaultMetaClassPathElementTest { + + MetaClassPathElement location; + MetaClassLoader classLoader; + + @Before + public void loadLocation() throws URISyntaxException { + classLoader = MetaRegistry.DEFAULT_REGISTRY.getMetaClassLoader(getClass().getClassLoader()); + String thisUri = getClass().getProtectionDomain().getCodeSource().getLocation().toString(); + location = classLoader.getClassLocation(thisUri); + assertEquals(thisUri, location.getName()); + } + + @Test + public void testGetMetaClass() throws URISyntaxException { + MetaClass thisClass= location.getMetaClass(getClass().getCanonicalName()); + assertEquals(getClass().getCanonicalName(), thisClass.getName()); + } + + @Test + public void testGetMetaClasses() throws URISyntaxException { + Collection<? extends MetaClass> classes= location.getMetaClasses(); + + MetaClass thisClass= location.getMetaClass(getClass().getCanonicalName()); + assertTrue(classes.contains(thisClass)); + + MetaClass objClass = classLoader.findMetaClass(Object.class.getCanonicalName()); + assertFalse(classes.contains(objClass)); + } +} Added: commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/UrlClassPathTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/UrlClassPathTest.java?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/UrlClassPathTest.java (added) +++ commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/UrlClassPathTest.java Thu Jul 5 02:48:34 2012 @@ -0,0 +1,51 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.classscan.builtin; + +import static org.junit.Assert.assertEquals; + +import java.net.URISyntaxException; +import java.util.Map; + +import org.apache.commons.classscan.ClassPath; +import org.apache.commons.classscan.ClassPathElement; +import org.apache.commons.classscan.MetaRegistry; +import org.apache.commons.classscan.ResourceFile; +import org.apache.commons.classscan.test.classes.FullInterface; +import org.junit.Before; +import org.junit.Test; + +public class UrlClassPathTest { + + ClassPath classPath; + + @Before + public void loadLocation() throws URISyntaxException { + ClassLoader classLoader = getClass().getClassLoader(); + classPath = MetaRegistry.DEFAULT_REGISTRY.getClassPath(classLoader); + } + + @Test + public void testGetClassLocationsWithMetaFile() throws URISyntaxException { + String serviceFileName = "META-INF/services/"+FullInterface.class.getCanonicalName(); + + Map<ClassPathElement, ResourceFile> resources = classPath.getResources(serviceFileName); + assertEquals(1, resources.size()); + + for(Map.Entry<ClassPathElement, ResourceFile> entry : resources.entrySet()) { + ResourceFile file = entry.getValue(); + assertEquals(serviceFileName, file.getFileName()); + } + } +} Added: commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/UrlMetaClassLoaderTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/UrlMetaClassLoaderTest.java?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/UrlMetaClassLoaderTest.java (added) +++ commons/sandbox/classscan/trunk/bcel/src/test/java/org/apache/commons/classscan/builtin/UrlMetaClassLoaderTest.java Thu Jul 5 02:48:34 2012 @@ -0,0 +1,117 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.classscan.builtin; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.net.URISyntaxException; +import java.util.Collection; +import java.util.Iterator; + +import org.apache.commons.classscan.MetaClassLoader; +import org.apache.commons.classscan.MetaClassPathElement; +import org.apache.commons.classscan.MetaRegistry; +import org.apache.commons.classscan.model.MetaClass; +import org.apache.commons.classscan.test.classes.FullInterface; +import org.apache.commons.classscan.test.classes.FullyDecorated; +import org.apache.commons.classscan.test.classes.ValidateFullyDecorated; +import org.junit.Before; +import org.junit.Test; + +public class UrlMetaClassLoaderTest { + + private static final String OBJECT_CANONICAL_NAME = Object.class.getCanonicalName(); + private static final String FULLY_DECORATED_NAME = FullyDecorated.class.getCanonicalName(); + + MetaClassLoader classLoader; + MetaClass fdClass; + MetaClass objClass; + + @Before + public void loadLocation() throws URISyntaxException { + classLoader = MetaRegistry.DEFAULT_REGISTRY.getMetaClassLoader(getClass().getClassLoader()); + fdClass = classLoader.findMetaClass(FULLY_DECORATED_NAME); + objClass = classLoader.findMetaClass(OBJECT_CANONICAL_NAME); + } + + @Test + public void testGetMetaClass() { + new ValidateFullyDecorated(classLoader.getMetaClass(FULLY_DECORATED_NAME)); + assertNull(classLoader.getMetaClass(OBJECT_CANONICAL_NAME)); + } + + @Test + public void testFindMetaClass() { + assertEquals(FULLY_DECORATED_NAME, fdClass.getName()); + assertEquals(OBJECT_CANONICAL_NAME, objClass.getName()); + } + + <T> boolean contains(Iterator<? extends T> classes, T toFind) { + while(classes.hasNext()) { + T mc = classes.next(); + if(mc.equals(toFind)) { + return true; + } + } + return false; + } + + @Test + public void testGetMetaClasses() { + Iterator<? extends MetaClass> classes = classLoader.getMetaClasses(); + assertTrue(contains(classes, fdClass)); + assertFalse(contains(classes, objClass)); + } + + @Test + public void testIterateGetMetaClasses() { + Iterator<? extends MetaClass> classes = classLoader.getMetaClasses(); + while(classes.hasNext()) { + assertNotNull(classes.next()); + } + } + + @Test + public void testGetClassLocations() throws URISyntaxException { + Collection<? extends MetaClassPathElement> locations = classLoader.getClassLocations(); + MetaClassPathElement location = classLoader.getClassLocation(getClass().getProtectionDomain().getCodeSource().getLocation().toString()); + assertTrue(locations.contains(location)); + } + + @Test + public void testGetParent() { + for (int i = 0; i < 4; ++i) { + MetaClassLoader pcl = classLoader.getParent(); + if (pcl == null) { + // cl is now bootstrap class loader + assertNotNull(classLoader.getMetaClass(Integer.class.getCanonicalName())); + return; + } + classLoader = pcl; + } + fail("Too many nested ClassLoaders"); + } + + @Test + public void findAllImplementations() throws URISyntaxException { + Collection<? extends MetaClass> implementations= classLoader.findAllImplementors(FullInterface.class.getCanonicalName()); + assertTrue(implementations.contains(fdClass)); + assertFalse(implementations.contains(objClass)); + } +} Added: commons/sandbox/classscan/trunk/parent/pom.xml URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/parent/pom.xml?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/parent/pom.xml (added) +++ commons/sandbox/classscan/trunk/parent/pom.xml Thu Jul 5 02:48:34 2012 @@ -0,0 +1,115 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.commons.classscan</groupId> + <artifactId>parent</artifactId> + <name>ClassScan parent</name> + <version>0.2-SNAPSHOT</version> + <packaging>pom</packaging> + <description>Dependency and build directives to build the ClassScan libraries</description> + + <parent> + <groupId>org.apache.commons</groupId> + <artifactId>commons-parent</artifactId> + <version>24</version> + </parent> + + <properties> + <maven.compile.source>1.5</maven.compile.source> + <maven.compile.target>1.5</maven.compile.target> + <commons.subproject.location>sandbox/classscan/trunk</commons.subproject.location> + </properties> + + <scm> + <connection>scm:svn:http://svn.apache.org/repos/asf/commons/${commons.subproject.location}</connection> + <developerConnection>scm:svn:https://svn.apache.org/repos/asf/commons/${commons.subproject.location}</developerConnection> + <url>http://svn.apache.org/viewvc/commons/${commons.subproject.location}</url> + </scm> + + <build> + + <plugins> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <argLine>-Xmx512m</argLine> + </configuration> + </plugin> + + </plugins> + + </build> + + <dependencies> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.3</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.5.10</version> + </dependency> + + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-core</artifactId> + <version>0.9.18</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <version>0.9.18</version> + <scope>test</scope> + </dependency> + + </dependencies> + + <repositories> + <repository> + <id>apache-snapshot</id> + <url>https://repository.apache.org/content/repositories/snapshots</url> + <snapshots> + <enabled>true</enabled> + </snapshots> + </repository> + </repositories> + + <distributionManagement> + <site> + <id>apache.website</id> + <name>Apache Commons Site</name> + <url>${commons.deployment.protocol}://people.apache.org/www/commons.apache.org/${commons.componentid}</url> + </site> + </distributionManagement> + +</project> Added: commons/sandbox/classscan/trunk/pom.xml URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/pom.xml?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/pom.xml (added) +++ commons/sandbox/classscan/trunk/pom.xml Thu Jul 5 02:48:34 2012 @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.commons.classscan</groupId> + <artifactId>parent</artifactId> + <version>0.2-SNAPSHOT</version> + <relativePath>parent</relativePath> + </parent> + + <groupId>org.apache.commons.classscan</groupId> + <artifactId>classscan</artifactId> + <name>ClassScan Group</name> + <version>0.2-SNAPSHOT</version> + <packaging>pom</packaging> + <description>A collection of libraries to obtain information about class(es) without loading by the jvm</description> + + <modules> + <module>api</module> + <module>bcel</module> + </modules> + +</project> Added: commons/sandbox/classscan/trunk/src/site/site.xml URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/src/site/site.xml?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/src/site/site.xml (added) +++ commons/sandbox/classscan/trunk/src/site/site.xml Thu Jul 5 02:48:34 2012 @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project name="Lang"> + <bannerRight> + <name>Commons ClassScan</name> + <src>/images/logo.png</src> + <href>/index.html</href> + </bannerRight> + + <body> + <menu name="Lang"> + <item name="Overview" href="/index.html"/> + <item name="Users guide" href="/userguide.html"/> + </menu> + + <menu name="Development"> + <item name="Building" href="/building.html"/> + <item name="Mailing Lists" href="/mail-lists.html"/> + <item name="Javadoc (SVN latest)" href="apidocs/index.html"/> + </menu> + + </body> + +</project> Added: commons/sandbox/classscan/trunk/src/site/xdoc/building.xml URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/src/site/xdoc/building.xml?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/src/site/xdoc/building.xml (added) +++ commons/sandbox/classscan/trunk/src/site/xdoc/building.xml Thu Jul 5 02:48:34 2012 @@ -0,0 +1,46 @@ +<?xml version="1.0"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<document> + <properties> + <title>Building</title> + <author email="d...@commons.apache.org">Commons Documentation Team</author> + </properties> +<body> +<!-- ================================================== --> +<section name="Overview"> +<p> + Commons Lang uses <a href="http://maven.apache.org">Maven</a> as a build system. +</p> +</section> +<!-- ================================================== --> +<section name="Maven Goals"> + <p> + To build a jar file, change into the root directory of ClasspathInfo and run "mvn package". + The result will be in the "target" subdirectory. + </p> + <p> + To build the full website, run "mvn site". + The result will be in "target/site". + </p> + <p> + Further details can be found in the + <a href="http://commons.apache.org/building.html">commons build instructions</a>. + </p> +</section> +</body> +</document> Added: commons/sandbox/classscan/trunk/src/site/xdoc/index.xml URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/src/site/xdoc/index.xml?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/src/site/xdoc/index.xml (added) +++ commons/sandbox/classscan/trunk/src/site/xdoc/index.xml Thu Jul 5 02:48:34 2012 @@ -0,0 +1,68 @@ +<?xml version="1.0"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<document> + <properties> + <title>Commons ClasspathInfo</title> + <author email="d...@commons.apache.org">Commons Documentation Team</author> + </properties> +<body> +<!-- ================================================== --> +<section name="Commons ClasspathInfo"> + +<p> +The standard Java runtime hides many details about classes, particularly the location of classes which can potentially be loaded, and the details of unloaded classes. +</p> + +<p> +ClasspathInfo can provide information about all of the classes available to the runtime, whether or not the class has been loaded. +<ul>An application may want to discover: +<li>all of the implementers of a particular interface</li> +<li>all of the classes with a specific annotation (or even a particular annotation value)</li> +<li>a particular field or all of the fields of a class</li> +<li>a particular method or all of the fields of a class</li> +<li>Any of the above, limited by the location of the class (eg. only classes annotated with @Entity from a jar that has a META-INF/persistence.xml file</li> +</ul> +</p> +</section> +<!-- ================================================== --> +<section name="Documentation"> +<p> +A getting started <a href="userguide.html">user guide</a> is available +together with various <a href="project-reports.html">project reports</a>. +</p> +</section> +<!-- ================================================== --> +<section name="Release Information"> +</section> +<!-- ================================================== --> +<section name="Support"> +<p> +The <a href="mail-lists.html">commons mailing lists</a> act as the main support forum. +The user list is suitable for most library usage queries. +The dev list is intended for the development discussion. +Please remember that the lists are shared between all commons components, +so prefix your email by [meiyo]. +</p> +<!-- p> +Bug reports and enhancements are also welcomed via the <a href="issue-tracking.html">JIRA</a> issue tracker. +Please read the instructions carefully. +</p --> +</section> +<!-- ================================================== --> +</body> +</document> Added: commons/sandbox/classscan/trunk/src/site/xdoc/userguide.xml URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/trunk/src/site/xdoc/userguide.xml?rev=1357447&view=auto ============================================================================== --- commons/sandbox/classscan/trunk/src/site/xdoc/userguide.xml (added) +++ commons/sandbox/classscan/trunk/src/site/xdoc/userguide.xml Thu Jul 5 02:48:34 2012 @@ -0,0 +1,64 @@ +<?xml version="1.0"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +<document> + + <properties> + <title>Commons ClasspathInfo - User guide</title> + <author email="d...@commons.apache.org">Commons Documentation Team</author> + </properties> + + <body> + + + <section name="Description"> + <p>This library provides methods to search for classes without using reflection, in fact, without even loading the classes. This is particularly important when the class has + static initializers that cause behavior that cannot be undone. Additionally, framework libraries don't always have apriori knowledge of all classes which must be managed.</p> + </section> + + <section name="Find All Implementors of an Interface"> +<p>You can use the following code snippet to find all implementors of an interface.</p> + <source><![CDATA[ + MetaClassLoader classLoader = MetaRegistry.SINGLETON.getClassLoader(getClass().getClassLoader()); + Collection<? extends MetaClass> implementations = classLoader.findAllImplementors(FullInterface.class.getCanonicalName()); +]]></source> + </section> + + <section name="Find All Classes in a jar with a Annotation"> +<p>You can use the following code snippets to find all classes with a particular annotation in a jar with a marker META-INF file.</p> + <source><![CDATA[ + public void findAnnotatedClassesFromJar(MetaClassLoader classLoader) { + for(MetaClassLocation location : classLoader.getClassLocationsWithMetaFile("persistence.xml")) { + findAnnotatedClassesFromLocation(location); + } + } + + private void findAnnotatedClassesFromLocation(MetaClassLocation location) { + for(MetaClass mc : location.getMetaClasses()) { + MetaAnnotation ma = mc.getAnnotation("javax.persistence.Entity"); + if(ma!=null) { + workOnClass(mc); + } + } + } +]]></source> + </section> + + +</body> +</document>