Skip to content

Commit

Permalink
First version :
Browse files Browse the repository at this point in the history
contains some spoon processor for diversify
  • Loading branch information
petitpre committed Aug 25, 2015
0 parents commit bc5b322
Show file tree
Hide file tree
Showing 44 changed files with 3,668 additions and 0 deletions.
76 changes: 76 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Created by https://www.gitignore.io/api/java,intellij,maven

### Java ###
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties


### Maven ###
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
81 changes: 81 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>fr.inria.diversify</groupId>
<artifactId>SpoonProcessors</artifactId>
<version>1.1-SNAPSHOT</version>

<properties>
<gmavenVersion>1.4</gmavenVersion>
<gmavenProviderSelection>2.0</gmavenProviderSelection>
<groovyVersion>2.0.0</groovyVersion>
</properties>

<contributors>
<contributor>
<name>Nicolas Petitprez</name>
<email>[email protected]</email>
<roles>
<role>creator</role>
</roles>
</contributor>

</contributors>

<dependencies>
<dependency>
<groupId>fr.inria.gforge.spoon</groupId>
<artifactId>spoon-core</artifactId>
<version>4.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovyVersion}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>${gmavenVersion}</version>
<configuration>
<providerSelection>${gmavenProviderSelection}</providerSelection>
<sourceEncoding>UTF-8</sourceEncoding>
</configuration>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovyVersion}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Binary file not shown.
Binary file added src/docs/rapportTP1OPL.odt
Binary file not shown.
95 changes: 95 additions & 0 deletions src/main/groovy/fr/inria/diversify/FantomProcessor.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package fr.inria.diversify

import spoon.processing.AbstractProcessor
import spoon.reflect.declaration.CtClass
import spoon.reflect.declaration.CtMethod
import spoon.reflect.declaration.ModifierKind
import spoon.support.reflect.code.CtBlockImpl
import spoon.support.reflect.code.CtCodeSnippetStatementImpl
import spoon.support.reflect.code.CtInvocationImpl
import spoon.support.reflect.code.CtLocalVariableImpl
import spoon.support.reflect.declaration.CtMethodImpl

import java.lang.reflect.Method

import static fr.inria.diversify.utils.Looper.loop

/**
* Created by nicolas on 25/08/2015.
*/
public class FantomProcessor extends AbstractProcessor<CtClass<?>> {

public static final int FAKE_METHODS = 10;
public static final int RANDOM_CALLS = 10;
private Random random = new Random();
def actionDictionary = ["do", "get", "add", "generate", "process", "create", "is", "to"]
def objectDictionary = ["Method", "Value", "A", "Content", "Aray", "Hash"]
def fantoms = [];

@Override
void process(CtClass<?> element) {
fantoms.clear();

Collection<Method> previous = new ArrayList<>(element.getMethods());

// generate some fantom methods
generateFantomMethod(element);

// call fantom methods in random places
addRandomCalls(element, previous);

new ParentUpdater().scan(element);
}

private void generateFantomMethod(CtClass<?> element) {
FAKE_METHODS.times {
def name;
loop {
name = generateName();
} until { element.getMethod(name) == null && !fantoms.contains(name) }

CtMethod<?> method = new CtMethodImpl<>(
factory: getFactory(),
parent: element,
simpleName: name,
body: new CtBlockImpl(statements: [new CtCodeSnippetStatementImpl(value: "return \"\"")]),
type: factory.Type().createReference(Object.class)
);
fantoms.add(method.getSimpleName());
element.addMethod(method);
}
}

private void addRandomCalls(CtClass<?> element, List<CtMethod<?>> methods) {
if (methods.isEmpty()) return;

def names = [];
RANDOM_CALLS.times {
def methodName = fantoms.get(random.nextInt(fantoms.size()));
def method = element.getMethod(methodName);

// generate a variable name
def name;
loop {
name = generateName();
} until { !names.contains(name) }
names.add(name);

def localVariable = new CtLocalVariableImpl(
type: factory.Type().createReference(Object.class),
name: name,
defaultExpression: new CtInvocationImpl(executable: method.getReference()));

// insert at a random point
CtMethod insertMethod = methods[random.nextInt(methods.size())];

if (!insertMethod.hasModifier(ModifierKind.STATIC) && insertMethod.getBody() != null && !insertMethod.getBody().statements.isEmpty())
insertMethod.getBody().statements.add(random.nextInt(insertMethod.getBody().statements.size()), localVariable);
}
}

private String generateName() {
actionDictionary.get(random.nextInt(actionDictionary.size())) +
objectDictionary.get(random.nextInt(objectDictionary.size()));
}
}
28 changes: 28 additions & 0 deletions src/main/groovy/fr/inria/diversify/InvertIfProcessor.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fr.inria.diversify

