Typ-Unifikation jetzt in eigenem Thread.

This commit is contained in:
Michael Uhl 2019-01-16 22:34:42 +01:00
parent 14c49c22a7
commit c351cd4adf
2 changed files with 143 additions and 119 deletions

View File

@ -13,6 +13,9 @@ import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.Status;
@ -22,15 +25,15 @@ import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typedeployment.TypeInsert;
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
import de.dhbwstuttgart.typeinference.result.ResolvedType;
import de.dhbwstuttgart.typeinference.result.ResultSet;
import typinferenzplugin.editor.JavEditor;
import typinferenzplugin.error.ErrorOutput;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
public class Typinferenz {
private static final ILog LOG = Activator.getDefault().getLog();
@ -47,10 +50,9 @@ public class Typinferenz {
public Vector<TypeReplaceMarker> run() throws TypeinferenceException {
Vector<TypeReplaceMarker> ret = new Vector<TypeReplaceMarker>();
try {
// JavaTXCompiler compiler = new
// JavaTXCompiler(Arrays.asList(editor.getFilePath().toFile()), false);
JavaTXCompiler compiler = new JavaTXCompiler(editor.getFilePath().toFile(), false);
this.parsedSource = compiler.sourceFiles.get(editor.getFilePath().toFile());
tiResults = compiler.typeInference();
Set<TypeInsert> tips = new HashSet<>();
for (ResultSet tiResult : tiResults) {
@ -65,21 +67,7 @@ public class Typinferenz {
}
}
}
/*
* //Bytecode generieren: String outputDirectory =
* editor.getFilePath().toString(); outputDirectory =
* outputDirectory.substring(0,
* outputDirectory.length()-editor.getFilePath().lastSegment().length());//
* ".jav" hat länge 4 List<ByteCodeResult> bytecodeResults =
* compiler.generateBytecode(parsedSourceFiles, new
* TypeinferenceResults(results)); for(ByteCodeResult result: bytecodeResults){
* JavaClass javaClass = result.getByteCode().getJavaClass(); javaClass.dump(new
* File(outputDirectory+javaClass.getClassName()+".class"));
*
* for(ClassGenerator cg: result.getByteCode().getExtraClasses().values()){
* JavaClass jc = cg.getJavaClass(); jc.dump(new
* File(outputDirectory+jc.getClassName()+".class")); } }
*/
String outputDirectory = editor.getFilePath().toString();
outputDirectory = outputDirectory.substring(0,
outputDirectory.length() - editor.getFilePath().lastSegment().length());// ".jav" hat Länge 4

View File

@ -3,16 +3,24 @@ package typinferenzplugin.editor;
import java.util.Vector;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ICoreRunnable;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.e4.ui.di.UISynchronize;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.editors.text.TextEditor;
@ -39,6 +47,9 @@ import typinferenzplugin.Typinferenz;
import typinferenzplugin.error.ErrorOutput;
import static java.util.stream.Collectors.toSet;
import static org.eclipse.core.runtime.IStatus.ERROR;
import static org.eclipse.core.runtime.IStatus.INFO;
import static typinferenzplugin.Activator.PLUGIN_ID;
import java.util.ArrayList;
import java.util.Collection;
@ -46,12 +57,19 @@ import java.util.TreeSet;
//Example from: http://help.eclipse.org/indigo/index.jsp
/**
* Editor f<EFBFBD>r .jav-Dateien
* Anmerkung: F<EFBFBD>r jede ge<EFBFBD>ffntete Datei wird eine Instanz des Editors erstellt
* Editor f<EFBFBD>r .jav-Dateien Anmerkung: F<EFBFBD>r jede ge<EFBFBD>ffntete Datei wird eine
* Instanz des Editors erstellt
*
* @author janulrich
*
*/
public class JavEditor extends TextEditor {
private static final ILog LOG = Activator.getDefault().getLog();
// get UISynchronize injected as field
@Inject
UISynchronize sync;
private Typinferenz typeinference;
private JavOutline outlinePage;
@ -67,7 +85,8 @@ public class JavEditor extends TextEditor{
private Vector<TypeReplaceMarker> typeReplaceMarkers = new Vector<TypeReplaceMarker>();
//Help: http://wiki.eclipse.org/FAQ_How_do_I_get_started_with_creating_a_custom_text_editor%3F
// Help:
// http://wiki.eclipse.org/FAQ_How_do_I_get_started_with_creating_a_custom_text_editor%3F
public JavEditor() {
super();
// install the source configuration
@ -83,8 +102,8 @@ public class JavEditor extends TextEditor{
this.typeReconstruction(); // Marker generieren
}
/* (non-Javadoc)
* Method declared on AbstractTextEditor
/*
* (non-Javadoc) Method declared on AbstractTextEditor
*/
protected void initializeEditor() {
super.initializeEditor();
@ -105,27 +124,32 @@ public class JavEditor extends TextEditor{
* Startet den Typinferenzalgorithmus, zuvor sollten alle Marker entfernt werden
*/
private void typeReconstruction() {
Job job = Job.create("Update table", (ICoreRunnable) monitor -> {
// do something long running
Vector<JavMarker> markers = new Vector<JavMarker>();
if (!this.typeReplaceMarkers.isEmpty() || !this.errorMarkers.isEmpty()) {
new ErrorOutput("Fehler: Zuerst Marker l<>schen, bevor Typinferenz durchgef<65>hrt werden kann");
LOG.log(new Status(ERROR, PLUGIN_ID, "Fehler: Zuerst Marker löschen, bevor Typinferenz durchgef<65>hrt werden kann"));
return;
}
//this.typeReplaceMarkers = new Vector<TypeReplaceMarker>();
//this.errorMarkers = new Vector<JavMarker>();
try {
this.typeReplaceMarkers = typeinference.run();
for (TypeReplaceMarker m : this.getTypeReplaceMarkers()) {
markers.add(m);
};
}
;
} catch (TypeinferenceException texc) {
markers.add(new ErrorMarker(texc.getMessage(), new CodePoint(texc.getOffset())));
}
//Anschließend die TypeReplaceMarker im Quellcode anzeigen: https://stackoverflow.com/questions/8945371/how-to-implement-quick-fix-quick-assist-for-custom-eclipse-editor
System.out.println("Typinferez durchgef<65>hrt. Berechnete Marker:\n"+markers);
// Anschließend die TypeReplaceMarker im Quellcode anzeigen:
// https://stackoverflow.com/questions/8945371/how-to-implement-quick-fix-quick-assist-for-custom-eclipse-editor
LOG.log(new Status(INFO, PLUGIN_ID, "Typinferez durchgeführt. Berechnete Marker:\n" + markers));
Display.getDefault().asyncExec(() -> {
// do something in the user interface
IResource activeDocument = extractResource();
if (activeDocument == null) {
new ErrorOutput("Kann das aktive Dokument nicht ermitteln");
LOG.log(new Status(ERROR, PLUGIN_ID, "Kann das aktive Dokument nicht ermitteln"));
} else {
this.placeMarkers(activeDocument, markers);
}
@ -133,10 +157,13 @@ public class JavEditor extends TextEditor{
this.errorMarkers.addAll(markers);
this.updateOutlinePage();
// this.outlinePage.update(this.sourceFile,this.markers);
});
});
// Start the Job
job.schedule();
}
IResource extractResource() {
IEditorInput input = this.getEditorInput();
if (!(input instanceof IFileEditorInput))
@ -145,8 +172,10 @@ public class JavEditor extends TextEditor{
}
private void placeMarkers(IResource inDocument, Vector<? extends JavMarker> markers) {
//Marker Tut: http://wiki.eclipse.org/FAQ_How_do_I_create_my_own_tasks,_problems,_bookmarks,_and_so_on%3F
//ResourceMarkerAnnotationModel model = new ResourceMarkerAnnotationModel(inDocument.);
// Marker Tut:
// http://wiki.eclipse.org/FAQ_How_do_I_create_my_own_tasks,_problems,_bookmarks,_and_so_on%3F
// ResourceMarkerAnnotationModel model = new
// ResourceMarkerAnnotationModel(inDocument.);
for (JavMarker rm : markers) {
CodePoint point = rm.getPoint();
try {
@ -165,7 +194,8 @@ public class JavEditor extends TextEditor{
}
private Annotation addAnnotation(IMarker marker, int offset) {
//The DocumentProvider enables to get the document currently loaded in the editor
// The DocumentProvider enables to get the document currently loaded in the
// editor
IDocumentProvider idp = this.getDocumentProvider();
// This is the document we want to connect to. This is taken from
@ -190,6 +220,7 @@ public class JavEditor extends TextEditor{
/**
* Löscht die zu marker geh<EFBFBD>rende Annotation
*
* @param marker
*/
private void deleteAnnotation(JavMarker marker) {
@ -216,9 +247,9 @@ public class JavEditor extends TextEditor{
return super.getAdapter(required);
}
/**
* Diese Funktion f<EFBFBD>hrt einen ReplaceMarker aus.
*
* @param typeReplaceMarker
*/
public void runReplaceMarker(TypeReplaceMarker typeReplaceMarker) {
@ -236,8 +267,10 @@ public class JavEditor extends TextEditor{
} catch (CoreException e) {
e.printStackTrace();
}
for(JavMarker jm : errorMarkers)this.deleteAnnotation(jm);
for(JavMarker jm : typeReplaceMarkers)this.deleteAnnotation(jm);
for (JavMarker jm : errorMarkers)
this.deleteAnnotation(jm);
for (JavMarker jm : typeReplaceMarkers)
this.deleteAnnotation(jm);
this.errorMarkers.removeAllElements();
this.typeReplaceMarkers.removeAllElements();
this.updateOutlinePage();
@ -245,8 +278,8 @@ public class JavEditor extends TextEditor{
}
/**
* Aktualisiert die OutlinePage, falls vorhanden.
* Muss nach <EFBFBD>nderungen an markers aufgerufen werden.
* Aktualisiert die OutlinePage, falls vorhanden. Muss nach <EFBFBD>nderungen an
* markers aufgerufen werden.
*/
private void updateOutlinePage() {
if (this.outlinePage != null && this.sourceFile != null && this.errorMarkers != null)
@ -277,8 +310,11 @@ public class JavEditor extends TextEditor{
}
/**
* Ermittelt die momentan an dem Dokument, für welches dieser Editor zust<EFBFBD>ndig ist, angebrachten TypeReplaceMarker.
* Dabei werden die ReplaceMarker herausgefiltert, welche mindestens einen Typ an dem übergebenen offset einsetzen.
* Ermittelt die momentan an dem Dokument, für welches dieser Editor zust<EFBFBD>ndig
* ist, angebrachten TypeReplaceMarker. Dabei werden die ReplaceMarker
* herausgefiltert, welche mindestens einen Typ an dem übergebenen offset
* einsetzen.
*
* @param offset
* @return
*/