Home | History | Annotate | Download | only in target
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package vogar.target;
     18 
     19 import java.lang.reflect.Method;
     20 import vogar.Result;
     21 import vogar.monitor.TargetMonitor;
     22 
     23 /**
     24  * Runs a Java class with a main method. This includes jtreg tests.
     25  */
     26 public final class MainTargetRunner implements TargetRunner {
     27 
     28     private final TargetMonitor monitor;
     29     private final Class<?> mainClass;
     30     private final String[] args;
     31     private final Method main;
     32 
     33     public MainTargetRunner(TargetMonitor monitor, Class<?> mainClass, String[] args) {
     34         this.monitor = monitor;
     35         this.mainClass = mainClass;
     36         this.args = args;
     37         try {
     38             this.main = mainClass.getMethod("main", String[].class);
     39         } catch (NoSuchMethodException e) {
     40             // Don't create a MainRunner without first checking supports().
     41             throw new IllegalArgumentException(e);
     42         }
     43     }
     44 
     45     public boolean run(Profiler profiler) {
     46         monitor.outcomeStarted(mainClass.getName() + "#main");
     47         try {
     48             if (profiler != null) {
     49                 profiler.start();
     50             }
     51             main.invoke(null, new Object[] { args });
     52             monitor.outcomeFinished(Result.SUCCESS);
     53         } catch (Throwable ex) {
     54             ex.printStackTrace();
     55             monitor.outcomeFinished(Result.EXEC_FAILED);
     56         } finally {
     57             if (profiler != null) {
     58                 profiler.stop();
     59             }
     60         }
     61         return true;
     62     }
     63 }
     64