Skip to content

Commit

Permalink
Default values using annotation + isDecompiled is transformed to isWi…
Browse files Browse the repository at this point in the history
…ndupGenerated
  • Loading branch information
mbriskar committed Nov 4, 2015
1 parent 7052824 commit 45e8fcf
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jboss.windup.graph.frames;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* A windup-specific annotation that is used within
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface FrameBooleanDefaultValue {

public boolean value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.apache.commons.lang.StringUtils;
import org.jboss.windup.graph.Indexed;
import org.jboss.windup.graph.frames.FrameBooleanDefaultValue;
import org.jboss.windup.graph.model.ArchiveModel;
import org.jboss.windup.graph.model.ProjectModel;

Expand Down Expand Up @@ -35,6 +36,7 @@ public interface FileModel extends ResourceModel
String IS_DIRECTORY = "isDirectory";
String PRETTY_PATH = "fileModelPrettyPath";
String PRETTY_PATH_WITHIN_PROJECT = "fileModelPrettyPathWithinProject";
String WINDUP_GENERATED = "windupGenerated";

/**
* Contains the File Name (the last component of the path). Eg, a file /tmp/foo/bar/file.txt would have fileName set to "file.txt"
Expand Down Expand Up @@ -176,6 +178,19 @@ public interface FileModel extends ResourceModel
@JavaHandler
ProjectModel getApplication();

/**
* Specifies if the given file was generated by windup or it originates from application.
*/
@Property(WINDUP_GENERATED)
Boolean isWindupGenerated();

/**
* Specifies if the given file was generated by windup or it originates from application.
*/
@FrameBooleanDefaultValue(false)
@Property(WINDUP_GENERATED)
void setWindupGenerated(boolean generated);

abstract class Impl implements FileModel, JavaHandlerContext<Vertex>
{
public ProjectModel getApplication()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.jboss.windup.graph;

import com.tinkerpop.blueprints.Element;
import com.tinkerpop.frames.FrameInitializer;
import com.tinkerpop.frames.FramedGraph;
import com.tinkerpop.frames.Property;
import org.jboss.windup.graph.frames.FrameBooleanDefaultValue;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
* A tinkerpop frame initializer that makes it possible to specify default values for the elements
*
* @author <a href="mailto:mbriskar@gmail.com">Matej Briskar</a>
*/
public class DefaultValueInitializer implements FrameInitializer
{
private Map<Class<?>, LinkedList<PropertyDefaultValue>> cachedValues = new HashMap<>();

public void initElement(final Class<?> kind, final FramedGraph<?> framedGraph, final Element element)
{
if (!cachedValues.containsKey(kind))
{
cacheFrameInterface(kind);
}
setupDefaults(element, cachedValues.get(kind));
}

private void cacheFrameInterface(Class<?> kind) {
LinkedList<PropertyDefaultValue> values = new LinkedList<>();
for (Method m : kind.getMethods())
{
Annotation[] annotations = m.getAnnotations();
for (Annotation annotation : m.getAnnotations())
{
if (annotation instanceof FrameBooleanDefaultValue)
{
PropertyDefaultValue pDefault = new PropertyDefaultValue();
pDefault.value = ((FrameBooleanDefaultValue) annotation).value();
pDefault.key = m.getAnnotation(Property.class).value();
values.add(pDefault);
}
}
}
cachedValues.put(kind, values);
}

private void setupDefaults(Element element, LinkedList<PropertyDefaultValue> values)
{
for (PropertyDefaultValue pValue : values)
{
element.setProperty(pValue.key, pValue.value);
}
}

private class PropertyDefaultValue
{
private String key;
private Object value;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Set;
import java.util.logging.Logger;

import com.tinkerpop.blueprints.Element;
import com.tinkerpop.frames.FrameInitializer;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
Expand Down Expand Up @@ -143,7 +145,7 @@ public ClassLoader resolveClassLoader(Class<?> frameType)
public Graph configure(Graph baseGraph, FramedGraphConfiguration config)
{
config.setFrameClassLoaderResolver(classLoaderResolver);

config.addFrameInitializer(new DefaultValueInitializer());
config.addMethodHandler(new MapInPropertiesHandler());
config.addMethodHandler(new MapInAdjacentPropertiesHandler());
config.addMethodHandler(new MapInAdjacentVerticesHandler());
Expand All @@ -153,6 +155,8 @@ public Graph configure(Graph baseGraph, FramedGraphConfiguration config)
}
};



FramedGraphFactory factory = new FramedGraphFactory(
addModules,
new JavaHandlerModule(), // Supports @JavaHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jboss.windup.graph.test;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.arquillian.AddonDependencies;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.archive.AddonArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.GraphContextFactory;
import org.jboss.windup.graph.typedgraph.DefaultValueTestModel;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;

@RunWith(Arquillian.class)
public class DefaultValueTest
{
@Deployment
@AddonDependencies({
@AddonDependency(name = "org.jboss.windup.graph:windup-graph"),
@AddonDependency(name = "org.jboss.forge.furnace.container:cdi")
})
public static AddonArchive getDeployment()
{
AddonArchive archive = ShrinkWrap.create(AddonArchive.class)
.addBeansXML()
.addClasses(DefaultValueTestModel.class);
return archive;
}

@Inject
private GraphContextFactory factory;

@Test
public void testDefaultValue() throws Exception
{
try (GraphContext context = factory.create())
{
Assert.assertNotNull(context);
DefaultValueTestModel initialModelType = context.getFramed().addVertex(null, DefaultValueTestModel.class);
Assert.assertFalse(initialModelType.getDefaultFalseValue());
Assert.assertTrue(initialModelType.getDefaultTrueValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jboss.windup.graph.typedgraph;

import com.tinkerpop.frames.Property;
import com.tinkerpop.frames.modules.typedgraph.TypeValue;
import org.jboss.windup.graph.frames.FrameBooleanDefaultValue;
import org.jboss.windup.graph.model.WindupVertexFrame;
import org.jboss.windup.graph.model.resource.ResourceModel;

/**
* A vertex used to test default value settings
* @author <a href="mailto:mbriskar@redhat.com">Matej Briskar</a>
*/
@TypeValue("DefaultValueTestModel")
public interface DefaultValueTestModel extends WindupVertexFrame
{

@Property("defaultFalse")
@FrameBooleanDefaultValue(false)
public void setDefaultFalseValue(Boolean prop);

@Property("defaultFalse")
public Boolean getDefaultFalseValue();

@Property("defaultTrue")
@FrameBooleanDefaultValue(true)
public void setDefaultTrueValue(Boolean prop);

@Property("defaultTrue")
public Boolean getDefaultTrueValue();


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jboss.windup.graph.typedgraph;

import org.jboss.windup.graph.frames.FrameBooleanDefaultValue;
import org.jboss.windup.graph.model.resource.ResourceModel;

import com.tinkerpop.blueprints.Vertex;
Expand Down Expand Up @@ -33,6 +34,13 @@ public interface TestFooModel extends ResourceModel
@Property("prop3")
public String getProp3();

@Property("prop4")
@FrameBooleanDefaultValue(false)
public void setProp4(Boolean prop);

@Property("prop4")
public Boolean getProp4();

@JavaHandler
public String testJavaMethod();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public void perform(GraphRewrite event, EvaluationContext context, JspSourceFile
// Setup some basic details about the "Java Class"
// source root, is decompiled, javaclass. package name
sourceFile.setPackageName("");
sourceFile.setDecompiled(false);
JavaClassService javaClassService = new JavaClassService(event.getGraphContext());
JavaClassModel classModel = javaClassService.create();
classModel.setPackageName("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private PipeFunction returnVerticesToDelete(final GraphContext context)
while (iterator.hasNext())
{
JavaSourceFileModel javaModel = iterator.next();
if (javaModel.isDecompiled() != null && javaModel.isDecompiled() && !uniqueClassFound)
if (javaModel.isWindupGenerated() != null && javaModel.isWindupGenerated() && !uniqueClassFound)
{
uniqueClassFound = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void run()
JavaClassFileModel classModel = (JavaClassFileModel) classFileModel;
classModel.getJavaClass().setDecompiledSource(decompiledSourceFileModel);
decompiledSourceFileModel.setPackageName(classModel.getPackageName());
decompiledSourceFileModel.setDecompiled(true);
decompiledSourceFileModel.setWindupGenerated(true);

// Set the root path of this source file (if possible). Procyon should always be placing the file
// into a location that is appropriate for the package name, so this should always yield
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public void run()
JavaClassFileModel classModel = (JavaClassFileModel) classFileModel;
classModel.getJavaClass().setDecompiledSource(decompiledSourceFileModel);
decompiledSourceFileModel.setPackageName(classModel.getPackageName());
decompiledSourceFileModel.setDecompiled(true);
decompiledSourceFileModel.setWindupGenerated(true);

// Set the root path of this source file (if possible). Procyon should always be placing the file
// into a location that is appropriate for the package name, so this should always yield
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public interface AbstractJavaSourceModel extends FileModel, SourceFileModel
String PACKAGE_NAME = "packageName";
String JAVA_CLASS_MODEL = "javaClass";
String ROOT_SOURCE_FOLDER = "rootSourceFolder";
String IS_DECOMPILED = "decompiled";

/**
* This is the "root" directory for this source file.
Expand Down Expand Up @@ -71,15 +70,4 @@ public interface AbstractJavaSourceModel extends FileModel, SourceFileModel
@Adjacency(label = JAVA_CLASS_MODEL, direction = Direction.OUT)
void addJavaClass(JavaClassModel javaClassModel);

/**
* Specifies if the given .java file was decompiled from a .class file
*/
@Property(IS_DECOMPILED)
Boolean isDecompiled();

/**
* Specifies if the given .java file was decompiled from a .class file
*/
@Property(IS_DECOMPILED)
void setDecompiled(boolean decompiled);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface JavaClassFileModel extends FileModel
String PROPERTY_PACKAGE_NAME = "packageName";
String SKIP_DECOMPILATION = "skipDecompilation";

String DECOMPILED_FILE = "decompiledFile";

/**
* Indicates that we should not decompile this. This can allow us to skip the decompilation of class files that are not determined to be relevant
* for source scanning.
Expand Down Expand Up @@ -86,4 +88,5 @@ public interface JavaClassFileModel extends FileModel
*/
@Property(MINOR_VERSION)
void setMinorVersion(int minorVersion);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.jboss.windup.rules.apps.java.reporting.freemarker;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.tinkerpop.blueprints.Direction;
import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.graph.model.resource.FileModel;
Expand All @@ -14,6 +16,7 @@
import org.jboss.windup.rules.apps.java.model.JavaSourceFileModel;
import org.jboss.windup.rules.apps.xml.model.XmlFileModel;
import org.jboss.windup.rules.files.model.FileLocationModel;
import org.jboss.windup.rules.files.model.FileReferenceModel;
import org.jboss.windup.util.ExecutionStatistics;

import com.tinkerpop.blueprints.Vertex;
Expand All @@ -23,13 +26,13 @@

/**
* Finds the files that have not had {@link ClassificationModel}s linked, and also does not have {@link FileLocationModel}s linked.
*
* <p>
* Called by:
*
* <p>
* findFilesNotClassifiedOrHinted(Iterable<FileModel>)
*
* <p>
* NOTE: This will only return JavaSourceFileModels and XmlFileModels in order to reduce clutter.
*
*
* @author <a href="mailto:jesse.sightler@gmail.com">Jesse Sightler</a>
*/
public class FindFilesNotClassifiedOrHinted implements WindupFreeMarkerMethod
Expand Down Expand Up @@ -67,8 +70,25 @@ public Object exec(@SuppressWarnings("rawtypes") List arguments) throws Template
for (Vertex v : result)
{
FileModel f = context.getFramed().frame(v, FileModel.class);
if (f instanceof JavaSourceFileModel || f instanceof XmlFileModel || f instanceof JavaClassFileModel)

//we don't want to show our decompiled classes in the report
boolean wasNotGenerated = !f.isWindupGenerated();
boolean isOfInterestingType = f instanceof JavaSourceFileModel || f instanceof XmlFileModel || f instanceof JavaClassFileModel;
//we don't want to list .class files that have their decompiled .java file with hints/classifications
boolean withoutHiddenHints = true;

if (f instanceof JavaClassFileModel)
{
Iterator<Vertex> decompiled = v.getVertices(Direction.OUT, JavaClassFileModel.DECOMPILED_FILE).iterator();
if (decompiled.hasNext())
{
withoutHiddenHints = !decompiled.next().getVertices(Direction.IN, FileReferenceModel.FILE_MODEL).iterator().hasNext();
}
}

if (wasNotGenerated && withoutHiddenHints && isOfInterestingType)
{
//if it passed all the checks, add it
resultModels.add(f);
}
}
Expand Down
Loading

0 comments on commit 45e8fcf

Please sign in to comment.