From 744b9ac99d2627841caee191cb21119d6845ebe2 Mon Sep 17 00:00:00 2001 From: Michael Uhl Date: Wed, 16 Jan 2019 23:34:25 +0100 Subject: [PATCH] Code completion uses classpath of generateed classes. --- .../editor/JafCompletionProcess.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JafCompletionProcess.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JafCompletionProcess.java index a0998ab..19b997e 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JafCompletionProcess.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JafCompletionProcess.java @@ -1,10 +1,14 @@ package typinferenzplugin.editor; -import static org.eclipse.core.runtime.IStatus.*; +import static org.eclipse.core.runtime.IStatus.ERROR; +import static org.eclipse.core.runtime.IStatus.WARNING; import static typinferenzplugin.Activator.PLUGIN_ID; +import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -21,6 +25,7 @@ import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.jface.text.contentassist.IContentAssistProcessor; import org.eclipse.jface.text.contentassist.IContextInformation; import org.eclipse.jface.text.contentassist.IContextInformationValidator; +import org.eclipse.swt.internal.C; import typinferenzplugin.Activator; @@ -51,6 +56,9 @@ public class JafCompletionProcess implements IContentAssistProcessor { Set methodNames = new TreeSet<>(); Set fieldNames = new TreeSet<>(); + //LOG.log(new Status(WARNING, PLUGIN_ID,"New path for classpath: " + editor.getFilePath().toFile().getAbsolutePath())); + ClassLoader additionalCP = addPath(editor.getFilePath().toFile().getParentFile()); + for (String prop : editor.resolveTypeInserts(prefix, evaluateLineNumber(doc, offset))) { if (prop.contains("<")) { prop = prop.substring(0, prop.indexOf('<')); @@ -61,10 +69,10 @@ public class JafCompletionProcess implements IContentAssistProcessor { } try { - for (Method meth : Class.forName(prop).getMethods()) { + for (Method meth : additionalCP.loadClass(prop).getMethods()) { methodNames.add(meth.getName() + " from " + meth.getDeclaringClass().getCanonicalName()); } - for (Field fld : Class.forName(prop).getFields()) { + for (Field fld : additionalCP.loadClass(prop).getFields()) { fieldNames.add(fld.getName());// + " from " + fld.getDeclaringClass().getCanonicalName()); } } catch (Exception e) { @@ -91,6 +99,11 @@ public class JafCompletionProcess implements IContentAssistProcessor { return NO_COMPLETIONS; } } + + public static ClassLoader addPath(File f ) throws Exception { + URL u = f.toURI().toURL(); + return URLClassLoader.newInstance(new URL[]{u}, Thread.currentThread().getContextClassLoader()); + } private boolean isAfterDotOper(int offset, IDocument doc) { return lastChar(doc, offset) == DOT_OPERATOR;