JavaClassArtifactBuilder extends AbstractArtifactBuilder { @Override public ArtifactBuilder buildArtifacts(BaseArtifactType primaryArtifact, ArtifactContent artifactContent) throws Exception { super.buildArtifacts(primaryArtifact, artifactContent); ClassParser parser = new ClassParser(getContentStream(), primaryArtifact.getName()); JavaClass javaClass = parser.parse(); if (javaClass.isInterface()) { ((ExtendedDocument) primaryArtifact).setExtendedType(JavaModel.TYPE_JAVA_INTERFACE); } else if (javaClass.isClass()) { ((ExtendedDocument) primaryArtifact).setExtendedType(JavaModel.TYPE_JAVA_CLASS); } else if (javaClass.isEnum()) { ((ExtendedDocument) primaryArtifact).setExtendedType(JavaModel.TYPE_JAVA_ENUM); } String packageName = javaClass.getPackageName(); String className = javaClass.getClassName(); primaryArtifact.setName(className); String shortName = className; if (className.lastIndexOf('.') > 0) { shortName = className.substring(className.lastIndexOf('.') + 1); } ArtificerModelUtils.setCustomProperty(primaryArtifact, JavaModel.PROP_PACKAGE_NAME, packageName); ArtificerModelUtils.setCustomProperty(primaryArtifact, JavaModel.PROP_CLASS_NAME, shortName); return this; }  @Override ArtifactBuilder buildArtifacts(BaseArtifactType primaryArtifact, ArtifactContent artifactContent); @Override ArtifactBuilder buildRelationships(RelationshipContext context);  }
@Test public void testJavaDeriver() throws Exception { JavaClassArtifactBuilder builder = new JavaClassArtifactBuilder(); ExtendedDocument artifact = new ExtendedDocument(); artifact.setArtifactType(BaseArtifactEnum.EXTENDED_ARTIFACT_TYPE); artifact.setName("MyClass.class"); InputStream is = getClass().getResourceAsStream("MyClass.class"); Collection<BaseArtifactType> derivedArtifacts = builder.buildArtifacts(artifact, new ArtifactContent("MyClass.class", is)) .getDerivedArtifacts(); is.close(); Assert.assertNotNull(derivedArtifacts); Assert.assertEquals(0, derivedArtifacts.size()); Assert.assertEquals("org.overlord.sramp.integration.java.deriver.MyClass", artifact.getName()); Assert.assertEquals("org.overlord.sramp.integration.java.deriver", ArtificerModelUtils.getCustomProperty(artifact, JavaModel.PROP_PACKAGE_NAME)); Assert.assertEquals("MyClass", ArtificerModelUtils.getCustomProperty(artifact, JavaModel.PROP_CLASS_NAME)); Assert.assertEquals(JavaModel.TYPE_JAVA_CLASS, artifact.getExtendedType()); artifact = new ExtendedDocument(); artifact.setArtifactType(BaseArtifactEnum.EXTENDED_ARTIFACT_TYPE); artifact.setName("MyInterface.class"); is = getClass().getResourceAsStream("MyInterface.class"); derivedArtifacts = builder.buildArtifacts(artifact, new ArtifactContent("MyInterface.class", is)).getDerivedArtifacts(); is.close(); Assert.assertNotNull(derivedArtifacts); Assert.assertEquals(0, derivedArtifacts.size()); Assert.assertEquals("org.overlord.sramp.integration.java.deriver.MyInterface", artifact.getName()); Assert.assertEquals("org.overlord.sramp.integration.java.deriver", ArtificerModelUtils.getCustomProperty(artifact, JavaModel.PROP_PACKAGE_NAME)); Assert.assertEquals("MyInterface", ArtificerModelUtils.getCustomProperty(artifact, JavaModel.PROP_CLASS_NAME)); Assert.assertEquals(JavaModel.TYPE_JAVA_INTERFACE, artifact.getExtendedType()); }