Skip to content

Commit

Permalink
Added documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Aug 31, 2016
1 parent c538a5e commit 06df257
Show file tree
Hide file tree
Showing 10 changed files with 379 additions and 35 deletions.
Expand Up @@ -3,27 +3,58 @@
import java.io.File;
import java.util.Iterator;

/**
* An abstract base class for a user configuration implying a class path.
*/
public class AbstractUserConfiguration {

/**
* The class to use for this configuration path or {@code null} if no class path is specified.
*/
private Iterable<File> classPath;

/**
* Returns the class path or builds a class path from the supplied arguments if no class path was set.
*
* @param root The root directory of the project being built.
* @param classPath The class path dependencies.
* @return An iterable of all elements of the class path to be used.
*/
public Iterable<? extends File> getClassPath(File root, Iterable<? extends File> classPath) {
return this.classPath == null
? new PrefixIterable(root, classPath)
: this.classPath;
}

/**
* Sets the class path to use for this configuration.
*
* @param classPath The class path to use.
*/
public void setClassPath(Iterable<File> classPath) {
this.classPath = classPath;
}

/**
* An iterable with a single {@link File} element prepended.
*/
protected static class PrefixIterable implements Iterable<File> {

/**
* The prefixed file.
*/
private final File file;

/**
* The iterable containing the reminder files.
*/
private final Iterable<? extends File> files;

public PrefixIterable(File file, Iterable<? extends File> files) {
/**
* @param file The prefixed file.
* @param files The iterable containing the reminder files.
*/
protected PrefixIterable(File file, Iterable<? extends File> files) {
this.file = file;
this.files = files;
}
Expand All @@ -33,15 +64,56 @@ public Iterator<File> iterator() {
return new PrefixIterator(file, files.iterator());
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
PrefixIterable files1 = (PrefixIterable) object;
return file.equals(files1.file) && files.equals(files1.files);
}

@Override
public int hashCode() {
int result = file.hashCode();
result = 31 * result + files.hashCode();
return result;
}

@Override
public String toString() {
return "AbstractUserConfiguration.PrefixIterable{" +
"file=" + file +
", files=" + files +
'}';
}

/**
* An iterator with a single prefixed file.
*/
protected static class PrefixIterator implements Iterator<File> {

/**
* The file being prefixed.
*/
private final File file;

/**
* An iterator over the reminind files.
*/
private final Iterator<? extends File> files;

/**
* {@code true} if the prefix was not yet returned from the iteration.
*/
private boolean first;

public PrefixIterator(File file, Iterator<? extends File> files) {
/**
* Creates a prefix iterator.
*
* @param file The file being prefixed.
* @param files An iterator over the reminind files.
*/
protected PrefixIterator(File file, Iterator<? extends File> files) {
this.file = file;
this.files = files;
first = true;
Expand All @@ -64,7 +136,16 @@ public File next() {

@Override
public void remove() {
throw new UnsupportedOperationException("Cannot remove from iterator");
throw new UnsupportedOperationException("Cannot remove file from iterator");
}

@Override
public String toString() {
return "AbstractUserConfiguration.PrefixIterable.PrefixIterator{" +
"file=" + file +
", files=" + files +
", first=" + first +
'}';
}
}
}
Expand Down
@@ -1,66 +1,162 @@
package net.bytebuddy.build.gradle;

import groovy.lang.Closure;
import net.bytebuddy.dynamic.scaffold.inline.MethodNameTransformer;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;

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

/**
* Representation of a Gradle configuration for Byte Buddy.
*/
public class ByteBuddyExtension {

/**
* The current project.
*/
private final Project project;

private List<Transformation> transformations;
/**
* A list of registered transformations.
*/
private final List<Transformation> transformations;

/**
* The initialization or {@code null} if no initialization was defined by a user.
*/
private Initialization initialization;

/**
* The suffix to use for rebased methods or {@code null} if a random suffix should be used.
*/
private String suffix;

/**
* {@code true} if the plugin should fail upon discovering a live runtime initializer.
*/
private boolean failOnLiveInitializer;

private List<String> tasks;

/**
* Creates a new Byte Buddy extension.
*
* @param project The current project.
*/
public ByteBuddyExtension(Project project) {
this.project = project;
transformations = new ArrayList<Transformation>();
failOnLiveInitializer = true;
}

public Transformation transformation(Closure<?> closure) {
/**
* Adds a transformation to apply.
*
* @param closure The closure for configuring the transformation.
*/
public void transformation(Closure<?> closure) {
Transformation transformation = (Transformation) project.configure(new Transformation(), closure);
transformations.add(transformation);
return transformation;
}

public Initialization initialization(Closure<?> closure) {
/**
* Adds an initialization to apply.
*
* @param closure The closure for configuring the initialization.
*/
public void initialization(Closure<?> closure) {
if (initialization != null) {
throw new GradleException("Initialization is already set");
}
Initialization initialization = (Initialization) project.configure(new Initialization(), closure);
this.initialization = initialization;
return initialization;
initialization = (Initialization) project.configure(new Initialization(), closure);
}

/**
* Returns a list of transformations to apply.
*
* @return A list of transformations to apply.
*/
public List<Transformation> getTransformations() {
return transformations;
}

/**
* Returns the initialization to use.
*
* @return The initialization to use.
*/
public Initialization getInitialization() {
return initialization == null ? Initialization.makeDefault() : initialization;
return initialization == null
? Initialization.makeDefault()
: initialization;
}

public String getSuffix() {
return suffix;
/**
* Returns the method name transformer to use.
*
* @return The method name transformer to use.
*/
public MethodNameTransformer getMethodNameTransformer() {
return suffix == null || suffix.isEmpty()
? MethodNameTransformer.Suffixing.withRandomSuffix()
: new MethodNameTransformer.Suffixing(suffix);
}

/**
* Sets the suffix to apply upon rebased methods.
*
* @param suffix The suffix to apply upon rebased methods.
*/
public void setSuffix(String suffix) {
this.suffix = suffix;
}

/**
* Returns {@code true} if the build should fail upon discovering a live runtime initializer.
*
* @return {@code true} if the build should fail upon discovering a live runtime initializer.
*/
public boolean isFailOnLiveInitializer() {
return failOnLiveInitializer;
}

/**
* Determines if the build should fail upon discovering a live runtime initializer.
*
* @param failOnLiveInitializer {@code true} if the build should fail upon discovering a live runtime initializer.
*/
public void setFailOnLiveInitializer(boolean failOnLiveInitializer) {
this.failOnLiveInitializer = failOnLiveInitializer;
}

/**
* Sets the initialization that should be used.
*
* @param initialization The initialization to be used.
*/
public void setInitialization(Initialization initialization) {
this.initialization = initialization;
}

/**
* Determines if a task is subject to transformation.
*
* @param task The task to consider.
* @return {@code true} if this task should be followed up by a transformation.
*/
public boolean implies(Task task) {
return tasks == null || tasks.contains(task.getName());
}

/**
* Sets an explicit list of tasks for which a transformation should be applied.
*
* @param tasks The tasks to explicitly append a transformation to.
*/
public void setTasks(List<String> tasks) {
this.tasks = tasks;
}
}
Expand Up @@ -4,6 +4,9 @@
import org.gradle.api.Project;
import org.gradle.api.tasks.compile.AbstractCompile;

/**
* A Byte Buddy plugin that appends transformations to all compilation tasks.
*/
public class ByteBuddyPlugin implements Plugin<Project> {

@Override
Expand Down

0 comments on commit 06df257

Please sign in to comment.