Commit 677d8787 authored by Philippe Merle's avatar Philippe Merle

* Improved the code quality according to Sonar's checking rules:

  - removed unused import statements.
  - put static final fields in uppercase.
  - added 'final' to not overridden methods.
  - moved from protected to private fields.
  - added brackets to if/else/do/for statements.
  - removed public to interfaces' methods.
* Replaced "USTL" by "University of Lille 1".
* Cosmetic updates.
parent 2f08ab06
/***
/**
* OW2 FraSCAti FScript
* Copyright (C) 2009 INRIA, University of Lille 1
* Copyright (C) 2009-2010 INRIA, University of Lille 1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -20,7 +20,8 @@
*
* Author: Christophe Demarey
*
* Contributor(s):
* Contributor(s): Philippe Merle
*
*/
package org.ow2.frascati.fscript.console;
......@@ -34,14 +35,16 @@ import org.objectweb.fractal.fscript.jsr223.InvocableScriptEngine;
import org.ow2.frascati.fscript.FraSCAtiFScript;
import org.ow2.frascati.fscript.jsr223.FraSCAtiScriptEngineFactory;
/**
* Main class to launch the FraSCAtiScript console.
*
* @author Christophe Demarey.
*/
public class Main extends org.objectweb.fractal.fscript.console.Main {
public static void main(String[] args) throws Exception {
public class Main
extends org.objectweb.fractal.fscript.console.Main
{
public static void main(String[] args) throws Exception
{
FraSCAtiScriptEngineFactory factory = new FraSCAtiScriptEngineFactory();
// Instantiate the engine
Component fscriptCmpt = ((Interface) factory.getScriptEngine()).getFcItfOwner();
......
/***
/**
* OW2 FraSCAti FScript
* Copyright (C) 2009 INRIA, University of Lille 1
* Copyright (C) 2009-2010 INRIA, University of Lille 1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -20,7 +20,8 @@
*
* Author: Christophe Demarey
*
* Contributor(s):
* Contributor(s): Philippe Merle
*
*/
package org.ow2.frascati.fscript.console;
......@@ -61,7 +62,9 @@ import com.google.common.collect.Lists;
*
* @author Christophe Demarey
*/
public class TextConsole extends org.objectweb.fractal.fscript.console.TextConsole {
public class TextConsole
extends org.objectweb.fractal.fscript.console.TextConsole
{
/** The console context. */
private Bindings bindings = null;
......@@ -71,7 +74,8 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
* @param fscript The FScript implementation
* @throws IOException
*/
public TextConsole(Component fscript) throws IOException {
public TextConsole(Component fscript) throws IOException
{
this(fscript, new SimpleBindings());
}
......@@ -81,7 +85,8 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
* @param ctx The context to use for this console.
* @throws IOException
*/
public TextConsole(Component fscript, Bindings ctx) throws IOException {
public TextConsole(Component fscript, Bindings ctx) throws IOException
{
Preconditions.checkNotNull(fscript);
this.fscript = fscript;
this.bindings = ctx;
......@@ -104,11 +109,13 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
* Get the command context that will be used to execute this command.
* @return the command context.
*/
public Bindings getContext() {
public final Bindings getContext()
{
return this.bindings;
}
protected void registerCommands() {
protected final void registerCommands()
{
// Internal commands
register(new HelpCommand());
......@@ -144,53 +151,66 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
}
}
protected void register(Command cmd) {
protected final void register(Command cmd)
{
cmd.setSession(this);
cmd.setFScriptEngine(fscript);
this.commands.put(cmd.getName(), cmd);
}
private class QuitCommand extends AbstractCommand {
private class QuitCommand
extends AbstractCommand
{
@Override
public String getName() {
public final String getName()
{
return "quit";
}
@Override
public String getShortDescription() {
public final String getShortDescription()
{
return "Quit the console.";
}
@Override
public String getLongDescription() {
public final String getLongDescription()
{
return getShortDescription();
}
public void execute(String args) throws Exception {
public final void execute(String args) throws Exception
{
TextConsole.this.finished = true;
}
}
protected class HelpCommand extends AbstractCommand {
protected class HelpCommand
extends AbstractCommand
{
public HelpCommand() {};
@Override
public String getName() {
public final String getName()
{
return "help";
}
@Override
public String getShortDescription() {
public final String getShortDescription()
{
return "Shows a list of available commands.";
}
@Override
public String getLongDescription() {
public final String getLongDescription()
{
return super.getShortDescription();
}
public void execute(String args) throws Exception {
public final void execute(String args) throws Exception
{
if (args.length() == 0) {
listAvailableCommands();
} else {
......@@ -204,7 +224,8 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
}
}
private void listAvailableCommands() {
private void listAvailableCommands()
{
showTitle("Available commands");
showMessage("Type ':help <cmd>' for more details on a specific command.");
newline();
......@@ -212,7 +233,8 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
List<Command> sortedCmds = Lists.newArrayList(commands.values());
Collections.sort(sortedCmds, Comparators
.fromFunction(new Function<Command, String>() {
public String apply(Command cmd) {
public final String apply(Command cmd)
{
return cmd.getName();
}
}));
......@@ -230,16 +252,19 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
}
}
protected class Request {
protected class Request
{
private final Command command;
private final String arguments;
public Request(String commandName, String arguments) {
public Request(String commandName, String arguments)
{
this.command = commands.get(commandName);
this.arguments = arguments;
}
public void execute() throws Exception {
public final void execute() throws Exception
{
if (command != null) {
command.execute(arguments);
} else {
......@@ -248,7 +273,8 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
}
}
protected Request parseRequest(String line) {
protected final Request parseRequest(String line)
{
if (line == null) {
return new Request("quit", "");
} else if (line.startsWith(":")) {
......@@ -265,24 +291,28 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
}
}
public void setFScriptEngine(Component newEngine) {
public final void setFScriptEngine(Component newEngine)
{
this.fscript = newEngine;
for (Command cmd : commands.values()) {
cmd.setFScriptEngine(this.fscript);
}
}
public void setSessionInterpreter(Component fscript) {
public final void setSessionInterpreter(Component fscript)
{
for (Command cmd : commands.values()) {
cmd.setFScriptEngine(fscript);
}
}
public DiagnosticListener getDiagnosticListener() {
public final DiagnosticListener getDiagnosticListener()
{
return new SessionDiagnosticListener(this);
}
public void processRequest(String line) throws Exception {
public final void processRequest(String line) throws Exception
{
if (line != null && line.length() == 0) {
return;
}
......@@ -292,12 +322,14 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
}
}
public Set<String> getGlobalVariablesNames() {
public final Set<String> getGlobalVariablesNames()
{
FScriptEngine engine = FScript.getFScriptEngine(fscript);
return engine.getGlobals();
}
public Set<String> getAxesNames() {
public final Set<String> getAxesNames()
{
Model model = getModel();
if (model != null) {
Set<Axis> axes = model.getAxes();
......@@ -311,7 +343,8 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
}
}
protected Model getModel() {
protected final Model getModel()
{
try {
for (Component c : Fractal.getContentController(fscript).getFcSubComponents()) {
String name = Fractal.getNameController(c).getFcName();
......@@ -327,7 +360,8 @@ public class TextConsole extends org.objectweb.fractal.fscript.console.TextConso
}
}
public Set<String> getCommandNames() {
public final Set<String> getCommandNames()
{
return commands.keySet();
}
}
/***
/**
* OW2 FraSCAti FScript
* Copyright (C) 2010 INRIA, University of Lille 1
*
......@@ -21,7 +21,7 @@
* Author: Pierre-Charles David
* Christophe Demarey
*
* Contributor(s):
* Contributor(s): Philippe Merle
*/
package org.ow2.frascati.fscript.console.commands;
......@@ -45,7 +45,9 @@ import com.google.common.base.Preconditions;
/**
* Common abstract base class for all the console's special commands.
*/
public abstract class AbstractCommand implements Command, Session {
public abstract class AbstractCommand
implements Command, Session
{
/** The command's name. */
private String name;
......@@ -69,8 +71,9 @@ public abstract class AbstractCommand implements Command, Session {
// Private methods
// ========================================================================
protected void ensureComponentIsStarted(Component comp)
throws IllegalLifeCycleException {
protected final void ensureComponentIsStarted(Component comp)
throws IllegalLifeCycleException
{
try {
LifeCycleController lcc = Fractal.getLifeCycleController(comp);
if (!"STARTED".equals(lcc.getFcState())) {
......@@ -89,7 +92,8 @@ public abstract class AbstractCommand implements Command, Session {
* Get the command context that will be used to execute this command.
* @return the command context.
*/
protected Bindings getContext() {
protected final Bindings getContext()
{
return ((TextConsole) session).getContext();
}
......@@ -97,15 +101,18 @@ public abstract class AbstractCommand implements Command, Session {
// Implementation of the Command API
// ========================================================================
public String getName() {
public String getName()
{
return name;
}
public void setName(String name) {
public final void setName(String name)
{
this.name = name;
}
public void setShortDescription(String desc) {
public final void setShortDescription(String desc)
{
if (desc != null) {
this.shortDescription = desc;
} else {
......@@ -113,11 +120,13 @@ public abstract class AbstractCommand implements Command, Session {
}
}
public String getShortDescription() {
public String getShortDescription()
{
return this.shortDescription;
}
public void setLongDescription(String desc) {
public final void setLongDescription(String desc)
{
if (desc != null) {
this.longDescription = desc;
} else {
......@@ -125,11 +134,13 @@ public abstract class AbstractCommand implements Command, Session {
}
}
public String getLongDescription() {
public String getLongDescription()
{
return longDescription;
}
public void setFScriptEngine(Component newFscript) {
public final void setFScriptEngine(Component newFscript)
{
// parameter not used but need to be compliant with FScript API
this.fscript = FraSCAtiFScript.getSingleton();
this.engine = fscript.getScriptEngine();
......@@ -139,55 +150,67 @@ public abstract class AbstractCommand implements Command, Session {
// Implementation of the Session API
// ========================================================================
public void setSession(Session session) {
public final void setSession(Session session)
{
Preconditions.checkNotNull(session, "Null session");
this.session = session;
}
@Override
public String toString() {
public final String toString()
{
return ":" + getName();
}
// Delegate Session implementation to the current session
public void setSessionInterpreter(Component fscript) {
public final void setSessionInterpreter(Component fscript)
{
session.setSessionInterpreter(fscript);
}
public void showMessage(String message) {
public final void showMessage(String message)
{
session.showMessage(message);
}
public void showResult(Object result) {
public final void showResult(Object result)
{
session.showResult(result);
}
public void showWarning(String warning) {
public final void showWarning(String warning)
{
session.showWarning(warning);
}
public void showError(String error) {
public final void showError(String error)
{
session.showError(error);
}
public void showError(String error, Throwable cause) {
public final void showError(String error, Throwable cause)
{
session.showError(error, cause);
}
public void showTitle(String title) {
public final void showTitle(String title)
{
session.showTitle(title);
}
public void newline() {
public final void newline()
{
session.newline();
}
public void showTable(String[][] table) {
public final void showTable(String[][] table)
{
session.showTable(table);
}
public DiagnosticListener getDiagnosticListener() {
public final DiagnosticListener getDiagnosticListener()
{
return session.getDiagnosticListener();
}
}
/***
/**
* OW2 FraSCAti FScript
* Copyright (C) 2010 INRIA, University of Lille 1
*
......@@ -20,7 +20,8 @@
*
* Author: Christophe Demarey
*
* Contributor(s):
* Contributor(s): Philippe MErle
*
*/
package org.ow2.frascati.fscript.console.commands;
......@@ -43,14 +44,15 @@ import org.objectweb.fractal.util.Fractal;
* @author Christophe Demarey
*/
public class ClassPathAddCommand
extends AbstractCommand {
extends AbstractCommand
{
/**
* Adds an URL at the class-path used by FraSCAtiScript.
*
* @param arg An URL defining a valid path or jar file.
*/
public void execute(String name) throws Exception {
public final void execute(String name) throws Exception
{
URL url = null;
try {
url = new URL(name);
......@@ -72,12 +74,14 @@ public class ClassPathAddCommand
fscript.getClassLoaderManager().loadLibraries(urls);
urls = ((URLClassLoader) cl).getURLs();
for(URL u : urls)
for(URL u : urls) {
showMessage(u.toString());
}
showMessage("Classpath updated.");
}
protected Map<String, Object> getInstanciationContext() throws Exception {
protected final Map<String, Object> getInstanciationContext() throws Exception
{
ContentController cc = Fractal.getContentController( fscript.getFraSCAtiScriptComposite() );
for (Component child : cc.getFcSubComponents()) {
......
/***
/**
* OW2 FraSCAti FScript
* Copyright (C) 2010 INRIA, University of Lille 1
*
......@@ -20,7 +20,7 @@
*
* Author: Christophe Demarey
*
* Contributor(s):
* Contributor(s): Philippe Merle
*/
package org.ow2.frascati.fscript.console.commands;
......@@ -28,8 +28,11 @@ package org.ow2.frascati.fscript.console.commands;
/**
* This command evaluates an FPath expression and prints the result.
*/
public class EvalCommand extends AbstractCommand {
public void execute(String expression) throws Exception {
public class EvalCommand
extends AbstractCommand
{
public final void execute(String expression) throws Exception
{
Object result = engine.eval(expression, getContext());
showResult(result);
}
......
/***
/**
* OW2 FraSCAti FScript
* Copyright (C) 2010 INRIA, University of Lille 1
*
......@@ -20,7 +20,8 @@
*
* Author: Christophe Demarey
*
* Contributor(s):
* Contributor(s): Philippe Merle
*
*/
package org.ow2.frascati.fscript.console.commands;
......@@ -28,8 +29,11 @@ package org.ow2.frascati.fscript.console.commands;
/**
* This command executes an FScript statement and prints the result.
*/
public class ExecCommand extends AbstractCommand {
public void execute(String statement) throws Exception {
public class ExecCommand
extends AbstractCommand
{
public final void execute(String statement) throws Exception
{
long start = System.currentTimeMillis();
Object result = engine.eval(statement, getContext());
long duration = System.currentTimeMillis() - start;
......
/***
/**
* OW2 FraSCAti FScript
* Copyright (C) 2010 INRIA, University of Lille 1
*
......@@ -21,6 +21,8 @@
* Author: Pierre-Charles David
*
* Contributor(s): Christophe Demarey
* Philippe Merle
*
*/
package org.ow2.frascati.fscript.console.commands;
......@@ -37,8 +39,11 @@ import org.objectweb.fractal.util.ContentControllerHelper;
* This command prints out information about the procedure whose name is given in
* parameter.
*/
public class InfoCommand extends AbstractCommand {
public void execute(String procName) throws Exception {
public class InfoCommand
extends AbstractCommand
{
public final void execute(String procName) throws Exception
{
Component lib = ContentControllerHelper.getSubComponentByName(fscript.getFraSCAtiScriptComposite(), "library");
if (lib == null) {
showWarning("Unable to locate the library component.");
......
/***
/**
* OW2 FraSCAti FScript
* Copyright (C) 2010 INRIA, University of Lille 1
*
......@@ -21,6 +21,8 @@
* Author: Pierre-Charles David
*
* Contributor(s): Christophe Demarey
* Philippe Merle
*
*/
package org.ow2.frascati.fscript.console.commands;
......@@ -41,9 +43,11 @@ import org.objectweb.fractal.util.ContentControllerHelper;
*
* @author Pierre-Charles David
*/
public class ListCommand extends AbstractCommand {
public void execute(String args) throws Exception {
public class ListCommand
extends AbstractCommand
{
public final void execute(String args) throws Exception
{
Component lib = ContentControllerHelper.getSubComponentByName(fscript.getFraSCAtiScriptComposite(), "library");
if (lib == null) {
showWarning("Unable to locate the library component.");
......@@ -70,7 +74,8 @@ public class ListCommand extends AbstractCommand {
showTable(data);
}
private void fillProcData(Procedure proc, String[] row) {
private void fillProcData(Procedure proc, String[] row)
{
row[0] = proc.getName() + proc.getSignature().toString();
row[1] = proc.isPureFunction() ? "Function" : "Action";
if (proc instanceof NativeProcedure) {
......
/***
/**
* OW2 FraSCAti FScript
* Copyright (C) 2010 INRIA, University of Lille 1
*
......@@ -21,6 +21,8 @@
* Author: Pierre-Charles David
*
* Contributor(s): Christophe Demarey
* Philippe Merle
*
*/
package org.ow2.frascati.fscript.console.commands;
......@@ -37,15 +39,17 @@ import java.net.URL;
/**
* This command loads new FScript procedures definitions from a file.
*/
public class LoadCommand extends AbstractCommand {
public void execute(String args) throws Exception {
public class LoadCommand
extends AbstractCommand
{
public final void execute(String args) throws Exception
{
Reader reader = null;
if (args.startsWith("classpath:"))
if (args.startsWith("classpath:")) {
reader = getResourceReader(args.substring("classpath:".length()));
else if (args.matches("^[a-z]+:.*"))