From 1dd755bc0a7e24445d445df8f62d87fbca860fac Mon Sep 17 00:00:00 2001 From: Michael Uhl Date: Sat, 29 Dec 2018 19:46:50 +0100 Subject: [PATCH] Ersetzung funtioniert jetzt. --- .../JavaCompilerPlugin.Plugin/.classpath | 2 +- .../editor/IdentifierFinder.java | 63 ++++++++++++++++ .../editor/JafCompletionProcess.java | 75 ++++++++++++------- .../typinferenzplugin/editor/JavEditor.java | 24 +++++- .../JavaCompilerPlugin.Configuration/pom.xml | 2 +- 5 files changed, 131 insertions(+), 35 deletions(-) create mode 100644 JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/IdentifierFinder.java diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/.classpath b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/.classpath index a473135..3e7741f 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/.classpath +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/.classpath @@ -17,7 +17,7 @@ - + diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/IdentifierFinder.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/IdentifierFinder.java new file mode 100644 index 0000000..a53764c --- /dev/null +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/IdentifierFinder.java @@ -0,0 +1,63 @@ +package typinferenzplugin.editor; + +import de.dhbwstuttgart.syntaxtree.ASTVisitor; +import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; +import de.dhbwstuttgart.syntaxtree.statement.LocalVar; +import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.result.PairTPHEqualTPH; +import de.dhbwstuttgart.typeinference.result.PairTPHequalRefTypeOrWildcardType; +import de.dhbwstuttgart.typeinference.result.PairTPHsmallerTPH; +import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; + +public class IdentifierFinder extends AbstractASTWalker implements ASTVisitor, ResultSetVisitor { + + private RefTypeOrTPHOrWildcardOrGeneric result; + + private String identifier; + private int line; + + public IdentifierFinder(String identifier, int line) { + this.identifier = identifier; + this.line = line; + } + + public RefTypeOrTPHOrWildcardOrGeneric getResult() { + return result; + } + + @Override + public void visit(LocalVar localVar) { + if (localVar.name.equals(identifier)) { + this.result = localVar.getType(); + localVar.getType().accept((ResultSetVisitor)this); + } + } + + @Override + public void visit(LocalVarDecl localVar) { + if (localVar.getName().equals(identifier)) { + this.result = localVar.getType(); + localVar.getType().accept((ResultSetVisitor)this); + } + } + + + @Override + public void visit(PairTPHsmallerTPH p) { + System.out.println(); + + } + + @Override + public void visit(PairTPHequalRefTypeOrWildcardType p) { + System.out.println(); + + } + + @Override + public void visit(PairTPHEqualTPH p) { + System.out.println(); + } +} diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JafCompletionProcess.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JafCompletionProcess.java index 98fa6e9..47d85b4 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JafCompletionProcess.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JafCompletionProcess.java @@ -1,9 +1,16 @@ package typinferenzplugin.editor; import static org.eclipse.core.runtime.IStatus.ERROR; +import static org.eclipse.core.runtime.IStatus.WARNING; import static typinferenzplugin.Activator.PLUGIN_ID; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import java.util.TreeSet; import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.Status; @@ -16,7 +23,6 @@ import org.eclipse.jface.text.contentassist.IContentAssistProcessor; import org.eclipse.jface.text.contentassist.IContextInformation; import org.eclipse.jface.text.contentassist.IContextInformationValidator; -import de.dhbwstuttgart.typedeployment.TypeInsert; import typinferenzplugin.Activator; public class JafCompletionProcess implements IContentAssistProcessor { @@ -39,21 +45,46 @@ public class JafCompletionProcess implements IContentAssistProcessor { IDocument doc = viewer.getDocument(); if (isAfterDotOper(offset, doc)) { - String prefix = lastWord(doc, offset - 1); - String indent = lastIndent(doc, offset); + String prefix = lastWord(doc, offset); - ArrayList result = new ArrayList<>(); - /* - for (String ti : editor.resolveTypeInserts()) { - String proposal = ti; - result.add(new CompletionProposal(proposal, offset - prefix.length(), prefix.length(), proposal.length())); - } - */ + Collection result = new ArrayList<>(); + Set duplicatesFinder = new HashSet<>(); + Set methodNames = new TreeSet<>(); + Set fieldNames = new TreeSet<>(); - String proposal = "Line '" + evaluateLineNumber(doc, offset) + "' identifer '" + lastWord(doc, offset) + "'."; - result.add(new CompletionProposal(proposal, offset - prefix.length(), prefix.length(), proposal.length())); - - return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]); + for (String prop : editor.resolveTypeInserts(prefix, evaluateLineNumber(doc, offset))) { + if (prop.contains("<")) { + prop = prop.substring(0, prop.indexOf('<')); + } + + if (!duplicatesFinder.add(prop)) { + continue; + } + + try { + for (Method meth : Class.forName(prop).getMethods()) { + methodNames.add(meth.getName() + " from " + meth.getDeclaringClass().getCanonicalName()); + } + for (Field fld : Class.forName(prop).getFields()) { + fieldNames.add(fld.getName());// + " from " + fld.getDeclaringClass().getCanonicalName()); + } + } catch (Exception e) { + LOG.log(new Status(WARNING, PLUGIN_ID, e.getMessage(), e)); + continue; + } + } + + for (String methProp : methodNames) { + String insertMeth = methProp.substring(0, methProp.indexOf(' ')); + result.add(new CompletionProposal(insertMeth, offset, 0, insertMeth.length(), null, methProp, null, null)); + } + + for (String fieldProp : fieldNames) { + fieldProp = "." + fieldProp; + result.add(new CompletionProposal(fieldProp, offset - prefix.length(), prefix.length(), fieldProp.length())); + } + + return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]); } return NO_COMPLETIONS; } catch (Exception e) { @@ -103,21 +134,6 @@ public class JafCompletionProcess implements IContentAssistProcessor { return ' '; } - private String lastIndent(IDocument doc, int offset) { - try { - int start = offset - 1; - while (start >= 0 && doc.getChar(start) != '\n') - start--; - int end = start; - while (end > offset && Character.isSpaceChar(doc.getChar(end))) - end++; - return doc.get(start + 1, end - start - 1); - } catch (BadLocationException e) { - LOG.log(new Status(ERROR, PLUGIN_ID, e.getMessage(), e)); - } - return ""; - } - @Override public IContextInformation[] computeContextInformation(ITextViewer arg0, int arg1) { return NO_CONTEXTS; @@ -143,4 +159,5 @@ public class JafCompletionProcess implements IContentAssistProcessor { return "Error resolving Autocomplete in '" + PLUGIN_ID + "'."; } + } diff --git a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java index 006e11a..d7520d0 100644 --- a/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java +++ b/JavaCompilerPlugin/bundles/JavaCompilerPlugin.Plugin/src/typinferenzplugin/editor/JavEditor.java @@ -23,6 +23,7 @@ import org.eclipse.ui.views.contentoutline.IContentOutlinePage; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typedeployment.TypeInsert; import typinferenzplugin.Activator; import typinferenzplugin.CodePoint; @@ -34,7 +35,9 @@ import typinferenzplugin.error.ErrorOutput; import static java.util.stream.Collectors.toSet; +import java.util.ArrayList; import java.util.Collection; +import java.util.TreeSet; //Example from: http://help.eclipse.org/indigo/index.jsp /** @@ -215,7 +218,7 @@ public class JavEditor extends TextEditor{ IDocument document = this.getDocumentProvider().getDocument(this.getEditorInput()); document.set(typeReplaceMarker.insertType(document.get())); this.removeMarkers(); - //Erneut den Typinferenzalgorithmus ausführen: + //Erneut den Typinferenzalgorithmus ausführen: this.typeReconstruction(); } @@ -251,9 +254,22 @@ public class JavEditor extends TextEditor{ return typeReplaceMarkers ; } - public Collection resolveTypeInserts() { - return typeinference.getSourceFile().getImports().stream().map(i -> i.toString()).collect(Collectors.toList()); - //return getTypeReplaceMarkers().stream().map(ti -> ti.getInsertPoint()).collect(toSet()); + public Collection resolveTypeInserts(String identifier, int line) { + Collection typesToInsert = new TreeSet<>(); + + IdentifierFinder identifierFinder = new IdentifierFinder(identifier, line); + typeinference.getSourceFile().accept(identifierFinder); + RefTypeOrTPHOrWildcardOrGeneric result = identifierFinder.getResult(); + + int type = result.getOffset().getTokenIndex(); + + + for (TypeReplaceMarker trm : typeinference.run()) { + int tp = trm.getPoint().getPositionInCode(); + typesToInsert.add(trm.getInsertPoint().getInsertString()); + } + + return typesToInsert; } /** diff --git a/JavaCompilerPlugin/releng/JavaCompilerPlugin.Configuration/pom.xml b/JavaCompilerPlugin/releng/JavaCompilerPlugin.Configuration/pom.xml index 81a28bd..5ea663e 100644 --- a/JavaCompilerPlugin/releng/JavaCompilerPlugin.Configuration/pom.xml +++ b/JavaCompilerPlugin/releng/JavaCompilerPlugin.Configuration/pom.xml @@ -27,7 +27,7 @@ de.dhbwstuttgart JavaTXcompiler - 0.1.0 + 0.1 org.antlr