说明:

因为上个代码,总是要输入完整的绝对路径,比较麻烦,于是,就写了这个小程序,直接进入文件对话框选择需要运行的class文件。

只需要提前输入完整的类名。

注意:加的MyTest必须打个包,加上:

import cn.hncu.myJuniitApp.vo.MyTest;

不然不是同一个注解呢。

测试的类:

package cn.hncu.myJuniitApp;

import cn.hncu.myJuniitApp.vo.MyTest;

public class MyJunitTest {

    public void run1(){
System.out.println("run1().....");
} @MyTest
public void run2(){
System.out.println("run2().....含有MyTest");
} public void run3(){
System.out.println("run3().....");
} @MyTest
public void run4(){
System.out.println("run4().....含有MyTest");
} public void run5(){
System.out.println("run5().....");
} }

然后是注解类:

package cn.hncu.myJuniitApp.vo;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; //运行时也存在的,如果想看到运行结果,必须定义这个注解的保持性为运行时
@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.METHOD)//限制这个注解只能用在方法上面
public @interface MyTest {
}

数据层:

接口:

package cn.hncu.myJuniitApp.dao.dao;

/**
* 数据层接口
* @author 陈浩翔
*
* @version 1.0 2016-5-6
*/
public interface JunitDao {
public Class<?> findClass(String name,String className);
}

实现类:

package cn.hncu.myJuniitApp.dao.impl;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import cn.hncu.myJuniitApp.dao.dao.JunitDao;
/**
* 数据层的实现类
* @author 陈浩翔
*
* @version 1.0 2016-5-6
*/
public class JunitDaoImpl extends ClassLoader implements JunitDao{ @Override
public Class<?> findClass(String name, String className) {
byte buf[] = loadClassData(name);
Class c = defineClass(className, buf, 0, buf.length);
return c;
} private byte[] loadClassData(String name) {
byte buf[]=null;
try {
FileInputStream in = new FileInputStream(name); ByteArrayOutputStream bout = new ByteArrayOutputStream();
int len=0;
byte[] b = new byte[1024];
while((len=in.read(b))!=-1){
bout.write(b, 0, len);
}
in.close();
bout.close();
buf=bout.toByteArray(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buf;
} }

工厂方法:

package cn.hncu.myJuniitApp.dao.factory;

import cn.hncu.myJuniitApp.dao.dao.JunitDao;
import cn.hncu.myJuniitApp.dao.impl.JunitDaoImpl; /**
* 数据层的工厂方法
* @author 陈浩翔
*
* @version 1.0 2016-5-6
*/
public class JunitDaoFactory {
public static JunitDao getJunitDao(){
return new JunitDaoImpl();
}
}

逻辑层:

接口:

package cn.hncu.myJuniitApp.business.ebi;

/**
* 逻辑层接口
* @author 陈浩翔
*
* @version 1.0 2016-5-6
*/
public interface JunitEbi {
public void run(String name,String className);
}

实现类:

package cn.hncu.myJuniitApp.business.ebo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import cn.hncu.myJuniitApp.business.ebi.JunitEbi;
import cn.hncu.myJuniitApp.dao.factory.JunitDaoFactory;
import cn.hncu.myJuniitApp.vo.MyTest;
/**
* 逻辑层的实现类
* @author 陈浩翔
*
* @version 1.0 2016-5-6
*/
public class JunitEbo implements JunitEbi{ //通过数据层获得对象的Class
@Override
public void run(String name, String className) {
Class c = JunitDaoFactory.getJunitDao().findClass(name, className);
try {
Object obj = c.newInstance();
Method ms[] = c.getDeclaredMethods();//获得当前类的所有声明方法,包括私有的
for(Method m : ms){//增强for循环遍历
if(m.isAnnotationPresent(MyTest.class)){//如果这个方法有这个注解,就运行
m.invoke(obj, null);//现在知道为什么要无参了吧,这样方便很多,直接就可以调用了。
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

工厂方法:

package cn.hncu.myJuniitApp.business.factory;

import cn.hncu.myJuniitApp.business.ebi.JunitEbi;
import cn.hncu.myJuniitApp.business.ebo.JunitEbo; /**
* 逻辑层的工厂方法
* @author 陈浩翔
*
* @version 1.0 2016-5-6
*/
public class JunitEbiFactory {
public static JunitEbi getJunitEbi(){
return new JunitEbo();
}
}

界面和main方法:

package cn.hncu.myJuniitApp;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File; import javax.swing.JFileChooser;
import javax.swing.JOptionPane; import cn.hncu.myJuniitApp.business.factory.JunitEbiFactory; /**
*
* @author 陈浩翔
* @version 1.0 2016-5-6
*/
public class myJunit extends javax.swing.JFrame { public myJunit() {
initComponents();
} private void initComponents() {
jLabel1 = new javax.swing.JLabel();
tfdClassName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
btnFileName = new javax.swing.JButton();
btnRun = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); //获得系统屏幕分辨率
Toolkit t = Toolkit.getDefaultToolkit() ;
Dimension size=t.getScreenSize(); setBounds((int)size.getWidth()/4,(int)size.getHeight()/4,400,400);
getContentPane().setLayout(null); jLabel1.setFont(new java.awt.Font("Dialog", 1, 30));
jLabel1.setForeground(new java.awt.Color(204, 0, 0));
jLabel1.setText("MyJunit-\u8fd0\u884c\u4efb\u610f\u7a7a\u53c2\u65b9\u6cd5");
getContentPane().add(jLabel1);
jLabel1.setBounds(10, 20, 380, 80); tfdClassName.setFont(new java.awt.Font("Dialog", 1, 18));
tfdClassName.setForeground(new java.awt.Color(255, 0, 0));
getContentPane().add(tfdClassName);
tfdClassName.setBounds(30, 180, 350, 40); jLabel2.setFont(new java.awt.Font("Dialog", 1, 18));
jLabel2.setForeground(new java.awt.Color(0, 0, 255));
jLabel2.setText("请先输入完整类名(再选择文件)");
getContentPane().add(jLabel2);
jLabel2.setBounds(30, 120, 310, 80); btnFileName.setFont(new java.awt.Font("Dialog", 1, 18));
btnFileName.setText("\u9009\u62e9\u6587\u4ef6");
btnFileName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFileNameActionPerformed(evt);
}
});
getContentPane().add(btnFileName);
btnFileName.setBounds(40, 270, 110, 50); btnRun.setFont(new java.awt.Font("Dialog", 1, 18));
btnRun.setText("\u8fd0\u884c");
btnRun.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRunActionPerformed(evt);
}
});
getContentPane().add(btnRun);
btnRun.setBounds(240, 270, 110, 50);
} private void btnRunActionPerformed(java.awt.event.ActionEvent evt) {
if(fileName==null){
JOptionPane.showMessageDialog(this, "请先选择文件!");
}
JunitEbiFactory.getJunitEbi().run(fileName, className);
} private void btnFileNameActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser();
//打开文件选择对话框
int result = jfc.showOpenDialog(this);
File file=null;
if(result==JFileChooser.APPROVE_OPTION){//选择了文件
file=jfc.getSelectedFile();//获得选择的文件
//System.out.println(file.getParent());
//System.out.println(file.getName());
fileName = file.getParent()+"\\"+file.getName();
className = tfdClassName.getText().trim();
if("".equals(className)){
JOptionPane.showMessageDialog(this, "请输入正确的完整类名!!!");
return ;
}
//System.out.println(fileName);
//System.out.println(className);
}
} public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new myJunit().setVisible(true);
}
});
} private javax.swing.JButton btnFileName;//选择文件的按钮
private javax.swing.JButton btnRun;//运行含有@MyTest的空参方法
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField tfdClassName;
private String fileName =null;
private String className =null; }

下面看看一些程序的图片:

先看下运行结果吧:我把那个测试类移到了d盘去了的。

run2().....含有MyTest
run4().....含有MyTest

也许有人认为一个类完全就可以解决这个问题了,你还写这么多的包,这么多类,这不是麻烦嘛。

我想说,虽然麻烦,但是可以让我们看起来结构清楚,谁调用谁也很明白,而且是面向接口编程。

这个写得更加规范,特别是以后到公司工作,基本上都是几个人开发同一个项目的,这个人写这里的,那个人写那里的,如果没有规范,怎么行呢。怎么合作开发同一个项目呢。

所以,分包是必须的。分逻辑层和数据层也是需要的。特别是一个层的铁三角,必须都要有。

接口,工厂方法,实现类,缺一不可!!!

Java---实现运行任意目录下class中加了@MyTest的空参方法(实现图形界面)的更多相关文章

  1. Java---注解、类加载器-加强-实现运行任意目录下class中加了@MyTest的空参方法

    做自己的类加载器 虚拟机的核心是通过类加载器来加载.class文件,然后进行相应的解析执行.那么我们可以自己做类加载器,手动加载需要的.class以进行解析执行,从而扩展虚拟机的功能. 以下内容摘自A ...

  2. 如何配置任意目录下Web应用程序

    1,首先创建一个Web项目,tomcat 7, JDK 1.8 2,创建Web项目并部署到tomcat服务器下运行的步骤和方法: 在Eclipse下创建一个JAVA project 在JAVA项目下创 ...

  3. 【转】链接任意目录下库文件(解决错误“/usr/bin/ld: cannot find -lxxx”

    netbeans构建项目也出现了同样的问题.猜测是netbeans内部就用的是-l 这种编译方式,所以需要把***.a手动改为lib***.a 原文地址:链接任意目录下库文件(解决错误“/usr/bi ...

  4. Node.js在任意目录下使用express命令‘不是内部或外部命令’解决方法

    1.一开始我只能在nodejs全局目录下使用express命令建一个新的项目,建在其他任意一个目录命令行都会提示"不是内部或外部命令",导致目录会乱,目录如下. 2.尝试了一会,发 ...

  5. java获取ubuntu某个目录下的所有文件信息

    java获取ubuntu某个目录下的所有文件信息 public List<VCFile> getAllFiles(String basicDirName) { List<VCFile ...

  6. eclipse新建web项目,运行后在tomcat安装目录下webapps中没有该项目

    一.发现问题在eclipse中新建Dynamic Web Project,配置好本地的tomcat并写好代码后选择Run on Server,但运行后发现在tomcat的安装目录下的webapps并没 ...

  7. Apache 优雅重启 Xampp开机自启 - 【环境变量】用DOS命令在任意目录下启动服务

    D:\xampp\apache\bin\httpd.exe" -k runservice Apache 优雅重启 :httpd -k graceful Xampp开机自启动  参考文献:ht ...

  8. 用Java代码列出一个目录下所有的文件

    1.File类 File类在java.io.File包中,所以要导入这个包. File类中用到的方法: boolean isDirectory()       测试此抽象路径名表示的文件是否是个目录 ...

  9. java 实现读取某个目录下指定类型的文件

    我这里是读取txt类型的文件,在指定的目录下有不同类型的文件 实现代码,读取txt类型的文件并打印出该文件的绝对路径 package com.SBgong.test; import java.io.F ...

随机推荐

  1. 安装Visual Studio 2010时提示"The location specified for the help content store is invalid or you do not have access to it".

    运行注册表: (运行->输入"regedit").在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Help\v1.0中,删除"Loc ...

  2. install erlang environment on centos

    #(erlide in linux can't detect the runtime if build from source, but erlang shell works correctly)su ...

  3. 进程识别号(PID)的理解

    PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...

  4. Objective-C和C++的区别

    1.都是有C语言延伸而来2.OC是完全动态的,C++是部分动态的3.OC不支持多继承,通过代理 类别 协议优雅的实现了相关的一系列特性4.调用机制不同OC里面叫发送消息  C++叫做调用函数数5.OC ...

  5. JavaScript HTML DOM 事件

    JavaScript HTML DOM 事件 HTML DOM 使 JavaScript 有能力对 HTML 事件做出反应. 实例 Mouse Over Me 对事件做出反应 我们可以在事件发生时执行 ...

  6. Jmeter软件测试2--http接口测试

    上次利用Jmeter进行了webservice接口的测试,本次利用Jmeter进行http接口的测试 1.新建线程组 2.新建配置文件 3.新建http请求 4.配置动态请求 4.查看测试结果

  7. 理解pkg-config工具

    你在 Unix 或 Linux 下开发过软件吗?写完一个程序,编译运行完全正常,在你本机上工作得好好的,你放到源代码管理系统中.然后,告诉你的同事说,你可以取下来用了.这时,你长长的出了一口气,几天的 ...

  8. 初涉JavaScript模式 (1) : 简介

    什么是模式? 广义上的模式是指 :在物体或事件上,产生的一种规律变化与自我重复的样式与过程.在模式之中,某些固定的元素不断以可预测的方式周期性重现.最基本而常见的模式,称为密铺,具备重复性以及周期性两 ...

  9. vertical-align:top属性

    vertical-align这个是设置元素的垂直排列的. 用来定义行内元素的基线相对于该元素所在行的基线的垂直对齐. 它的值比较多:baseline | sub | super | top | tex ...

  10. Inter系列处理器名称浅析

    东拼西凑之作,仅仅整理而已,望周知 ------------------------------------------------------------------ 举例 CPU酷睿i5-3230 ...