在运行时访问自定义eclipse插件的界面

时间:2016-07-23 15:42:30

标签: java eclipse eclipse-plugin eclipse-rcp

stackoverflow社区,

我有一个定义界面的自定义eclipse插件

public interface IMatchResultEvaluator {

    String evaluateMatchResult(MatchResult match);
}

现在我希望用户能够在运行时实现此接口。所以我希望他能够通过gui编写evaluateMatchResult方法的代码,然后我想用Java 6编译器api编译他的类,然后我想用反射加载类。 为此,我需要在运行时访问IMatchResultEvaluator类。怎么做?

在svasa的回答的帮助下,我试图以这种方式调用javac编译器

Bundle bundle = Platform.getBundle("com.florian.regexfindandreplace");
   String bundleLocation = bundle.getLocation();
   String[] locs = bundleLocation.split("reference:file:/");
   String interfaceFileLoc = locs[1] + bundle.getEntry( "/IMatchResultEvaluator" );

    ProcessBuilder processBuilder = new ProcessBuilder(javaCompiler.getAbsolutePath(), "-classpath",
interfaceFileLoc,   myImplFile.getAbsolutePath());
            Process p = processBuilder.start();

但是没有编译,因为找不到符号IMatchResultEvaluator。 我需要在这里更改什么?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我会这样做: 我假设用户只会在GUI中输入实现主体, 你知道界面的完整路径。

try 
{
    Class<?> IMatchResultEvaluator = Class.forName( "UI.IMatchResultEvaluator" );
    String userText = "My Implementation";
    String MyImplClass = "public class MyImplClass implements " + IMatchResultEvaluator.getSimpleName() + "\n" + "{" + "\n" +  "@Override String evaluateMatchResult( MatchResult match )" + "\n" + "{ " + "\n" + userText + "\n" + " }" + "\n" +  "}";


    File myImplFile = createMyImplClass( MyImplClass );
    if (myImplFile != null & myImplFile.exists())
    {
        compileClass( myImplFile );
    }

} 
catch (ClassNotFoundException e) 
{
    e.printStackTrace();
}

private File createMyImplClass( String MyImplClass )
{
   File file = new File( "MyImplClass.java" );

   //write to file from string MyImplClass
   return file;
}

private void compileClass( File file )
{
   //Get full path of interface
   Bundle bundle = Platform.getBundle("<your plugin qualified name>");
   String bundleLocation = bundle.getLocation();
   String[] locs = bundleLocation.split("reference:file:/");
   String interfaceFileLoc = locs[1] + bundle.getEntry( "/IMatchResultEvaluator" );


   //Compile class

}

答案 1 :(得分:0)

我终于解决了问题。我现在只编译这个课程

    public class MyImplClass {
public static String evaluateMatch( MatchResult match ){
 userText 
}
}

然后我编译并将此类加载到类型为Class的变量clazz中并执行此操作

            final Method method = clazz.getMethod("evaluateMatch", MatchResult.class);
            return new IMatchEvaluator()
                    {

                        @Override
                        public String evaluateMatch(MatchResult match) {
                            try {
                                return (String) method.invoke(null, match);
                            } catch (Exception e) {
                                System.err.println(e.getMessage());
                                return null;
                            }
                        }

                    }; 

如果有人想,可以从https://marketplace.eclipse.org/content/findreplace-regular-expressions-and-match-evaluators下载我的插件。