forked from JavaTX/JavaCompilerCore
Code aufgeräumt
This commit is contained in:
parent
2925926e58
commit
69668434da
@ -18,6 +18,8 @@ import mycompiler.mytype.TypePlaceholder;
|
||||
|
||||
/**
|
||||
* Ein InsertPoint für Generische Variablen
|
||||
* Wird beim Einsetzen eines TIP generiert.
|
||||
* Steht für die Einsetzung aller Generischen Variablendeklarationen zugehörig zu einem Punkt in der SourceFile.
|
||||
* @author janulrich
|
||||
*
|
||||
*/
|
||||
@ -34,54 +36,15 @@ public class GenericTypeInsertPoint extends SourcePatchPoint {
|
||||
this.genericInsertPoint = syntaxTreeNode;
|
||||
}
|
||||
|
||||
/*
|
||||
public GenericTypeInsertPoint(TypeInsertable syntaxTreeNode,
|
||||
Vector<Pair> pairs,ResultSet resultSet) {
|
||||
super(syntaxTreeNode, null, resultSet);
|
||||
this.genericPairs = pairs;
|
||||
if(this.genericPairs.size() == 0)throw new DebugException("Menge der generischen Paare ist leer");
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Der TypeInsertString wird im Falle des GTIP von dem zugehörigen SyntaxTreeNode generiert.
|
||||
* @return
|
||||
*/
|
||||
public String getTypeInsertString() {
|
||||
String genericVar = this.patch.getInsertString(this.resultSet);
|
||||
if(genericVar.length()==0)return "";
|
||||
return this.genericInsertPoint.getGenericVarDeclarationString(genericVar)+" ";
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
protected Type getInsertType(){
|
||||
if(genericPairs.size()==0){
|
||||
return new RefType("<"+this.type.printJavaCode(resultSet)+">",-1);
|
||||
}
|
||||
|
||||
Vector<Pair> pairs = new Vector<Pair>();
|
||||
//Problem: <P1 extends P2> ist falsch! Es muss: <P2, P1 extends P2> heißen:
|
||||
Vector<Type> P1s = new Vector<Type>();
|
||||
Vector<Type> P2s = new Vector<Type>();
|
||||
for(Pair p : genericPairs){
|
||||
P1s.add(p.TA1);
|
||||
P2s.add(p.TA2);
|
||||
}
|
||||
for(Type t : P2s){
|
||||
if(!P1s.contains(t))pairs.add(new Pair(t,null));
|
||||
}
|
||||
pairs.addAll(this.genericPairs);
|
||||
|
||||
Iterator<Pair> it = pairs.iterator();
|
||||
String genericTypeParameters = "<";
|
||||
while(it.hasNext()){
|
||||
genericTypeParameters += new GenericTypeVar(it.next(), 0).printJavaCode(this.getResultSet());
|
||||
if(it.hasNext())genericTypeParameters += ", ";
|
||||
}
|
||||
genericTypeParameters += ">";
|
||||
//Der Generische Variablen String zum Einsetzen ist nun vorhanden
|
||||
this.type = new RefType(genericTypeParameters,0);
|
||||
return this.type;
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
@ -96,63 +59,14 @@ public class GenericTypeInsertPoint extends SourcePatchPoint {
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Versucht den GenericTypeInsertPoint mit dem <FC>bergebenenen p2 zusam
|
||||
* basierend auf ihren insertPoints.
|
||||
* @param p2
|
||||
* @return - Die bei der Zusammenf<FC>hrung entstandene GenericTypeInser
|
||||
|
||||
public Vector<GenericTypeInsertPoint> merge(GenericTypeInsertPoint p2) {
|
||||
Vector<GenericTypeInsertPoint> ret = new Vector<GenericTypeInsertPoint>();
|
||||
if(this.getInsertNode().equals(p2.getInsertNode())){
|
||||
for(Pair p : p2.genericPairs)this.addGenericPair(p);
|
||||
ret.add(this);
|
||||
}else{
|
||||
ret.add(this);
|
||||
ret.add(p2);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
@Override
|
||||
public JavaCodeResult patch(String fileContent, int additionalOffset) {
|
||||
/*
|
||||
int offset = additionalOffset;
|
||||
String insertString = "";
|
||||
if(this.genericParameters != null){
|
||||
offset += this.genericParameters.getOffsetOfLastElement();
|
||||
insertString = ", "+genericVarDecl;
|
||||
}else{
|
||||
offset += this.offset;
|
||||
|
||||
}
|
||||
return sourceCode.substring(0,offset) + insertString + sourceCode.substring(offset);
|
||||
*/
|
||||
//TODO: Es kann sein, dass eine Methode bereits Generische Variablen hat, diese müssen dann an diese Liste angefügt werden.
|
||||
JavaCodeResult ret = new JavaCodeResult(fileContent.substring(0, this.getOffset()+additionalOffset));
|
||||
JavaCodeResult ret = new JavaCodeResult(fileContent.substring(0, this.getOffset()+additionalOffset));
|
||||
JavaCodeResult mitte = new JavaCodeResult(this.getTypeInsertString()).attach(fileContent.substring(this.getOffset()+additionalOffset));
|
||||
ret.attach(mitte);
|
||||
//this.
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
public boolean associatedWith(TypeInsertPoint tip, ResultSet resultSet) {
|
||||
if((tip.type instanceof TypePlaceholder)){
|
||||
Vector<TypePlaceholder> tphs = new Vector<>();
|
||||
tphs.add((TypePlaceholder)tip.type);
|
||||
for(Pair p : resultSet.getConstraintsFor(tphs)){
|
||||
if(p.TA1.equals(this.type))return true;
|
||||
if(p.TA2.equals(this.type))return true;
|
||||
if(this.genericPairs.contains(p))return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
public int getOffset() {
|
||||
return genericInsertPoint.getGenericVarDeclarationOffset();
|
||||
|
@ -21,7 +21,6 @@ import mycompiler.mytypereconstruction.replacementlistener.ITypeReplacementListe
|
||||
*
|
||||
*/
|
||||
public class TypeInsertPoint extends SourcePatchPoint {
|
||||
|
||||
|
||||
public Type type;
|
||||
private TypeInsertable point;
|
||||
@ -59,12 +58,6 @@ public class TypeInsertPoint extends SourcePatchPoint {
|
||||
return this.getTypeInsertString().toString().length();
|
||||
}
|
||||
|
||||
public Vector<TypePlaceholder> getUnresolvedTPH(){
|
||||
Vector<TypePlaceholder> ret = new Vector<TypePlaceholder>();
|
||||
ret.addAll(this.getInsertType().getUnresolvedTPH(resultSet));
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Der Offset des TypeInsertPoints in dem geparsten Source für dessen Klasse er erstellt wurde.
|
||||
@ -102,7 +95,7 @@ public class TypeInsertPoint extends SourcePatchPoint {
|
||||
if(! (obj instanceof TypeInsertPoint))return false;
|
||||
TypeInsertPoint equals = (TypeInsertPoint) obj;
|
||||
if(!(equals.getInsertPoint().equals(this.getInsertPoint())))return false;
|
||||
if(!(equals.getResultSet().equals(this.getResultSet())))return false; //ResultSet spielt bei Equals keine Rolle
|
||||
if(!(equals.getResultSet().equals(this.getResultSet())))return false;
|
||||
if(!(equals.getInsertType().equals(this.getInsertType())))return false;
|
||||
|
||||
return true;
|
||||
|
@ -17,20 +17,10 @@ import mycompiler.mytype.Pair;
|
||||
import mycompiler.mytype.RefType;
|
||||
import mycompiler.mytype.TypePlaceholder;
|
||||
|
||||
/*
|
||||
* Der Plan:
|
||||
* SyntaxTreeNode generiert die TypeInsertPoints.
|
||||
* SyntaxTreeNode berechnet nicht die GenericInsertPoints. Das wird vom InsertSet übernommen.
|
||||
* Das funktioniert mithilfe des ResultSets. Denn es müssen alle Constraints mit einbezogen werden, da der Unify
|
||||
* Algorithmus als Ergebnis nur die zu einem RefType aufgelösten TPHs ausgibt.
|
||||
*/
|
||||
/**
|
||||
* Bündelt ein Set von TypeInsertPoints, die alle zu einem TypePlaceholder gehören.
|
||||
* Diese müssen gemeinsam eingesetzt werden.
|
||||
* Bündelt ein Set von TypeInsertPoints, welche zu einem TypeInferenceResultSet gehören.
|
||||
* Das TypeInsertSet löst zudem Abhängigkeiten auf. Wird eine Generische Variable eingesetzt,
|
||||
* müssen alle mit ihr in Verbindung stehenden Typen ebenfalls eingesetzt werden.
|
||||
* TODO: Es müssen eigentlich nur die generischen Variablendeklarationen eingesetzt werden.
|
||||
* TODO: Einsetzen von generischen Variablen in eine bereits vorhanden Parameterliste funktioniert nicht.
|
||||
* @author janulrich
|
||||
*
|
||||
*/
|
||||
@ -39,7 +29,6 @@ public class TypeInsertSet {
|
||||
protected Logger typinferenzLog = Logger.getLogger("Typeinference");
|
||||
|
||||
public Vector<TypeInsertPoint> points = new Vector<TypeInsertPoint>();
|
||||
private Vector<GenericTypeInsertPoint> genericTypeInsertPoints = new Vector<GenericTypeInsertPoint>();
|
||||
private ResultSet resultSet;
|
||||
|
||||
public TypeInsertSet(ResultSet resultSet) {
|
||||
@ -56,11 +45,6 @@ public class TypeInsertSet {
|
||||
points.add(typeInsertPoint);
|
||||
}
|
||||
}
|
||||
|
||||
public void add(GenericTypeInsertPoint typeInsertPoint) {
|
||||
//this.add((TypeInsertPoint)typeInsertPoint);
|
||||
if(!this.genericTypeInsertPoints.contains(typeInsertPoint))this.genericTypeInsertPoints.add(typeInsertPoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt nur einen einzelnen TypeInsertPoint in fileContent ein.
|
||||
@ -114,47 +98,13 @@ public class TypeInsertSet {
|
||||
* @param fileContent
|
||||
* @return
|
||||
*/
|
||||
@Deprecated
|
||||
public String insertAllTypes(String fileContent) {
|
||||
//TODO: Das hier kann nicht funktionieren. Nach jedem einsetzen eines TIP muss momentan neu geparst werden
|
||||
TypePatchJob tpj = new TypePatchJob();
|
||||
for(TypeInsertPoint tip : this.points){
|
||||
this.insertType(tip, tpj);
|
||||
}
|
||||
return tpj.run(fileContent);
|
||||
/*
|
||||
int additionalOffset = 0;
|
||||
String ret = fileContent;
|
||||
|
||||
|
||||
Vector<GenericTypeInsertPoint> mergedPoints = new Vector<GenericTypeInsertPoint>();
|
||||
//GenericTypeInsertPoints mergen und dem TypeInsertSet anfügen:
|
||||
for(GenericTypeInsertPoint p : this.genericTypeInsertPoints){
|
||||
GenericTypeInsertPoint mergePoint = null;
|
||||
for(GenericTypeInsertPoint mp : mergedPoints){
|
||||
if(mp.getOffset() == p.getOffset())mergePoint = mp;
|
||||
}
|
||||
if(mergePoint == null){
|
||||
mergedPoints.add(p);
|
||||
}else{
|
||||
mergePoint.merge(p);
|
||||
}
|
||||
}
|
||||
for(TypeInsertPoint mp : mergedPoints){
|
||||
this.add(mp);
|
||||
}
|
||||
|
||||
//Anschließend sortieren (nach dem Offset der InsertPoints):
|
||||
Collections.sort(points);
|
||||
|
||||
for(TypeInsertPoint p : points){
|
||||
//Zuerst den Typ einsetzen
|
||||
JavaCodeResult insertCode = p.insertType(ret, additionalOffset);
|
||||
ret = insertCode.toString();
|
||||
//Dann Additional Offset korrigieren:
|
||||
additionalOffset += p.getInsertLength();
|
||||
}
|
||||
return ret;
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
@ -182,12 +132,6 @@ public class TypeInsertSet {
|
||||
//... aber nicht öfter als 1x :
|
||||
if(equals.points.lastIndexOf(point)!=equals.points.indexOf(point))return false;
|
||||
}
|
||||
for(GenericTypeInsertPoint point : this.genericTypeInsertPoints){
|
||||
//Jeder TypeInsertPoint muss auch in equals vorkommen:
|
||||
if(!equals.genericTypeInsertPoints.contains(point))return false;
|
||||
//... aber nicht öfter als 1x :
|
||||
if(equals.genericTypeInsertPoints.lastIndexOf(point)!=equals.genericTypeInsertPoints.indexOf(point))return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -201,20 +145,6 @@ public class TypeInsertSet {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Durchsucht alle bisher angefügten TypeInsertPoints nach nicht aufgelösten TypePlaceholdern.
|
||||
* Diese sollten als Generische Variablen eingesetzt werden.
|
||||
* TODO: Es dürfen nur die TypePlaceholder zurückgegeben werden, welche noch in keinem GenericVarTypeInsertPoint verwendet wurden.
|
||||
* @return
|
||||
*/
|
||||
public Vector<TypePlaceholder> getUnresolvedTPHs(){
|
||||
Vector<TypePlaceholder> ret = new Vector<TypePlaceholder>();
|
||||
for(TypeInsertPoint p : this.points){
|
||||
ret.addAll(p.getUnresolvedTPH());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,6 +12,7 @@ import org.junit.Test;
|
||||
import junit.framework.TestCase;
|
||||
import mycompiler.MyCompiler;
|
||||
import mycompiler.MyCompilerAPI;
|
||||
import mycompiler.SyntaxTreeNode;
|
||||
import mycompiler.myparser.JavaParser.yyException;
|
||||
import mycompiler.mytype.Pair;
|
||||
import mycompiler.mytype.Pair.PairOperator;
|
||||
@ -65,7 +66,7 @@ public class TRMEqualTest {
|
||||
}
|
||||
}
|
||||
|
||||
class TestNode implements TypeInsertable{
|
||||
class TestNode extends SyntaxTreeNode implements TypeInsertable{
|
||||
|
||||
@Override
|
||||
public void replaceType(CReplaceTypeEvent e) {
|
||||
@ -112,7 +113,7 @@ class TestNode implements TypeInsertable{
|
||||
@Override
|
||||
public TypeInsertPoint createTypeInsertPoint(TypePlaceholder tph,
|
||||
ResultSet resultSet) {
|
||||
return new TypeInsertPoint( this, resultSet.getTypeEqualTo(tph), resultSet);
|
||||
return new TypeInsertPoint( this, this, resultSet.getTypeEqualTo(tph), resultSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -120,6 +121,12 @@ class TestNode implements TypeInsertable{
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector<SyntaxTreeNode> getChildren() {
|
||||
// TODO Auto-generated method stub
|
||||
return new Vector<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
11
test/plugindevelopment/TypeInsertTests/LambdaTest15.jav
Normal file
11
test/plugindevelopment/TypeInsertTests/LambdaTest15.jav
Normal file
@ -0,0 +1,11 @@
|
||||
class Overload{
|
||||
|
||||
void method( x ){
|
||||
String varS;
|
||||
Integer varI;
|
||||
|
||||
x.apply(varI);
|
||||
x.apply(varS);
|
||||
}
|
||||
|
||||
}
|
16
test/plugindevelopment/TypeInsertTests/LambdaTest15.java
Normal file
16
test/plugindevelopment/TypeInsertTests/LambdaTest15.java
Normal file
@ -0,0 +1,16 @@
|
||||
package plugindevelopment.TypeInsertTests;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class LambdaTest15 {
|
||||
private static final String TEST_FILE = "LambdaTest15.jav";
|
||||
|
||||
@Test
|
||||
public void run(){
|
||||
Vector<String> mustContain = new Vector<String>();
|
||||
//mustContain.add("A a");
|
||||
MultipleTypesInsertTester.testSingleInsert(this.TEST_FILE, mustContain);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user