import spoon.processing.AbstractProcessor
import spoon.reflect.code.CtIf
import spoon.reflect.code.CtStatement
import spoon.reflect.code.UnaryOperatorKind
import spoon.support.reflect.code.CtBlockImpl
import spoon.support.reflect.code.CtUnaryOperatorImpl

/**
* Created by nicolas on 21/08/2015.
*/
public class InvertIfProcessor extends AbstractProcessor<CtIf> {
@Override
public void process(CtIf ctIf) {
// inverse conditions
ctIf.setCondition(new CtUnaryOperatorImpl<Boolean>(kind: UnaryOperatorKind.NOT, operand: ctIf.getCondition()));

// invert then and else
CtStatement th = ctIf.getThenStatement();
CtStatement el = ctIf.getElseStatement();

ctIf.setThenStatement(el != null ? el : new CtBlockImpl<>());
ctIf.setElseStatement(th);

new ParentUpdater().scan(ctIf);
}
}
30 changes: 30 additions & 0 deletions src/main/groovy/fr/inria/diversify/ParentUpdater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.inria.diversify;

import spoon.reflect.declaration.CtElement;
import spoon.reflect.visitor.CtScanner;

import java.util.Stack;

/**
* Created by nicolas on 21/08/2015.
*/
public class ParentUpdater extends CtScanner {

private Stack<CtElement> elements = new Stack<CtElement>();

@Override
protected void enter(CtElement e) {
if (!elements.isEmpty()) {
e.setParent(elements.peek());
}
elements.push(e);
super.enter(e);
}

@Override
protected void exit(CtElement e) {
elements.pop();

super.exit(e);
}
}
39 changes: 39 additions & 0 deletions src/main/groovy/fr/inria/diversify/VariableDeclaration.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fr.inria.diversify

import spoon.processing.AbstractProcessor
import spoon.reflect.code.CtAssignment
import spoon.reflect.code.CtBlock
import spoon.reflect.code.CtLocalVariable
import spoon.reflect.code.CtStatement
import spoon.reflect.declaration.CtVariable
import spoon.reflect.declaration.ModifierKind

/**
* Created by nicolas on 24/08/2015.
*/
class VariableDeclaration extends AbstractProcessor<CtBlock<?>> {

@Override
public void process(CtBlock<?> ctBlock) {
for (CtStatement stmt : new ArrayList<CtStatement>(ctBlock.getStatements())) {
if (stmt instanceof CtVariable) {
CtLocalVariable variable = (CtLocalVariable) stmt;

if (variable.getDefaultExpression() != null) {
CtAssignment assignment = getFactory().Code()
.createVariableAssignment(variable.getReference(), false,
variable.getDefaultExpression());
variable.insertAfter(assignment);

if (variable.hasModifier(ModifierKind.FINAL) || variable.getType().isPrimitive()) {
variable.setDefaultExpression(null);
} else {
variable.setDefaultExpression(getFactory().Code().createLiteral(null));
}
}
}
}

new ParentUpdater().scan(ctBlock);
}
}
19 changes: 19 additions & 0 deletions src/main/groovy/fr/inria/diversify/utils/Looper.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fr.inria.diversify.utils

/**
* Created by nicolas on 25/08/2015.
*/
class Looper {
private Closure code

static Looper loop( Closure code ) {
new Looper(code:code)
}

void until( Closure test ) {
code()
while (!test()) {
code()
}
}
}
Loading

0 comments on commit bc5b322

Please sign in to comment.