7
\$\begingroup\$

I'm modeling a reflection-based controller. I would like to know if you agree with my implementation and about what could be enhanced.

I'm starting with reflection and I would like to know if I'm using good practices.

public class StartController extends HttpServlet {
 @Override
 public void service(HttpServletRequest req, HttpServletResponse resp)
 throws ServletException, IOException
 {
 String vars = req.getParameter("mvc");
 ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
 MainController main = new MainController();
 main.RESTProcess(req, resp, context, vars);
 }
}
public class MainController extends Controller {
 public void RESTProcess(HttpServletRequest req, HttpServletResponse resp, ApplicationContext context, String vars)
 throws IOException, ServletException
 {
 String[] url;
 int nvars;
 String controller = null;
 String action = null;;
 String[] params = null;
 int i;
 int n;
 String controllerName;
 String actionName;
 if(vars == null || "".equals(vars.trim()))
 {
 this.redirect(resp,"home");
 }
 else
 { 
 url = vars.split("/");
 nvars = url.length;
 if(nvars > 0)
 {
 controller = url[0].trim(); //users
 if(nvars > 1)
 {
 action = url[1].trim(); //edit
 if(nvars > 2)
 {
 n = 0;
 params = new String[nvars - 2]; //array[0] = 5 (iduser)
 for(i = 2; i < nvars; i++)
 {
 params[n] = url[i];
 n++;
 }
 }
 }
 controllerName = this.getFirstUpper(controller) + "Controller"; //HomeController, UserController pattern
 if(!controllerName.equals("Controller"))
 {
 actionName = "action" + this.getFirstUpper(action); //actionIndex, actionEdit pattern
 if(!action.equals("action"))
 {
 try
 {
 Class classe = Class.forName("com.sample.controller." + controllerName);
 try
 {
 Constructor c = classe.getConstructor(HttpServletRequest.class, HttpServletResponse.class, String[].class);
 try
 {
 Object obj = c.newInstance(req, resp, context, params);
 Method m = classe.getMethod(actionName, null);
 m.invoke(obj, null);
 System.out.println(obj.toString());
 }
 catch (Exception e)
 {
 e.printStackTrace();
 }
 }
 catch (Exception e)
 {
 e.printStackTrace();
 }
 }
 catch (Exception e)
 {
 e.printStackTrace();
 }
 }
 }
 }
 else
 {
 this.redirect(resp,"home");
 }
 }
 }
 public String getFirstUpper(String str)
 {
 Integer x = str.length();
 str = str.substring(0,1).toUpperCase().concat(str.substring(1, x));
 return str;
 }
}
public class UserController extends Controller {
 private HttpServletRequest req;
 private HttpServletResponse resp;
 private ApplicationContext context;
 private String[] params;
 public UserController(HttpServletRequest req, HttpServletResponse resp, ApplicationContext context, String[] params)
 {
 this.req = req;
 this.resp = resp;
 this.context = context;
 this.params = params;
 }
 public void actionEdit()
 {
 Long id = Long.valueOf(this.params[0]);
 System.out.println("/home/edit");
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 26, 2012 at 17:30
\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

I would make some changes to getFirstUpper():

public String getFirstUpper(String str)
{
 Integer x = str.length();
 str = str.substring(0,1).toUpperCase().concat(str.substring(1, x));
 return str;
}

The name x is not a very good name as it's only a single character. I'm not even sure if a separate variable is needed for this, but if so, consider renaming it to stringLength. The type should also be int instead, which is the return type of String.length(). Either way, you can just pass length() directly into substring() (in place of x).

It's also unnecessary to create a str variable if the inner function's return value is just going to be returned right away. Plus, it's already known that this method is supposed to return a String.

return str.substring(0,1).toUpperCase().concat(str.substring(1, str.length()));

I also have a general comment regarding curly braces:

The Java standard dictates the use of "Egyptian braces," which involves putting the opening brace on the same line as the statement, rather than the next line by itself:

public String getFirstUpper(String str) {
 // ...
}
answered Aug 8, 2014 at 17:47
\$\endgroup\$
1
\$\begingroup\$

Only few general comments from me:

  1. Start your method names with lowercase.

  2. Remove excess try keywords.

  3. Catch only specific type of Exceptions!

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Oct 29, 2012 at 13:22
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Please elaborate a bit more on the reasoning behind your proposals. While they are all good, the motto of CR is "teach a man how to fish" and not "give a man a fish" ;) \$\endgroup\$ Commented Aug 8, 2014 at 18:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.