Permalink
Browse files

optionally turn off pooled component weaving.

  • Loading branch information...
junkdog committed Dec 9, 2013
1 parent 2c49da8 commit e04aa6b6dbb493e2a3fae88b09a842aff58e146a
View
@@ -2,6 +2,10 @@
## Developed but not released
- Direct field access works for `@PackedWeaver` components.
+- `-DideFriendlyPacking`: If true, will leave field stubs to keep IDE:s
+ happy after transformations. Defaults to false.
+- `-DenablePooledWeaving`: Enables weaving of pooled components (more viable on
+ Android than JVM). Defaults to true.
## Version: 0.5.0 - 2013-11-24
- Changed artemis to a multi-module project (the `artemis` folder is the old root).
@@ -10,26 +10,43 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.sonatype.plexus.build.incremental.BuildContext;
import com.artemis.meta.ClassMetadata;
+/**
+ * The artemis plugin does bytecode-weaving on annotated components.
+ */
@Mojo(name="artemis", defaultPhase=PROCESS_CLASSES)
-public class ArtemisMaven extends AbstractMojo{
+public class ArtemisMaven extends AbstractMojo {
+ /**
+ * Root folder for class files.
+ */
@Parameter(property="project.build.outputDirectory")
private File outputDirectory;
+ /**
+ * Root source folder.
+ */
@Parameter(property="project.build.sourceDirectory")
private File sourceDirectory;
+ /**
+ * If true, will leave field stubs to keep IDE:s happy after transformations.
+ */
@Parameter
private boolean ideFriendlyPacking;
+ /**
+ * Enabled weaving of pooled components (more viable on Android than JVM).
+ */
+ @Parameter(defaultValue="true")
+ private boolean enablePooledWeaving;
+
@Component
private BuildContext context;
@@ -39,13 +56,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (context != null && !context.hasDelta(sourceDirectory))
return;
+ getLog().info(String.format("Configuration:\n\tideFriendlyPacking=%b \n\tenablePooledWeaving=%b ",
+ ideFriendlyPacking, enablePooledWeaving));
+
Weaver.retainFieldsWhenPacking(ideFriendlyPacking);
+ Weaver.enablePooledWeaving(enablePooledWeaving);
Weaver weaver = new Weaver(outputDirectory);
List<ClassMetadata> processed = weaver.execute();
- Log log = getLog();
- log.info(getSummary(processed, start));
+ getLog().info(getSummary(processed, start));
}
private static CharSequence getSummary(List<ClassMetadata> processed, long start) {
@@ -4,7 +4,7 @@
import com.artemis.annotations.PooledWeaver;
-@PooledWeaver @SuppressWarnings("unused")
+@PooledWeaver(forceWeaving=true) @SuppressWarnings("unused")
public class PooledAllFields extends Component {
private boolean _boolean = true;
private char _char = 'a';
@@ -8,12 +8,10 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.artemis.ComponentMapper;
-import com.artemis.PackedComponent;
public class LongPackedWeavingTest extends PackedWeavingTest {
@@ -14,7 +14,6 @@
import com.artemis.ComponentMapper;
import com.artemis.Entity;
-import com.artemis.EntityManager;
import com.artemis.World;
public abstract class PackedWeavingTest {
@@ -3,7 +3,6 @@
import static com.artemis.FluentUtil.element;
import static com.artemis.FluentUtil.Match.ONE_OF;
import static javax.lang.model.element.Modifier.FINAL;
-import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.STATIC;
import static javax.lang.model.util.ElementFilter.constructorsIn;
import static javax.lang.model.util.ElementFilter.fieldsIn;
@@ -18,6 +18,7 @@
import org.objectweb.asm.Type;
import com.artemis.meta.ClassMetadata;
+import com.artemis.meta.ClassMetadata.GlobalConfiguration;
import com.artemis.meta.ClassMetadata.WeaverType;
import com.artemis.meta.FieldDescriptor;
import com.artemis.meta.MetaScanner;
@@ -70,6 +71,11 @@ public static void retainFieldsWhenPacking(boolean ideFriendlyPacking) {
ClassMetadata.GlobalConfiguration.ideFriendlyPacking = ideFriendlyPacking;
}
+
+ public static void enablePooledWeaving(boolean enablePooledWeaving) {
+ ClassMetadata.GlobalConfiguration.enabledPooledWeaving = enablePooledWeaving;
+ }
+
private void rewriteFieldAccess(List<ClassMetadata> packed) {
if (packed.isEmpty())
return;
@@ -93,6 +99,10 @@ private static void processClass(ExecutorService threadPool, String file, List<C
if (meta.annotation == WeaverType.NONE)
return;
+
+ if (meta.annotation == WeaverType.POOLED && !GlobalConfiguration.enabledPooledWeaving) {
+ if (!meta.forcePooledWeaving) return;
+ }
threadPool.submit(new ComponentTypeTransmuter(file, cr, meta));
processed.add(meta);
@@ -23,13 +23,18 @@
public List<MethodDescriptor> methods = new ArrayList<MethodDescriptor>();
public List<FieldDescriptor> fields = new ArrayList<FieldDescriptor>();
+ // pooled components
+ public boolean forcePooledWeaving;
+
// packed components
public boolean foundStaticInitializer;
public boolean directFieldAccess;
+
public static enum WeaverType { NONE, POOLED, PACKED };
public static class GlobalConfiguration {
public static boolean ideFriendlyPacking;
+ public static boolean enabledPooledWeaving;
}
}
@@ -10,8 +10,6 @@
import java.util.List;
import java.util.Set;
-import com.artemis.meta.ClassMetadata.WeaverType;
-
public final class ClassMetadataUtil {
private ClassMetadata meta;
@@ -13,11 +13,30 @@
import com.artemis.meta.ClassMetadata.WeaverType;
-public class MetaScanner extends ClassVisitor {
+public class MetaScanner extends ClassVisitor implements Opcodes {
+
+ private static final class AnnotationReader extends AnnotationVisitor {
+
+ private ClassMetadata info;
+
+ private AnnotationReader(AnnotationVisitor av, ClassMetadata meta) {
+ super(ASM4, av);
+ this.info = meta;
+ }
+
+ @Override
+ public void visit(String name, Object value) {
+ if ("forceWeaving".equals(name)) {
+ info.forcePooledWeaving = (Boolean)value;
+ }
+ super.visit(name, value);
+ }
+ }
+
private ClassMetadata info;
public MetaScanner(ClassMetadata metadata) {
- super(Opcodes.ASM4);
+ super(ASM4);
info = metadata;
}
@@ -29,14 +48,18 @@ public void visit(int version, int access, String name, String signature, String
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
- if (PACKED_ANNOTATION.equals(desc))
+ AnnotationVisitor av = super.visitAnnotation(desc, visible);
+
+ if (PACKED_ANNOTATION.equals(desc)) {
info.annotation = WeaverType.PACKED;
- else if (POOLED_ANNOTATION.equals(desc))
+ } else if (POOLED_ANNOTATION.equals(desc)) {
info.annotation = WeaverType.POOLED;
- else if (WOVEN_ANNOTATION.equals(desc))
+ av = new AnnotationReader(av, info);
+ } else if (WOVEN_ANNOTATION.equals(desc)) {
info.isPreviouslyProcessed = true;
-
- return super.visitAnnotation(desc, visible);
+ }
+
+ return av;
}
@Override
@@ -5,16 +5,26 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import com.artemis.component.ComponentToWeave;
import com.artemis.component.PackedToBeB;
+import com.artemis.component.PooledForced;
+import com.artemis.component.PooledNotForced;
import com.artemis.meta.ClassMetadata;
+import com.artemis.meta.ClassMetadata.GlobalConfiguration;
import com.artemis.meta.ClassMetadata.WeaverType;
@SuppressWarnings("static-method")
public class ComponentTypeWeaverTest {
-
+
+ @Before
+ public void init() {
+ GlobalConfiguration.enabledPooledWeaving = true;
+ }
+
@Test
public void pooled_weaver_test() throws Exception {
ClassMetadata meta = transform(ComponentToWeave.class);
@@ -32,4 +42,23 @@ public void packed_weaver_test() throws Exception {
assertTrue(meta.foundEntityFor);
assertEquals("com/artemis/PackedComponent", meta.superClass);
}
+
+ @Test @Ignore // rewrite to match actual waeving
+ public void pooled_disbled_weaving_test() throws Exception {
+ GlobalConfiguration.enabledPooledWeaving = false;
+
+ ClassMetadata meta = transform(PooledNotForced.class);
+ assertFalse(meta.foundReset);
+ assertEquals("com/artemis/Component", meta.superClass);
+ }
+
+ @Test
+ public void pooled_forced_weaving_test() throws Exception {
+ GlobalConfiguration.enabledPooledWeaving = false;
+
+ ClassMetadata meta = transform(PooledForced.class);
+ assertEquals(WeaverType.NONE, meta.annotation);
+ assertTrue(meta.foundReset);
+ assertEquals("com/artemis/PooledComponent", meta.superClass);
+ }
}
@@ -0,0 +1,10 @@
+package com.artemis.component;
+
+import com.artemis.annotations.PooledWeaver;
+
+
+@PooledWeaver(forceWeaving=true)
+public class PooledForced {
+ public float x;
+ public float y;
+}
@@ -0,0 +1,10 @@
+package com.artemis.component;
+
+import com.artemis.annotations.PooledWeaver;
+
+
+@PooledWeaver()
+public class PooledNotForced {
+ public float x;
+ public float y;
+}
@@ -18,6 +18,7 @@
import com.artemis.component.PooledComponentWithReset;
import com.artemis.meta.ClassMetadata.WeaverType;
+@SuppressWarnings("static-method")
public class MetaScannerTest {
private World world;
@@ -28,7 +29,7 @@ public void setup() {
world.initialize();
}
- @Test
+ @Test @SuppressWarnings("unused")
public void pooled_component_scanning() throws Exception {
Entity e1 = world.createEntity();
ComponentToWeave c1a = e1.createComponent(ComponentToWeave.class);
@@ -10,4 +10,11 @@
* @see <a href="https://github.com/junkdog/artemis-odb/wiki/Component%20Types">Component types</a>
* on the wiki.
*/
-public @interface PooledWeaver {}
+public @interface PooledWeaver {
+
+ /**
+ * If true, forces weaving even if maven property <code>enablePooledWeaving</code> is
+ * set to <code>false</code>.
+ */
+ boolean forceWeaving() default false;
+}
@@ -8,7 +8,6 @@
import java.util.HashSet;
import java.util.Set;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -1,11 +1,8 @@
package com.artemis;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -1,7 +1,6 @@
package com.artemis;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
import org.junit.Assert;
import org.junit.Before;
@@ -1,6 +1,7 @@
package com.artemis.managers;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import org.junit.Before;
import org.junit.Test;

0 comments on commit e04aa6b

Please sign in to comment.