Commit 57ea4d8e authored by Daniel Ratiu's avatar Daniel Ratiu
Browse files

small refactorings

refs 883
parent 0c6f2a62
/*--------------------------------------------------------------------------+
$Id: CausalityUtils.java 2768 2012-01-12 18:03:35Z becker $
$Id: GeneratorUtils.java 2768 2012-01-12 18:03:35Z ratiu $
| |
| Copyright 2012 ForTISS GmbH |
| |
......@@ -17,66 +17,20 @@ $Id: CausalityUtils.java 2768 2012-01-12 18:03:35Z becker $
+--------------------------------------------------------------------------*/
package org.fortiss.af3.component.utils;
import static org.conqat.lib.commons.reflect.ReflectionUtils.listInstances;
import java.util.Collection;
import org.fortiss.af3.expression.model.terms.imperative.IStatementTerm;
import org.fortiss.af3.expression.model.terms.imperative.StatementSequence;
import org.fortiss.af3.generator.common.model.c.CFunctionDefinition;
import org.fortiss.af3.generator.common.model.source.SourcePackage;
import org.fortiss.af3.generator.common.model.source.SourceUnit;
/**
* Utility methods for the generator.
*
* @author hoelzl
* @author $Author: becker $
* @author $Author: ratiu $
* @version $Rev: 2768 $
* @ConQAT.Rating GREEN Hash: 7DDF710640EC4432DC0624D40E10B626
*/
public class GeneratorUtils {
/**
* Finds a source unit in the source package or in any of its sub-packages recursively.
*
* @param name
* the unit name.
* @return the unit or null if it does not exist.
*/
public static SourceUnit findSourceUnitByNameRecursively(SourcePackage pack, String name) {
for(SourceUnit su : pack.getUnitsList()) {
if(su.getName().equals(name)) {
return su;
}
}
for(SourcePackage subPack : pack.getSubPackagesList()) {
SourceUnit su = findSourceUnitByNameRecursively(subPack, name);
if(su != null) {
return su;
}
}
return null;
}
/**
* Finds a function declaration in the source unit.
*
* @param name
* the function name.
* @return the function or null if it does not exist.
*/
public static CFunctionDefinition findFunctionByName(SourceUnit unit, String name) {
Collection<CFunctionDefinition> functions =
listInstances(unit.getDefinitionsList(), CFunctionDefinition.class);
for(CFunctionDefinition fd : functions) {
if(fd.getName().equals(name)) {
return fd;
}
}
return null;
}
/**
* Appends a new statement to the body of this function.
*
......
......@@ -17,12 +17,16 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.af3.generator.common.utils;
import static org.conqat.lib.commons.reflect.ReflectionUtils.listInstances;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.fortiss.af3.generator.common.model.c.CFunctionDefinition;
import org.fortiss.af3.generator.common.model.source.FormattedSourceUnit;
import org.fortiss.af3.generator.common.model.source.SourcePackage;
import org.fortiss.af3.generator.common.model.source.SourceUnit;
......@@ -73,6 +77,48 @@ public class SourceUtils {
return null;
}
/**
* Finds a source unit in the source package or in any of its sub-packages recursively.
*
* @param pack
* the package name.
* @param name
* the unit name.
* @return the unit or null if it does not exist.
*/
public static SourceUnit findSourceUnitByNameRecursively(SourcePackage pack, String name) {
SourceUnit su = findSourceUnitByName(name, pack);
if(su != null) {
return su;
}
for(SourcePackage subPack : pack.getSubPackagesList()) {
su = findSourceUnitByNameRecursively(subPack, name);
if(su != null) {
return su;
}
}
return null;
}
/**
* Finds a function declaration in the source unit.
*
* @param name
* the function name.
* @return the function or null if it does not exist.
*/
public static CFunctionDefinition findFunctionByName(SourceUnit unit, String name) {
Collection<CFunctionDefinition> functions =
listInstances(unit.getDefinitionsList(), CFunctionDefinition.class);
for(CFunctionDefinition fd : functions) {
if(fd.getName().equals(name)) {
return fd;
}
}
return null;
}
/**
* Writes the given source package to the given location.
*
......
......@@ -23,10 +23,10 @@ import static org.conqat.lib.commons.filesystem.FileSystemUtils.deleteRecursivel
import static org.conqat.lib.commons.filesystem.FileSystemUtils.ensureDirectoryExists;
import static org.eclipse.emf.ecore.util.EcoreUtil.copy;
import static org.fortiss.af3.component.utils.GeneratorUtils.appendStatementToFunction;
import static org.fortiss.af3.component.utils.GeneratorUtils.findFunctionByName;
import static org.fortiss.af3.component.utils.GeneratorUtils.findSourceUnitByNameRecursively;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.funcCall;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.var;
import static org.fortiss.af3.generator.common.utils.SourceUtils.findFunctionByName;
import static org.fortiss.af3.generator.common.utils.SourceUtils.findSourceUnitByNameRecursively;
import static org.fortiss.af3.generator.common.utils.SourceUtils.findSubPackageByName;
import static org.fortiss.af3.testing.utils.TestingUtils.getTestOutputFor;
import static org.fortiss.af3.testing.validator.platform.generic.DeploymentGenerator.createDeploymentFor;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment