当前位置: 技术问答>java相关
如何判断一个函数是否存在?
来源: 互联网 发布时间:2015-04-18
本文导语: 是这样的。我用到一个函数是Servlet 2.3新增加的,2.2没有,我想编一个bean,使得如果存在着个函数,就用该函数,反之采取我自己的一个替代的处理过程。 可是俺是java新手,不知道怎样判断某个函数是否存在? 比...
是这样的。我用到一个函数是Servlet 2.3新增加的,2.2没有,我想编一个bean,使得如果存在着个函数,就用该函数,反之采取我自己的一个替代的处理过程。
可是俺是java新手,不知道怎样判断某个函数是否存在?
比如 request.setContentEncoding()
可是俺是java新手,不知道怎样判断某个函数是否存在?
比如 request.setContentEncoding()
|
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
public class MakeTodayClass {
Date today = new Date();
String todayMillis = Long.toString(today.getTime());
String todayClass = "z_" + todayMillis;
String todaySource = todayClass + ".java";
public static void main (String args[]){
MakeTodayClass mtc = new MakeTodayClass();
mtc.createIt();
if (mtc.compileIt()) {
System.out.println("Running " + mtc.todayClass + ":nn");
mtc.runIt();
}
else
System.out.println(mtc.todaySource + " is bad.");
}
public void createIt() {
try {
FileWriter aWriter = new FileWriter(todaySource, true);
//aWriter.write("package com;");
aWriter.write("public class "+ todayClass + "{");
aWriter.write(" public void doit() {");
aWriter.write(" System.out.println(""+todayMillis+"");");
aWriter.write(" }}n");
aWriter.flush();
aWriter.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public boolean compileIt() {
String [] source = {"-d","d:/",new String(todaySource) };
ByteArrayOutputStream baos= new ByteArrayOutputStream();
//new sun.tools.javac.Main(baos,source[0]).compile(source);
// if using JDK >= 1.3 then use
System.out.println(com.sun.tools.javac.Main.compile(source));
System.out.println("================"+new String(baos.toByteArray()));
return (baos.toString().indexOf("error")==-1);
}
public void runIt() {
try {
Class params[] = {};
Object paramsObj[] = {};
Class thisClass = Class.forName(todayClass);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("doit", params);
thisMethod.invoke(iClass, paramsObj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.*;
import java.lang.reflect.*;
public class MakeTodayClass {
Date today = new Date();
String todayMillis = Long.toString(today.getTime());
String todayClass = "z_" + todayMillis;
String todaySource = todayClass + ".java";
public static void main (String args[]){
MakeTodayClass mtc = new MakeTodayClass();
mtc.createIt();
if (mtc.compileIt()) {
System.out.println("Running " + mtc.todayClass + ":nn");
mtc.runIt();
}
else
System.out.println(mtc.todaySource + " is bad.");
}
public void createIt() {
try {
FileWriter aWriter = new FileWriter(todaySource, true);
//aWriter.write("package com;");
aWriter.write("public class "+ todayClass + "{");
aWriter.write(" public void doit() {");
aWriter.write(" System.out.println(""+todayMillis+"");");
aWriter.write(" }}n");
aWriter.flush();
aWriter.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public boolean compileIt() {
String [] source = {"-d","d:/",new String(todaySource) };
ByteArrayOutputStream baos= new ByteArrayOutputStream();
//new sun.tools.javac.Main(baos,source[0]).compile(source);
// if using JDK >= 1.3 then use
System.out.println(com.sun.tools.javac.Main.compile(source));
System.out.println("================"+new String(baos.toByteArray()));
return (baos.toString().indexOf("error")==-1);
}
public void runIt() {
try {
Class params[] = {};
Object paramsObj[] = {};
Class thisClass = Class.forName(todayClass);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("doit", params);
thisMethod.invoke(iClass, paramsObj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
1, 可以通过 Reflection 机制来做. (推荐)
2, 可以通过比如比较 servlet 的版本号来做, 这样不好.
2, 可以通过比如比较 servlet 的版本号来做, 这样不好.