diff --git a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java index 0f8012ac..be42c2ba 100644 --- a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java +++ b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.bytecode; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import org.apache.commons.bcel6.classfile.BootstrapMethod; import org.apache.commons.bcel6.classfile.BootstrapMethods; @@ -29,9 +31,10 @@ public class ClassGenerator extends ClassGen{ private Type superClass; private Menge usedTPHs = new Menge<>(); + + private Map extraClasses = new HashMap<>(); - public ClassGenerator(String name, Type superClass, String string, - short accPublic, String[] strings, TypeinferenceResultSet resultSet) { + public ClassGenerator(String name, Type superClass, String string, short accPublic, String[] strings, TypeinferenceResultSet resultSet) { super(name,superClass.get_Name(),string,accPublic,strings, new DHBWConstantPoolGen()); this.tiResult = resultSet; this.superClass = superClass; @@ -156,4 +159,12 @@ public class ClassGenerator extends ClassGen{ return ret; } + public void addExtraClass(ClassGenerator cg){ + extraClasses.put(cg.getClassName(), cg); + } + + public Map getExtraClasses() { + return extraClasses; + } + } diff --git a/src/de/dhbwstuttgart/core/MyCompiler.java b/src/de/dhbwstuttgart/core/MyCompiler.java index 74099f38..60ea5bb2 100755 --- a/src/de/dhbwstuttgart/core/MyCompiler.java +++ b/src/de/dhbwstuttgart/core/MyCompiler.java @@ -1,9 +1,6 @@ -// ino.module.MyCompiler.8569.package + package de.dhbwstuttgart.core; -// ino.end - -// ino.module.MyCompiler.8569.import import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; @@ -48,67 +45,28 @@ import de.dhbwstuttgart.typeinference.exceptions.ParserError; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; - -// ino.class.MyCompiler.21258.declaration -public class MyCompiler implements MyCompilerAPI -// ino.end -// ino.class.MyCompiler.21258.body -{ - // ino.attribute.NO_LINENUMBER.21262.decldescription type=line +public class MyCompiler implements MyCompilerAPI{ // PL: Der Zusammenhang zwischen paralist und vParaOrg muesste // noch geklaert werden 05-01-07 - // ino.end - // ino.attribute.NO_LINENUMBER.21262.declaration public static final int NO_LINENUMBER = -1; - // ino.end - - // ino.attribute.codegenlog.21265.decldescription type=line - // Logger - // ino.end - // ino.attribute.codegenlog.21265.declaration - //protected static Logger codegenlog = Logger.getLogger("codegen"); - // ino.end - // ino.attribute.inferencelog.21268.declaration + protected static Logger inferencelog = Logger.getLogger(MyCompiler.class.getName()); - // ino.end - // ino.attribute.parserlog.21271.declaration - //protected static Logger parserlog = Logger.getLogger("parser"); - // ino.end - - // ino.attribute.OutputDir.21274.declaration + protected String OutputDir = ""; - // ino.end + public Menge testPair = null; - // ino.attribute.m_AbstractSyntaxTree.21280.decldescription type=javadoc - /** - * Der abstrake Syntaxbaum - *
Autor: J�rg B�uerle - */ - // ino.end - // ino.attribute.m_AbstractSyntaxTree.21280.declaration - private Menge m_AbstractSyntaxTree = new Menge(); - // ino.end - - // ino.method.MyCompiler.21283.defdescription type=javadoc /** * Author: J�rg B�uerle
* Der private Konstruktor. Es soll von au�en kein Compiler angelegt werden * k�nnen, sondern nur eine API zur Verf�gung gestellt werden. * @param logger Konfiguration für Debug Ausgabe TODO */ - // ino.end - // ino.method.MyCompiler.21283.definition - private MyCompiler() - // ino.end - // ino.method.MyCompiler.21283.body - { + private MyCompiler(){ this.init(); } - // ino.end - - // ino.method.getAPI.21286.defdescription type=javadoc + /** * Author: Jörg Bäuerle
* Stellt eine neue Instanz der CompilerAPI zur Verf�gung. @@ -116,116 +74,11 @@ public class MyCompiler implements MyCompilerAPI * um eine Quellcode-Datei zu kompilieren. * @return Die Compiler-API */ - // ino.end - // ino.method.getAPI.21286.definition - public static MyCompilerAPI getAPI(LoggerConfiguration loggerConfig) - // ino.end - // ino.method.getAPI.21286.body - { + public static MyCompilerAPI getAPI(LoggerConfiguration loggerConfig){ Logger.setStandardConfiguration(loggerConfig); return new MyCompiler(); } - // ino.end - public Menge testPair = null; - - // ino.method.wandleGeneric2RefType.21289.defdescription type=javadoc - /** - * Author: Thomas Ott
- * Ersetzt in der Superklassenparameterliste einer Klasse, diejenigen - * GenericTypeVars, zu denen es eine Klasse gibt, die gleich hei�t. - * Beim Parsen werden n�mlich vom Jay nur GenericTypeVars erzeugt und keine - * RefTypes. Dies wird durch diese Methode nachgeholt.
- * Bsp.: class JoergsTolleKlasse extends MartinsSuperklasse - *
Wie man an diesem Beispiel sieht, kann nur eine Superklasse instantiierte - * Typvariablen zwischen den eckigen Klammern stehen haben, nicht jedoch die - * abgeleitete Klasse. - * - * @param className Klassenname der aktuell betrachteten Klasse - * @param Parameter Parameter der Superklasse - * @param KlassenVektor - - // ino.end - // ino.method.wandleGeneric2RefType.21289.definition - public static void wandleGeneric2RefType(Menge Parameter, Menge KlassenVektor ) - // ino.end - // ino.method.wandleGeneric2RefType.21289.body - { - //wandleGeneric2RefType SOLLTE NICHT NUR FUER LISTEN - //VON TYPEN FUNKTIONIEREN PL 05-01-19 - // otth: GenericTypeVar in Superklassenparameterlisten in RefTypes mit Parameterlsite = NULL umwandeln, - // falls: f�r GenericTypeVar existiert eine gleichnamige Klasse - if(Parameter == null) return; - - for( int i = 0; i < Parameter.size(); i++) - { - Type TempParameter = Parameter.elementAt(i); - inferencelog.debug("Nr. des Parameters: " + i); - - // an dieser Stelle: Parametername - if ( TempParameter instanceof GenericTypeVar) - { - inferencelog.debug("Generic, WANDLE: " + TempParameter.getName()); - // existiert f�r GenericTypeVar eine deklarierte Klasse - for( int k = 0; k < KlassenVektor.size(); k++) - { - if( KlassenVektor.elementAt(k).getSimpleName().equals(TempParameter.getSimpleName()) ) - { - // Klasse existiert, darf aber keine Parameterliste in der Definition haben - if( KlassenVektor.elementAt(k).get_ParaList().size() == 0 ) - { - RefType RNeu = new RefType( TempParameter.getName().toString(), null,TempParameter.getOffset()); - inferencelog.debug( "Vorher: " + Parameter ); - // i-te Stelle ersetzen - Parameter.set( i, RNeu ); - inferencelog.debug( "GenericTypeVar " + TempParameter.getName() + " umwandeln..." ); - inferencelog.debug( "Nachher: " + Parameter ); - } - else - { - //parserlog.error( "SEMANTIK-CHECK-FEHLER: Parameter " + TempParameter.getName() + " muss weitere Parameter besitzen (laut Klassendefinition)" ); - //FIXME Throw exception instead of simple exit - System.exit( 1 ); - } - } - else { - inferencelog.debug("Ist echter Generic, wird nicht ersetzt!"); - } - } // end for - } // end if - else - { - inferencelog.debug("Nicht Generic, WANDLE nicht: " + TempParameter.getName()); - // RefType --> u.U. rekursiv weiterwandeln - if(TempParameter instanceof RefType) - { - RefType R = (RefType)TempParameter; - if( R.get_ParaList() != null ) - wandleGeneric2RefType( R.get_ParaList(), KlassenVektor ); - } - else if(TempParameter instanceof ITypeContainer) - { - Type T = ((ITypeContainer)TempParameter).getContainedType(); - if(T instanceof RefType) - { - RefType R = (RefType)T; - if( R.get_ParaList() != null ) - wandleGeneric2RefType( R.get_ParaList(), KlassenVektor ); - } - } - else - { - inferencelog.error("Internal Error"); - System.exit( 1 ); - } - } - } //end for - } //end wandleGeneric2RefType - // ino.end - */ - - - // ino.method.parse.21292.defdescription type=javadoc /** * Parst den Quellcode und baut den abstrakten Syntaxbaum auf. Danach wird * automatisch der von Thomas Ott implementierte Algorithmus @@ -236,246 +89,19 @@ public class MyCompiler implements MyCompilerAPI * @throws IOException * @throws JavaParser.yyException */ - // ino.end - // ino.method.parse.21292.definition - private void parse_backup(Reader reader) - throws IOException, JavaParser.yyException - // ino.end - // ino.method.parse.21292.body - { - /* - parserlog.info("#########################################"); - parserlog.info("# Parsen - START #"); - parserlog.info("#########################################\n"); - - ////////////////////////////////////// - // Alte Daten l�schen: - ////////////////////////////////////// - m_AbstractSyntaxTree = null; - - ////////////////////////////////////// - // Scanner und Parser erzeugen: - ////////////////////////////////////// - Scanner scanner = new Scanner(reader); - JavaParser parser = new JavaParser(); - - ////////////////////////////////////// - // Parsen ==> Ergebnis: srcFile = Abstrakter Syntaxbaum - ////////////////////////////////////// - SourceFile srcFile = (SourceFile) parser.yyparse( scanner ); - this.testPair = parser.testPair; - - parserlog.info( "Parsen war erfolgreich!\n"); - - //PL 05-07-31 verschoben nach SourceFile.java in Methode typeReconstruction - // otth: TypePlaceholders in Superklassenparameterlisten in RefTypes mit Parameterlsite = NULL umwandeln, - // falls: f�r TypePlaceholder existiert eine gleichnamige Klasse - // Superklasse suchen - //for( int i = 0; i < srcFile.KlassenVektor.size(); i++ ) - //{ - //Class tempKlasse = (Class)srcFile.KlassenVektor.elementAt( i ); - - //PL 05-07-30 ausgetauscht um alle Typedeklarationen zu wandeln - // if( tempKlasse.superclassid != null && tempKlasse.get_ParaList() != null ) - // { - // // aktuelle Klasse hat Superklasse und Parameter - // Menge Parameter = tempKlasse.superclassid.get_ParaList(); - // this.wandleGeneric2RefType(Parameter, srcFile.KlassenVektor ); - // } - //wandleGeneric2RefType(tempKlasse.getContainedTypes(), srcFile.KlassenVektor ); - - //} - - // otth: echte Konstruktoren von Methodendeklarationen ohne Typen unterscheiden - if ( srcFile != null ) - { - Class tempKlasse = null; - ClassBody tempKlassBody = null; - Menge tempMengeFieldDecl; - mycompiler.myclass.Field tempFieldDecl = null; - String strKlasse; - - for( int i = 0; i < srcFile.KlassenVektor.size(); i++ ) - { - - // Unterscheidung zwischen Class und Interfaces - if (srcFile.KlassenVektor.elementAt(i) instanceof Class) { - tempKlasse = (Class)srcFile.KlassenVektor.elementAt( i ); - tempKlassBody = tempKlasse.get_ClassBody(); - } else { - tempKlasse = null; - tempKlassBody = null; - } - - if ( tempKlassBody != null ) - { - strKlasse = tempKlasse.getName(); - parserlog.debug("T->Felddeklarationen f�r die Klasse:" + strKlasse); - parserlog.debug( "------------------------------------"); - - // Schleife �ber alle fielddeclarations - tempMengeFieldDecl = tempKlassBody.getFields(); - for( int k = 0; k < tempMengeFieldDecl.size(); k++ ) - { - tempFieldDecl = tempMengeFieldDecl.elementAt(k); - if( tempFieldDecl instanceof Constructor ) - { - //parserlog.debug("T->Konstruktor: " + ((DeclId)tempFieldDecl.get_Name().elementAt(0)).get_Name() + " - ReturnType: " + tempFieldDecl.getTypeName()); - - // pr�fen, ob Construktorname == Klassenname - falls nein: Construktor in Methode umwandeln !!! - String strConstName = ((DeclId)tempFieldDecl.get_Name().elementAt(0)).get_Name(); // Konstruktorname - if ( !strConstName.equals( strKlasse ) ) - { - // Element k durch neues ersetzen! - - Method Methode = new Method(); - Method Konstruktor = (Constructor)tempFieldDecl; - - // Elementweise vom Konstruktor in die Methode kopieren - Methode.set_Block( Konstruktor.get_Block() ); - Methode.setParameterList( Konstruktor.getParameterList() ); - Methode.set_ExceptionList( Konstruktor.get_ExceptionList() ); - Methode.setReturnType( Konstruktor.getReturnType() ); - Methode.setDeclIdMenge( Konstruktor.getDeclIdMenge() ); - // types_in_parameterlist wird wohl erst sp�ter und intern gef�llt - - // R�ckgabetyp = Objekt der Klasse 'TypePlaceholder' - - // #JB# 31.03.2005 - // ########################################################### - Methode.setReturnType(TypePlaceholder.fresh(Methode)); - //Methode.setReturnType( new TypePlaceholder("###NEU###") ); - // ########################################################### - - // Element an der Position k durch neues ersetzen! - tempMengeFieldDecl.setElementAt( Methode, k ); - } - } - if( tempFieldDecl instanceof Method && !(tempFieldDecl instanceof Constructor) ) - { - //parserlog.debug("T->Methode: " + ((DeclId)tempFieldDecl.get_Name().elementAt(0)).get_Name() + " - ReturnType: " + tempFieldDecl.getTypeName()); - } - - } - - // Debugg-Infos - parserlog.debug(""); - parserlog.debug("T->NEUE Felddeklarationen f�r die Klasse:" + strKlasse); - parserlog.debug( "-----------------------------------------"); - for( int k = 0; k < tempMengeFieldDecl.size(); k++ ) - { - tempFieldDecl = tempMengeFieldDecl.elementAt(k); - //parserlog.debug("T->" + ((DeclId)tempFieldDecl.get_Name().elementAt(0)).get_Name() + " - Typ: " + tempFieldDecl + " - ReturnType: " + tempFieldDecl.getTypeName()); - } - - // otth: TypePlaceholders durchnummerieren mit A, B, ... - parserlog.debug(""); - parserlog.debug(""); - parserlog.debug("Suche TypePlaceholders in den FieldDecls:"); - parserlog.debug( "------------------------------------------"); - - ParameterList tempParameterList; - Menge tempVFktPara; - FormalParameter tempFP; - Method tempMethod; - Type tempReturn; - Type tempType; - for( int k = 0; k < tempMengeFieldDecl.size(); k++ ) - { - tempFieldDecl = tempMengeFieldDecl.elementAt(k); - tempMethod = null; - - if( tempFieldDecl instanceof Method ) - { - tempMethod = (Method)tempFieldDecl; - tempReturn = tempMethod.getReturnType(); - - // Funktionen ohne definierten R�ckgabetyp suchen!!! - if( tempReturn instanceof TypePlaceholder ) - { - // Methode mit nicht-definiertem R�ckgabetyp gefunden! - - // #JB# 31.03.2005 - // ########################################################### - // Wird bereits �ber fresh() gemacht!! - //tempReturn.setName( TypePlaceholder.makeNewName() ); - // ########################################################### - parserlog.debug(""); - parserlog.debug("Methode ohne Rueckgabetyp: " + tempMethod.get_Method_Name() + " --> " + tempReturn.getName()); - } - else - { - parserlog.debug(""); - parserlog.debug("Methode mit Rueckgabetyp / Konstruktor: " + tempMethod.get_Method_Name()); - } - - // Methoden-Funktionsparameter durchsuchen - tempParameterList = tempMethod.getParameterList(); - if ( tempParameterList != null ) - { - tempVFktPara = tempParameterList.sc_get_Formalparalist(); - for( int l = 0; l < tempVFktPara.size(); l++ ) - { - tempFP = tempVFktPara.elementAt(l); - tempType = tempFP.getType(); - if( tempType instanceof TypePlaceholder ) - { - if( tempType != null ) - { - // neuer Name berechnen - - // #JB# 31.03.2005 - // ########################################################### - // Wird bereits �ber fresh() gemacht!! - //tempType.setName( TypePlaceholder.makeNewName() ); - // ########################################################### - } - } - parserlog.debug(""); - parserlog.debug(" Parameter: " + tempFP.get_Name() + " --> " + tempType.getName()); - } - } - else - { - parserlog.debug(""); - parserlog.debug(" Methode hat keine Parameter!"); - } - } - } - } //end if - } //end for - } //end if - m_AbstractSyntaxTree = srcFile; - - parserlog.info("#########################################"); - parserlog.info("# Parsen - ENDE #"); - parserlog.info("#########################################\n"); - */ - } // end Methode parse() - // ino.end - + private void parse_backup(Reader reader) throws IOException, JavaParser.yyException{ + + } ///////////////////////////////////////////////////////////////////////////////////////////////// // Implementierte API-Methoden: ///////////////////////////////////////////////////////////////////////////////////////////////// - // ino.method.init.21295.defdescription type=javadoc /** * Author: J�rg B�uerle
* Initialisiert den Compiler */ - // ino.end - // ino.method.init.21295.definition - public void init() - // ino.end - // ino.method.init.21295.body - { + public void init(){ TypePlaceholder.deleteRegistry(); - - // Log4J fuer die Ausgabe vorbereiten - //DOMConfigurator.configure("log4j.xml"); } - // ino.end - - // ino.method.parse.21298.defdescription type=javadoc /** * Author: J�rg B�uerle
* Ruft die Parse-Methode. @@ -484,22 +110,13 @@ public class MyCompiler implements MyCompilerAPI * @throws IOException Wenn was schief l�uft. * @throws JavaParser.yyException Wenn ein Fehler beim Parsen auftritt. */ - // ino.end - // ino.method.parse.21298.definition - public SourceFile parse(File file) - throws FileNotFoundException, IOException, JavaParser.yyException - // ino.end - // ino.method.parse.21298.body - { + public SourceFile parse(File file) throws FileNotFoundException, IOException, JavaParser.yyException{ FileReader fr = new FileReader(file); SourceFile ret = this.parse2SyntaxTree(fr); - this.m_AbstractSyntaxTree.add(ret); fr.close(); return ret; } - // ino.end - - // ino.method.typeReconstruction.21304.defdescription type=javadoc + /** * Author: J�rg B�uerle
* Ruft den Typrekonstruktionsalgorithmus auf. @@ -508,16 +125,7 @@ public class MyCompiler implements MyCompilerAPI * ist. @throws CTypeReconstructionException Wenn ein Fehler bei der * Typrekonstruktion auftritt. */ - // ino.end - // ino.method.typeReconstruction.21304.definition - public Menge typeReconstruction() - throws NullPointerException, CTypeReconstructionException - // ino.end - // ino.method.typeReconstruction.21304.body - { - if(m_AbstractSyntaxTree==null){ - throw new NullPointerException("Es wurde noch kein Abstrakter Syntaxbaum erstellt!"); - } + public Menge typeReconstruction(Menge m_AbstractSyntaxTree) throws NullPointerException, CTypeReconstructionException{ inferencelog.info("##########################################", Section.TYPEINFERENCE); inferencelog.info("# TypeReconstruction-Algorithmus - START #", Section.TYPEINFERENCE); inferencelog.info("##########################################\n", Section.TYPEINFERENCE); @@ -535,7 +143,6 @@ public class MyCompiler implements MyCompilerAPI return result; } - // ino.end /** * Erstellt die FunN-Assumptions @@ -559,45 +166,13 @@ public class MyCompiler implements MyCompilerAPI return ret; } - - /** - * Author: J�rg B�uerle
- * Generiert den Bytecode und das Class-File f�r den Syntaxbaum. - * @throws NullPointerException Wenn noch kein abstrakter Syntaxbaum vorhanden - * ist. - - @Override - public Menge codeGeneration(ResultSet result) - throws NullPointerException, JVMCodeException - { - if(m_AbstractSyntaxTree==null){ - throw new NullPointerException("Es wurde noch kein Abstrakter Syntaxbaum erstellt!"); - } - codegenlog.info("Beginn der Codegenerierung ..."); - - Menge ret = new Menge(); - - for(SourceFile sf : m_AbstractSyntaxTree){ - ret.addAll(sf.codegen(result)); - } - - codegenlog.info("Codegenerierung beendet!"); - return ret; - }*/ - - // ino.method.main.21313.defdescription type=javadoc /** * Die Main-Funktion, �ber die der Compiler auch per Konsole gestartet * werden kann. * @param args Klassendatei */ - // ino.end - // ino.method.main.21313.definition - public static void main(String[] args) - // ino.end - // ino.method.main.21313.body - { + public static void main(String[] args){ MyCompilerAPI compiler = MyCompiler.getAPI(new LoggerConfiguration()); // Hier koennten ggf. Aenderungen der Ausgabeeinstellungen @@ -617,33 +192,9 @@ public class MyCompiler implements MyCompilerAPI System.err.println(e); System.exit(0); } - - ///////////////////////// - // Semantik-Check: - ///////////////////////// - // try { - // compiler.semanticCheck(); - // } catch (NullPointerException e) { - // System.out.println("Fehler beim Aufrufen des Semantik-Checks:"); - // System.out.println(e); - // System.exit(0); - // } catch (SCException e) { - // e.fehlerausgabe(); - // System.exit(0); - // } - - ///////////////////////// - // Code-Generierung: - ///////////////////////// - //compiler.codeGeneration(); - } - // ino.end + } - // ino.method.setOutputDir.21316.definition - public void setOutputDir(String dir) - // ino.end - // ino.method.setOutputDir.21316.body - { + public void setOutputDir(String dir){ char c = dir.charAt(dir.length()-1); if (c != '/' & c != '\\') dir = dir + "/"; OutputDir = dir; @@ -652,75 +203,11 @@ public class MyCompiler implements MyCompilerAPI File f = new File(dir); f.mkdirs(); } - // ino.end - // ino.method.getOutputDir.21319.definition - public String getOutputDir() - // ino.end - // ino.method.getOutputDir.21319.body - { + public String getOutputDir(){ return OutputDir; } - // ino.end -/* - // ino.method.getFullyQualifiedNameFromClassname.21322.definition - public static String getFullyQualifiedNameFromClassname(String typ, ImportDeclarations declarations) - // ino.end - // ino.method.getFullyQualifiedNameFromClassname.21322.body - { - String ret=null; - // Es ist kein FullyQualifiedName => In den Imports die Klasse suchen - for(int j=0;j de.dhbwstuttgart.typeinference.Menge x; - * @param containedTypes Alle Typen, die die Klasse beinhaltet - * @param name Alle Klassen, die es in den BasicAssumptions und im - * AbstractSyntaxTree gibt @param declarations Alle Import-Declarations - - // ino.end - // ino.method.makeRefTypesFullyQualified.21325.definition - public static void makeRefTypesFullyQualified(Menge containedTypes, ImportDeclarations declarations) - // ino.end - // ino.method.makeRefTypesFullyQualified.21325.body - { - // HOTI 8.5.06 Anhand der ContainedTypes alle Variablen aendern - for( int i = 0; i < containedTypes.size(); i++) - { - Type tempParameter = (Type)(containedTypes.elementAt(i)); - // Nat�rlich nur RefTypes updaten - if(tempParameter instanceof RefType){ - RefType typ=(RefType)tempParameter; - UsedId fullyQualifiedName=UsedId.createFromQualifiedName(typ.getTypeName(),typ.getOffset()); - // Kein FullyQualifiedName - if(fullyQualifiedName.name.size()==1){ - String newType=getFullyQualifiedNameFromClassname(typ.getSimpleName(),declarations); - if(newType!=null){ - typ.setName(newType); - } - - } - if(typ.get_ParaList()!=null){ - makeRefTypesFullyQualified(typ.get_ParaList(),declarations); - } - } - } - } - // ino.end - */ /** * @author Arne Lüdtke * Ersetzt alle GTVs durch TPHs mit gleichem Namen. Arbeitet Rekursiv. @@ -790,8 +277,10 @@ public class MyCompiler implements MyCompilerAPI /** * Diese Funktion nimmt einen Menge von Dateinamen. Alle diese Dateien werden zu einem SyntaxBaum geparst. + * @return */ - public void parse(Menge filenames) throws ParserError { + public Menge parse(Menge filenames) throws ParserError { + Menge m_AbstractSyntaxTree = new Menge(); for(String filename : filenames){ StringBuffer fileData = new StringBuffer(); @@ -816,58 +305,25 @@ public class MyCompiler implements MyCompilerAPI StringReader srcreader = new StringReader(fileData.toString()); //Den aus der Datei ausgelesenen Quellcode zu einem Syntaxbaum parsen: - this.m_AbstractSyntaxTree.add(parse2SyntaxTree(srcreader)); // Alle Dateien nacheinander hintereinander anhängen... - - } - /* - String gesamterSrc = ""; - //Hier werden alle übergebenen Dateinamen abgearbeitet: - for(String filename : filenames){ - try { - StringBuffer fileData = new StringBuffer(); - BufferedReader reader = new BufferedReader( - new FileReader(filename)); - char[] buf = new char[1024]; - int numRead=0; - while((numRead=reader.read(buf)) != -1){ - String readData = String.valueOf(buf, 0, numRead); - fileData.append(readData); - } - reader.close(); - gesamterSrc += fileData.toString() + "\n"; // Alle Dateien nacheinander hintereinander anhängen... - } catch (Exception e) { - e.printStackTrace(); - throw new TypinferenzException("Die übergebenen Dateien konnten nicht zum Parsen eingelesen werden."); - } + m_AbstractSyntaxTree.add(parse2SyntaxTree(srcreader)); // Alle Dateien nacheinander hintereinander anhängen... } - try { - // und anschließend zum Parsen übergeben. - this.parse(gesamterSrc.toString()); - } catch (Exception e) { - e.printStackTrace(); - //throw new TypinferenzException("Fehler beim Parsen"); - } - - */ + return m_AbstractSyntaxTree; } @Override public SourceFile parse(String sourceCode) { - SourceFile ret = this.parse2SyntaxTree(new StringReader(sourceCode)); - this.m_AbstractSyntaxTree.add(ret); - return ret; + return parse2SyntaxTree(new StringReader(sourceCode)); } @Override - public Menge generateBytecode(TypeinferenceResultSet typeinferenceResult) { + public Menge generateBytecode(Menge m_AbstractSyntaxTree, TypeinferenceResultSet typeinferenceResult) { //SourceFile parsedFile = this.m_AbstractSyntaxTree.firstElement(); //Class parsedClass = parsedFile.KlassenVektor.firstElement(); Menge ret = new Menge<>(); - for(SourceFile sf : this.m_AbstractSyntaxTree){ + for(SourceFile sf : m_AbstractSyntaxTree){ ret.addAll(sf.generateBytecode(typeinferenceResult)); } return ret; } } -// ino.end diff --git a/src/de/dhbwstuttgart/core/MyCompilerAPI.java b/src/de/dhbwstuttgart/core/MyCompilerAPI.java index 9c5d2a57..f0eac42f 100755 --- a/src/de/dhbwstuttgart/core/MyCompilerAPI.java +++ b/src/de/dhbwstuttgart/core/MyCompilerAPI.java @@ -85,8 +85,7 @@ public interface MyCompilerAPI */ // ino.end // ino.method.typeReconstruction.21340.declaration - public Menge typeReconstruction() - throws NullPointerException, TypeinferenceException; + public Menge typeReconstruction(Menge m_AbstractSyntaxTree) throws NullPointerException, CTypeReconstructionException; // ino.end // ino.method.setOutputDir.21349.decldescription type=javadoc @@ -112,8 +111,9 @@ public interface MyCompilerAPI /** * Parst zusammenhängende JavaKlassen in verschiedenen Dateien. * @param filenames - Eine Liste von Quellcodedateien, welche gseparst werden sollen + * @return */ - public void parse(Menge filenames) throws ParserError; + public Menge parse(Menge filenames) throws ParserError; /** * Parst den SourceCode einer Datei. @@ -127,6 +127,6 @@ public interface MyCompilerAPI * Dafür müssen die Schritte Parsen und typeReconstruction ausgeführt werden. * @return */ - public Menge generateBytecode(TypeinferenceResultSet rs); + public Menge generateBytecode(Menge m_AbstractSyntaxTree, TypeinferenceResultSet typeinferenceResult); } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 356e3298..debcf73e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -15,7 +15,6 @@ import org.apache.commons.bcel6.generic.InstructionHandle; import org.apache.commons.bcel6.generic.InstructionList; import org.apache.commons.bcel6.generic.MethodGen; -import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.logger.SectionLogger; @@ -25,6 +24,7 @@ import de.dhbwstuttgart.bytecode.DHBWInstructionFactory; import de.dhbwstuttgart.core.AClassOrInterface; import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.parser.JavaClassName; +import de.dhbwstuttgart.syntaxtree.misc.DeclId; import de.dhbwstuttgart.syntaxtree.misc.UsedId; import de.dhbwstuttgart.syntaxtree.modifier.Modifiers; import de.dhbwstuttgart.syntaxtree.statement.Block; @@ -80,6 +80,8 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I DHBWConstantPoolGen _cp; ClassGenerator _cg; + Menge results = new Menge(); + SectionLogger logger = Logger.getSectionLogger(this.getClass().getName(), Section.CODEGEN); logger.debug("Test"); @@ -111,11 +113,10 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I c.genByteCode(_cg, fieldInitializations); } - ByteCodeResult code = new ByteCodeResult(_cg); - return code; + return new ByteCodeResult(_cg); } - private Menge superif = new Menge(); + private Menge superif = new Menge(); public UsedId getPackageName() { @@ -223,7 +224,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I } this.offset = offset; if(!name.equals("Object"))//Alle Klassen außer Object erben von Object: - this.superClass = new Class("Object", -1).getType(); + this.superClass = new Class("java.lang.Object", -1).getType(); } // ino.end @@ -593,112 +594,6 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I { return this.parahash; } - // ino.end - - - -/*static void string_rec(Hashtable ht){ - String record=""; - boolean isError=false; - record=record.concat("["); - for(Enumeration e=ht.elements(),k=ht.keys();e.hasMoreElements();){ - String s = (String)k.nextElement(); - Object o = e.nextElement(); - - record=record.concat(" "+s); - - if(o instanceof Type){ - record=record.concat(" = "+((Type)o).getName()); - } else if(o instanceof Hashtable){ - record=record.concat("= "); - string_rec((Hashtable)o); - if(e.hasMoreElements()) - record=record.concat(", "); - } else if(o instanceof String){ - record=record.concat(" = "+o); - if(e.hasMoreElements()) - record=record.concat(", "); - } - else { - record=("[FEHLER: string_rec: unbekannter Typ!!!!!!"); - isError=true; - } - } - record=record.concat("]"); - if(isError){ - parserlog.error(record); - }else{ - parserlog.debug(record); - } - }*/ - - /*static void string_rec(Menge v){ - String record=("{"); - for(Enumeration e=v.elements();e.hasMoreElements();){ - Type t = (Type)e.nextElement(); - record=record.concat(" "+t.getName()); - - if(e.hasMoreElements()) - record=record.concat(","); - } - record=record.concat("}"); - parserlog.debug(record); - }*/ - /*static void string_rec(String st, Hashtable ht){ - String record=(st); - boolean isError=false; - record=record.concat("["); - for(Enumeration e=ht.elements(),k=ht.keys();e.hasMoreElements();){ - String s = (String)k.nextElement(); - Object o = e.nextElement(); - - record=record.concat(" "+s); - - if(o instanceof Type){ - record=record.concat(" = "+((Type)o).getName()); - } - - else if(o instanceof Hashtable){ - record=record.concat("= "); - string_rec((Hashtable)o); - if(e.hasMoreElements()) - record=record.concat(", "); - } - else if(o instanceof String){ - record=record.concat(" = "+o); - if(e.hasMoreElements()) - record=record.concat(", "); - } - else { - record=("[FEHLER: string_rec: unbekannter Typ!!!!!! " +o); - isError = true; - } - } - record=record.concat("]"); - if(isError){ - parserlog.error(record); - }else{ - parserlog.debug(record); - } - }*/ - - /*static void string_rec(String st,Menge v) - { - String record=(st); - record=record.concat("{"); - for(Enumeration e=v.elements();e.hasMoreElements();) - { - Type t = (Type)e.nextElement(); - record=record.concat(" "+t.getName()); - if(e.hasMoreElements()) - { - record=record.concat(", "); - } - } - record=record.concat("}"); - parserlog.debug(record); - }*/ - ///////////////////////////////////////////////////////////////////////// // TypeReconstructionAlgorithmus @@ -771,165 +666,7 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I typinferenzLog.debug("Erstellte Constraints: "+oderConstraints, Section.TYPEINFERENCE); return oderConstraints; - - - /* - CReconstructionTupleSet retTupleSet = this.TRStart(methodList, V, V_fields_methods, supportData); - inferencelog.debug("Bin aus TRStart() zur�ck in TRProg()."); - ////////////////////////////// - // Neu Ergebnismenge A aller - // Typannahmen erzeugen: - ////////////////////////////// - inferencelog.debug("Erstelle Ergebnismenge..."); - Menge newA = new Menge(); - - // Alle bisherigen M�glichkeiten an Typkombinationen durchgehen: - Menge oldA = supportData.getA(); - for(int i=0; i retTupleIt = retTupleSet.getIterator(); - while(retTupleIt.hasNext()){ - CReconstructionTuple possibleTuple = retTupleIt.next(); - this.clear(possibleTuple.getAssumSet(), rememberLocals); - - // Neue Typinformationen mit alten Typinformationen vereinigen: - CTypeReconstructionResult newReconResult = oneReconResult.shallowCopy(); - newReconResult.addDataFromTupel(possibleTuple); - - - //PL 05-08-02 eingefuegt - //Fuegt Klassen und GenericTypeVars zu A hinzu - newReconResult.addClassName(this.getName()); - Menge genericsList = new Menge(); - for(int ii =0; ii getMethodList() { - - - if(this.methodList != null) return this.methodList; - //TODO: Unnötige Berechnungen im folgenden Code rauskürzen: - ////////////////////////////// - // Die Eingabedaten bauen: - ////////////////////////////// - inferencelog.debug("Baue Eingabedaten..."); - TypeAssumptions V_fields_methods = new TypeAssumptions(this.getName()); - Menge methodList = new Menge(); - Menge V = new Menge(); - Menge rememberLocals = new Menge(); - - ////////////////////////////// - // Alle Felder durchgehen: - // Zuerst alle Attribute, dann Methoden - // ge�ndert: hoth 06.04.2006 - - ////////////////////////////// - for(Field field : this.getFields()) - { - ////////////////////////////// - // Attribut: - ////////////////////////////// - - } - - for(Field field : this.getFields()) - { - ////////////////////////////// - // Methode: - ////////////////////////////// - if(field instanceof Method){ - Method method = (Method)field; - //if(method.get_Method_Name().equals(this.getName()) && ((method.getReturnType().equals(new mycompiler.mytype.Void(0))) || method.getReturnType() instanceof TypePlaceholder)){ - if(method.get_Method_Name().equals(this.getName()) ){ - method.set_Method_Name(""); - } - //hoth: 06.04.2006 - //durchlaufe Block und suche nach Objektvariablen fuer Offset-Markierung - Iterator fieldVarIterator = V_fields_methods.iterator(); - while (fieldVarIterator.hasNext()) - { - //Wenn ObjektVariable - CTypeAssumption dieAssum = fieldVarIterator.next(); - if(dieAssum instanceof CInstVarTypeAssumption) - { - Class.isFirstLocalVarDecl=false; - if(method.get_Block() != null) - method.get_Block().addOffsetsToAssumption(dieAssum,dieAssum.getIdentifier(),true); - } - } - - methodList.addElement(method); - - // F�r V_fields_methods: - CMethodTypeAssumption methodAssum = new CMethodTypeAssumption(this.getType(), method.get_Method_Name(), method.getReturnType(), method.getParameterCount(),method.getLineNumber(),method.getOffset(),new Menge(),method.getGenericMethodParameters()); // Typannahme bauen... - - - // Methode in V_Fields_methods ablegen - // Dabei wird die OverloadedMethodID ermittelt !! - // => Method setzenuct - - - V_fields_methods.add(methodAssum); - method.setOverloadedID(methodAssum.getHashSetKey().getOverloadedMethodID()); - - - // F�r die V_i: - CTypeAssumptionSet localAssum = new CTypeAssumptionSet(); - - // Bauen... - ParameterList parameterList = method.getParameterList(); - if(parameterList!=null){ - for(int i=0; i()); - //fuege Offsets fuer Parameter hinzu, hoth: 06.04.2006 - Class.isFirstLocalVarDecl=false; - - if(method.get_Block() != null) - method.get_Block().addOffsetsToAssumption(paraAssum,paraAssum.getIdentifier(),true); - - methodAssum.addParaAssumption(paraAssum); - - - - - // F�r die V_i: - CLocalVarTypeAssumption varAssum = new CLocalVarTypeAssumption(this.getName(), method.get_Method_Name(), method.getParameterCount(), method.getOverloadedID(),"1", para.getIdentifier(),para.getType(), para.getLineNumber(),para.getOffset(),new Menge()); - localAssum.addElement(varAssum); - rememberLocals.addElement(varAssum); - } - } - // ...und hinzuf�gen: - - V.addElement(localAssum); - } - } - - this.methodList = methodList; - return methodList; - - } -*/ /** * Ermittelt alle privaten Felder und Methoden der Klasse und Erstellt eine Assumption für diese. @@ -957,49 +694,8 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I this.typeAssumptions = assumptions; //Diese müssen anschließend nicht wieder generiert werden. return assumptions; -} - /* - public ConstraintsSet TYPE(Menge methodList, Menge fielddeclarationList, TypeAssumptions assumptions){ - ConstraintsSet ret = new ConstraintsSet(); - // Die Felddeklarationen werden zu den Assumptions hinzugefügt und gelten danach für jede Methode. - //TypeAssumptions assumptionsPlusFieldAssumptions = new TypeAssumptions(assumptions); - for(Expr expr : fielddeclarationList){ - //ret.add(expr.TYPEStmt(assumptionsPlusFieldAssumptions)); - ret.add(expr.TYPEStmt(assumptions)); - } - for(Method methode : methodList){ - //ret.add(methode.TYPE(assumptionsPlusFieldAssumptions)); - ret.add(methode.TYPE(assumptions)); - } - return ret; - } - */ + } - - // ino.method.RetType.23119.defdescription type=javadoc - /** - * Liefert den berechneten R�ckgabetyp f�r die �bergebene Methode zur�ck.
- * (siehe Algorithmus RetType, Martin Pl�micke) - *
Author: J�rg B�uerle - * @param me - * @param V - * @return - - // ino.end - // ino.method.RetType.23119.definition - private Type RetType(Method me, CTypeAssumptionSet V) - // ino.end - // ino.method.RetType.23119.body - { - CTypeAssumption assum = V.getElement(new CMethodKey(this.getName(), me.get_Method_Name(), me.getParameterCount(),me.getOverloadedID())); - if(assum instanceof CMethodTypeAssumption){ - return ((CMethodTypeAssumption)assum).getAssumedType(); - } - else return null; - } - // ino.end - - */ // ino.method.toString.23125.defdescription type=javadoc /** *
Author: Martin Pl�micke @@ -1037,48 +733,8 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I for(Field f : this.getFields()){ //f.wandleRefTypeAttributes2GenericAttributes(paralist); } - /* - Menge fieldsAndMethods=this.getFields(); - - // Alle Methoden und Instanzvariablen durchgehen - for(int i=0;i enumer = V_last.getHashtable().keys(); -// while(enumer.hasMoreElements()){ -// CTypeAssumption currentAssum = V_last.getElement(enumer.nextElement()); -// if(currentAssum instanceof CLocalVarTypeAssumption){ -// V_i.removeElement(currentAssum); -// } -// } -// -// } - - - // ino.method.getSimpleName.23140.defdescription type=javadoc /** * HOTI * Liefert bei Klassen die fullyQualified angegeben wurden @@ -1115,18 +771,6 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I */ public String printJavaCode(TypeinferenceResultSet reconstructionResult){ JavaCodeResult ret = new JavaCodeResult("class "); - - - - - //Generiere JavaCode der extends-Typen: - /* - String containedTypesJavaCode = ""; - for(Type containedType : containedTypes){ - containedTypesJavaCode += containedType.printJavaCode(resultSet)+ ", "; - } - if(containedTypesJavaCode.length()>1)containedTypesJavaCode = containedTypesJavaCode.substring(0, containedTypesJavaCode.length() -2); - */ JavaCodeResult classBodyCode = new JavaCodeResult(); if(this.modifiers!=null)classBodyCode.attach(this.modifiers.printJavaCode(reconstructionResult.getUnifiedConstraints())).attach(" "); @@ -1382,5 +1026,67 @@ public class Class extends GTVDeclarationContext implements AClassOrInterface, I public boolean isInterface(){ return false; } + + /* + private Collection getGenericClasses() { + Collection results = new Menge<>(); + + for(Field field : this.fielddecl){ + Type type = field.getType(); + //Der Type des Feldes + if(type instanceof RefType){ + RefType refType = (RefType) type; + + if(!refType.getCombinedType(null).equals(refType.get_Name().replace(".", "%"))){ + results.addAll(generateGenericClass(refType.getCombinedType(null), new Class("java/util/Vector",-1))); + } + } + + if(field instanceof Method){ + Method method = (Method) field; + ParameterList parameterList = method.getParameterList(); + + //Die Typen der Methodenparameter + for(FormalParameter parameter: parameterList){ + Type parameterType = parameter.getType(); + + if(parameterType instanceof RefType){ + RefType refType = (RefType) parameterType; + + if(!refType.getCombinedType(null).equals(refType.get_Name().replace(".", "%"))){ + results.addAll(generateGenericClass(refType.getCombinedType(null), new Class("java/util/Vector",-1))); + } + } + } + } + } + + return results; + } + */ + + /* + private Menge generateGenericClass(String name, Class superClass){ + //TODO: bytecode -- Generics hinzuf�gen + //Type superClassType = superClass.getType(); + + //TODO: bytecode + //ClassGenerator genericClassGenerator = new ClassGenerator(name, superClassType, name + ".java", Constants.ACC_PUBLIC , new String[] { }, new TypeinferenceResultSet(null, null, null)); + + //TODO: bytecode -- Namen der neuen Klasse + Class generatedClass = new Class(name, 0); + + //TODO: bytecode -- alle Konstruktoren generieren + Block konstruktorBlock = new Block(); + konstruktorBlock.setType(new de.dhbwstuttgart.syntaxtree.type.Void(konstruktorBlock, 0)); + konstruktorBlock.statements.add(new SuperCall(konstruktorBlock)); + Constructor standardKonstruktor = new Constructor(Method.createEmptyMethod(konstruktorBlock, name, superClass), superClass); + standardKonstruktor.parserPostProcessing(generatedClass); + + generatedClass.addField(standardKonstruktor); + + return generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())); + } + */ } // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index ba8cba98..3d4a60ce 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -541,8 +541,7 @@ public class Method extends Field implements IItemWithOffset, TypeInsertable ret.add(this.block.TYPEStmt(localAss)); // eine Verknüpfung mit der Type Assumption aus dem Assumption Set // und dem ermittelten Typ der Methode: - ret.add(new SingleConstraint(this.block.getType().TYPE(localAss, this), - this.returntype.TYPE(localAss, this))); + ret.add(new SingleConstraint(this.block.getType().TYPE(localAss, this), this.returntype.TYPE(localAss, this))); return ret; } diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index f96cc32e..56222f1a 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -1834,7 +1834,7 @@ public class SourceFile * Bisher wird nur der Bytecode der Klassen generiert. Nicht der Interfaces. * @return */ - public Collection generateBytecode(TypeinferenceResultSet rs) { + public Menge generateBytecode(TypeinferenceResultSet rs) { Menge ret = new Menge<>(); for(Class cl : this.KlassenVektor){ ret.add(cl.genByteCode(rs)); diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java new file mode 100644 index 00000000..557ff35b --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -0,0 +1,56 @@ +package de.dhbwstuttgart.syntaxtree.factory; + +import de.dhbwstuttgart.bytecode.ClassGenerator; +import de.dhbwstuttgart.syntaxtree.Class; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.misc.DeclId; +import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.ResultSet; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; + +public class ASTFactory { + public static Method createMethod(String name, Block block, Class parent) { + block.parserPostProcessing(parent); + + Method method = new Method(0); + DeclId DImethod = new DeclId(); + DImethod.set_Name(name); + method.set_DeclId(DImethod); + method.set_Block(block); + method.parserPostProcessing(parent); + return method; + } + + public static Method createEmptyMethod(String withSignature, Class parent) { + return ASTFactory.createMethod(withSignature, new Block(), parent); + } + + public static Constructor createEmptyConstructor(Class parent){ + Block block = new Block(); + block.setType(new de.dhbwstuttgart.syntaxtree.type.Void(block, 0)); + block.statements.add(new SuperCall(block)); + + return ASTFactory.createConstructor(parent, block); + } + + public static Constructor createConstructor(Class superClass, Block block){ + block.parserPostProcessing(superClass); + + Method method = ASTFactory.createMethod("", block, superClass); + method.setType(new de.dhbwstuttgart.syntaxtree.type.Void(block, 0)); + + return new Constructor(method, superClass); + } + + public static Class createClass(String className, Class superClass) { + // TODO bytecode createClass + Class generatedClass = new Class(className, 0); + generatedClass.addField(ASTFactory.createEmptyConstructor(superClass)); + generatedClass.parserPostProcessing(superClass); + + return generatedClass; + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java index 60b2657b..73467924 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java @@ -6,8 +6,13 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; +import org.apache.commons.bcel6.generic.BranchInstruction; import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.GOTO; +import org.apache.commons.bcel6.generic.IF_ICMPNE; +import org.apache.commons.bcel6.generic.InstructionConstants; import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.NOP; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; @@ -228,8 +233,22 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) { if(this.getReturnType().getName().equals(new JavaClassName("String"))){ throw new TypeinferenceException("Zeichenketten zusammenfügen ist noch nicht unterstützt",this); } - //TODO: Je nachdem welches der Rückgabetyp ist, müssen die linke und rechte Seite unboxt und addiert werden. - return null; + + linkeSeite.append(rechteSeite); + + //TODO: bytecode (Bis jetzt wird jeder Condition als EQUALS behandelt) + //TODO: bytecode autoboxing + + BranchInstruction if_icmpneInstruction = new IF_ICMPNE(null); + linkeSeite.append(if_icmpneInstruction); + linkeSeite.append(InstructionConstants.ICONST_1); + + BranchInstruction gotoInstruction = new GOTO(null); + + linkeSeite.append(gotoInstruction); + if_icmpneInstruction.setTarget(linkeSeite.append(InstructionConstants.ICONST_0)); + gotoInstruction.setTarget(linkeSeite.append(new NOP())); + return linkeSeite; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java index 6f5ab521..dc8526ae 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java @@ -4,8 +4,13 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Iterator; +import org.apache.commons.bcel6.generic.BranchInstruction; import org.apache.commons.bcel6.generic.ClassGen; +import org.apache.commons.bcel6.generic.GOTO; +import org.apache.commons.bcel6.generic.IF_ICMPGE; +import org.apache.commons.bcel6.generic.InstructionHandle; import org.apache.commons.bcel6.generic.InstructionList; +import org.apache.commons.bcel6.generic.NOP; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; @@ -22,10 +27,13 @@ import de.dhbwstuttgart.syntaxtree.operator.RelOp; import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.Type; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typeinference.ConstraintsSet; import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.ResultSet; +import de.dhbwstuttgart.typeinference.SingleConstraint; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; import de.dhbwstuttgart.typeinference.unify.Unify; @@ -91,8 +99,13 @@ public class ForStmt extends Statement @Override public ConstraintsSet TYPEStmt(TypeAssumptions assumptions) { - // TODO Auto-generated method stub - return null; + //TODO: TYPEStmt + ConstraintsSet ret = new ConstraintsSet(); + this.setType(TypePlaceholder.fresh(this)); + + ret.add(body_Loop_block.TYPEStmt(assumptions)); + + return ret; } public int getTypeLineNumber() { @@ -100,9 +113,9 @@ public class ForStmt extends Statement } @Override -public JavaCodeResult printJavaCode(ResultSet resultSet) { - // TODO Auto-generated method stub - throw new NotImplementedException(); + public JavaCodeResult printJavaCode(ResultSet resultSet) { + // TODO printJavaCode + return new JavaCodeResult(); } @Override @@ -121,7 +134,34 @@ public JavaCodeResult printJavaCode(ResultSet resultSet) { @Override public InstructionList genByteCode(ClassGenerator _cg) { - // TODO Bytecode - throw new NotImplementedException(); + /* + 0: iconst_0 + 1: istore_1 + 2: iload_1 + 3: bipush 10 + 5: if_icmpge 14 + 8: iinc 1, 1 + 11: goto 2 + 14: return + */ + + InstructionList il = new InstructionList(); + + il.append(head_Initializer.genByteCode(_cg)); + + InstructionHandle ih = il.append(head_Condition.genByteCode(_cg)); + + BranchInstruction branch = new IF_ICMPGE(null); + il.append(branch); + + il.append(body_Loop_block.genByteCode(_cg)); + + il.append(head_Loop_expr.genByteCode(_cg)); + + il.append(new GOTO(ih)); + + branch.setTarget(il.append(new NOP())); + + return il; } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index 5a7488c7..0998e4d3 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -178,9 +178,8 @@ public class IfStmt extends Statement } @Override - public InstructionList genByteCode(ClassGenerator _cg) { - InstructionFactory _factory = new InstructionFactory(_cg, _cg.getConstantPool()); - + public InstructionList genByteCode(ClassGenerator _cg) { + InstructionFactory _factory = _cg.getInstructionFactory(); InstructionList il = new InstructionList(); IfInstruction ifInstruction = new IFEQ(null); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java index 8a39cbd0..50c2bdf8 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalOrFieldVar.java @@ -13,6 +13,7 @@ import org.apache.commons.bcel6.generic.ObjectType; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.logger.Logger; +import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.JVMCodeException; import de.dhbwstuttgart.myexception.SCExcept; @@ -152,7 +153,13 @@ public class LocalOrFieldVar extends Expr if(this.isFieldAccess){ il.append(cg.getInstructionFactory().createFieldAccess(this.getParentClass().getName().toString(), this.get_Name(), this.getType().getBytecodeType(cg), Constants.GETFIELD)); } - il.append(cg.getInstructionFactory().createLoad(this.getType().getBytecodeType(cg), this.get_Name())); + + Type type = this.getType(); + org.apache.commons.bcel6.generic.Type byteCodeType = type.getBytecodeType(cg); + + String name = this.get_Name(); + + il.append(cg.getInstructionFactory().createLoad(byteCodeType, name)); return il; } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/FreshWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/FreshWildcardType.java index 33f153e0..40ddb895 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/FreshWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/FreshWildcardType.java @@ -8,7 +8,7 @@ import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException; -public class FreshWildcardType extends Type { +public class FreshWildcardType extends ObjectType { private static String strNextName = "A"; private Menge upperBounds = new Menge(); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 5b96e961..ef49744d 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -14,11 +14,17 @@ import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.myexception.SCException; import de.dhbwstuttgart.parser.JavaClassName; import de.dhbwstuttgart.syntaxtree.Class; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.misc.UsedId; +import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.TypeInsertable; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; import de.dhbwstuttgart.typeinference.unify.CSubstitutionGenVar; @@ -814,11 +820,12 @@ public class RefType extends ObjectType implements IMatchable } public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { - return new org.apache.commons.bcel6.generic.ObjectType(this.getTypeName()); + return new org.apache.commons.bcel6.generic.ObjectType(getCombinedType(cg)); } @Override public String getBytecodeSignature(ClassGenerator cg) { + /* String paramString = ""; if(this.parameter != null && this.parameter.size()>0){ paramString+="<"; @@ -826,14 +833,42 @@ public class RefType extends ObjectType implements IMatchable while(it.hasNext()){ Type param = it.next(); paramString+=param.getBytecodeSignature(cg); - //if(it.hasNext()) - //paramString+=";"; //kein Delimiter zwischen den Typen } paramString+=">"; } String typeSignature = this.getBytecodeType(cg).getSignature(); typeSignature = typeSignature.substring(0, typeSignature.length()-1); return typeSignature+paramString+";"; + */ + + //TODO: bytecode woher bekommt ich die parent klasse + String combinedType = getCombinedType(cg); + if(!combinedType.equals(get_Name())){ + // TODO bytecode createClass + Class generatedClass = ASTFactory.createClass(getCombinedType(cg), getParentClass()); + + cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())).getByteCode()); + } + + return "L"+getCombinedType(cg)+";"; + } + + public String getCombinedType(ClassGenerator cg){ + //Bsp.: Ljava/util/Vector; + StringBuilder sb = new StringBuilder(); + + if(parameter != null && parameter.size() > 0){ + sb.append(this.get_Name().replace(".", "%")); + sb.append("%%"); + for(Type type: parameter){ + sb.append(((RefType) type).getCombinedType(cg).replace(".", "%")); + sb.append("%"); + } + }else{ + sb.append(this.get_Name()); + } + + return sb.toString(); } } diff --git a/src/de/dhbwstuttgart/typeinference/ByteCodeResult.java b/src/de/dhbwstuttgart/typeinference/ByteCodeResult.java index c036b5da..8ed910ba 100644 --- a/src/de/dhbwstuttgart/typeinference/ByteCodeResult.java +++ b/src/de/dhbwstuttgart/typeinference/ByteCodeResult.java @@ -2,23 +2,26 @@ package de.dhbwstuttgart.typeinference; import java.io.File; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import org.apache.commons.bcel6.generic.ClassGen; import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; public class ByteCodeResult{ - private ClassGen byteCode; + private ClassGenerator byteCode; //TODO: unresolvedTPHs entfernen. BROKEN! private Menge unresolvedTPHs = new Menge(); - public ByteCodeResult(ClassGen byteCode){ + public ByteCodeResult(ClassGenerator byteCode){ this.byteCode = byteCode; } - public ClassGen getByteCode(){ + public ClassGenerator getByteCode(){ return byteCode; } diff --git a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java index 9a38d298..5dbf463c 100755 --- a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java +++ b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java @@ -119,8 +119,7 @@ public class TypeinferenceResultSet * Dabei wird die codegen-Methode der inferierten Klasse mit diesem ResultSet aufgerufen. */ public ByteCodeResult codegen(){ - ByteCodeResult res = this.ownerOfResultSet.genByteCode(this); - return res; + return this.ownerOfResultSet.genByteCode(this); } } diff --git a/test/bytecode/BytecodeTest.java b/test/bytecode/BytecodeTest.java new file mode 100644 index 00000000..35b8dc33 --- /dev/null +++ b/test/bytecode/BytecodeTest.java @@ -0,0 +1,57 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; + +import junit.framework.TestCase; + +import org.junit.BeforeClass; +import org.junit.Test; + +public abstract class BytecodeTest extends TestCase{ + public String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public String testFile; + public String outputDirectory; + + protected String testName; + + public BytecodeTest(){ + init(); + + if(testName != null){ + + testFile = testName+".jav"; + outputDirectory = ""; + + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputDirectory); + }else{ + throw new RuntimeException("rootDirectory, testFile or outputFile is null."); + } + } + + protected abstract void init(); + + protected Class getClassToTest(){ + Class classToTest = null; + try { + File file = new File(rootDirectory); + URL url = file.toURL(); + URL[] urls = new URL[]{url}; + + ClassLoader classLoader = new URLClassLoader(urls); + + classToTest = classLoader.loadClass(testName); + + + } catch (Exception e) { + throw new RuntimeException(e); + } + + return classToTest; + } +} diff --git a/test/bytecode/Condition.jav b/test/bytecode/Condition.jav new file mode 100644 index 00000000..4f68e8ab --- /dev/null +++ b/test/bytecode/Condition.jav @@ -0,0 +1,7 @@ +class Condition{ + + Boolean method() { + return 0 == 1; + } + +} \ No newline at end of file diff --git a/test/bytecode/ConditionTest.java b/test/bytecode/ConditionTest.java new file mode 100644 index 00000000..851d6426 --- /dev/null +++ b/test/bytecode/ConditionTest.java @@ -0,0 +1,34 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +import org.junit.Test; + +import plugindevelopment.TypeInsertTester; +import de.dhbwstuttgart.core.MyCompiler; +import de.dhbwstuttgart.core.MyCompilerAPI; +import de.dhbwstuttgart.logger.LoggerConfiguration; +import de.dhbwstuttgart.logger.Section; +import de.dhbwstuttgart.parser.JavaParser.yyException; +import de.dhbwstuttgart.typeinference.ByteCodeResult; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; +import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet; + +public class ConditionTest { + + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public final static String testFile = "Condition.jav"; + public final static String outputFile = "Condition.class"; + + @Test + public void test() { + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); + } + +} diff --git a/test/bytecode/ForTest.jav b/test/bytecode/ForTest.jav index 7343a1b8..649b71b0 100644 --- a/test/bytecode/ForTest.jav +++ b/test/bytecode/ForTest.jav @@ -1,6 +1,6 @@ class IfElse{ method(){ - while(true){ + for( i = 0; i < 10 ; i = i++){ } } diff --git a/test/bytecode/ForTest.java b/test/bytecode/ForTest.java index fea1e7c5..497dc375 100644 --- a/test/bytecode/ForTest.java +++ b/test/bytecode/ForTest.java @@ -1,5 +1,6 @@ package bytecode; +import org.junit.Ignore; import org.junit.Test; public class ForTest { @@ -8,6 +9,7 @@ public class ForTest { public final static String outputFile = "ForTest.class"; @Test + @Ignore public void test() { SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); } diff --git a/test/bytecode/IfStatement.jav b/test/bytecode/IfStatement.jav index 476c9bc0..d9fdb16c 100644 --- a/test/bytecode/IfStatement.jav +++ b/test/bytecode/IfStatement.jav @@ -1,7 +1,7 @@ class IfStatement{ method(flag){ if(flag){ - + } } } \ No newline at end of file diff --git a/test/bytecode/LambdaExpr2.java b/test/bytecode/LambdaExpr2.java index ff607c6c..83308ca5 100644 --- a/test/bytecode/LambdaExpr2.java +++ b/test/bytecode/LambdaExpr2.java @@ -7,6 +7,7 @@ import java.io.IOException; import junit.framework.TestCase; +import org.junit.Ignore; import org.junit.Test; import plugindevelopment.TypeInsertTester; @@ -27,6 +28,7 @@ public class LambdaExpr2 { public final static String outputFile = "LambdaExpr2.class"; @Test + @Ignore public void test() { SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); } diff --git a/test/bytecode/PostDecrement.java b/test/bytecode/PostDecrement.java index 075cf135..b0dbee20 100644 --- a/test/bytecode/PostDecrement.java +++ b/test/bytecode/PostDecrement.java @@ -7,6 +7,7 @@ import java.io.IOException; import junit.framework.TestCase; +import org.junit.Ignore; import org.junit.Test; import plugindevelopment.TypeInsertTester; @@ -27,6 +28,7 @@ public class PostDecrement { public final static String outputFile = "PostDecrement.class"; @Test + @Ignore public void test() { SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); } diff --git a/test/bytecode/PostIncrement.java b/test/bytecode/PostIncrement.java index 8c0b3930..da1fe1e3 100644 --- a/test/bytecode/PostIncrement.java +++ b/test/bytecode/PostIncrement.java @@ -7,6 +7,7 @@ import java.io.IOException; import junit.framework.TestCase; +import org.junit.Ignore; import org.junit.Test; import plugindevelopment.TypeInsertTester; @@ -27,6 +28,7 @@ public class PostIncrement { public final static String outputFile = "PostIncrement.class"; @Test + @Ignore public void test() { SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); } diff --git a/test/bytecode/SingleClassTester.java b/test/bytecode/SingleClassTester.java index bbee95d6..46693780 100644 --- a/test/bytecode/SingleClassTester.java +++ b/test/bytecode/SingleClassTester.java @@ -2,10 +2,14 @@ package bytecode; import java.io.File; import java.io.IOException; +import java.util.Map; + +import org.apache.commons.bcel6.classfile.JavaClass; import com.google.common.io.Files; import junit.framework.TestCase; +import de.dhbwstuttgart.bytecode.ClassGenerator; import de.dhbwstuttgart.core.MyCompiler; import de.dhbwstuttgart.core.MyCompilerAPI; import de.dhbwstuttgart.logger.Logger; @@ -13,24 +17,44 @@ import de.dhbwstuttgart.logger.LoggerConfiguration; import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.logger.Timewatch; import de.dhbwstuttgart.parser.JavaParser.yyException; +import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typeinference.ByteCodeResult; import de.dhbwstuttgart.typeinference.Menge; public class SingleClassTester { - public static void compileToBytecode(String inputFile, String outputFile){ + public static void compileToBytecode(String inputFile, String outputDirectory){ LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out); MyCompilerAPI compiler = MyCompiler.getAPI(logConfig); try { - compiler.parse(new File(inputFile)); - Menge bytecode = compiler.generateBytecode(compiler.typeReconstruction().firstElement()); - System.out.println(bytecode); - bytecode.firstElement().getByteCode().getJavaClass().dump(new File(outputFile)); + SourceFile sf = compiler.parse(new File(inputFile)); + + Menge sourceFiles = new Menge<>(); + sourceFiles.add(sf); + + Menge bytecode = compiler.generateBytecode(sourceFiles, compiler.typeReconstruction(sourceFiles).firstElement()); + //System.out.println(bytecode); + + ByteCodeResult result = bytecode.firstElement(); + + 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")); + } + + Logger.getLogger("SingleClassTester").error(result.getByteCode().getJavaClass().toString(), Section.CODEGEN); + + + } catch (IOException | yyException e) { + Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN); e.printStackTrace(); TestCase.fail(); }finally{ - writeLog(outputFile+".log"); + writeLog(outputDirectory+".log"); } } diff --git a/test/bytecode/types/ExtendsObject.jav b/test/bytecode/types/ExtendsObject.jav new file mode 100644 index 00000000..758def9c --- /dev/null +++ b/test/bytecode/types/ExtendsObject.jav @@ -0,0 +1,3 @@ +class ExtendsObject extends Object{ + +} \ No newline at end of file diff --git a/test/bytecode/types/ExtendsObjectTest.java b/test/bytecode/types/ExtendsObjectTest.java new file mode 100644 index 00000000..43648fdf --- /dev/null +++ b/test/bytecode/types/ExtendsObjectTest.java @@ -0,0 +1,48 @@ +package bytecode.types; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.junit.Test; + +import bytecode.BytecodeTest; +import plugindevelopment.TypeInsertTester; +import de.dhbwstuttgart.core.MyCompiler; +import de.dhbwstuttgart.core.MyCompilerAPI; +import de.dhbwstuttgart.logger.LoggerConfiguration; +import de.dhbwstuttgart.logger.Section; +import de.dhbwstuttgart.parser.JavaParser.yyException; +import de.dhbwstuttgart.typeinference.ByteCodeResult; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; +import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet; + +public class ExtendsObjectTest extends BytecodeTest{ + @Override + protected void init() { + testName = "ExtendsObject"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + public void testConstruct(){ + try{ + Class cls = getClassToTest(); + + Constructor method = cls.getConstructor(new Class[]{}); + method.newInstance(); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + +} diff --git a/test/bytecode/types/ExtendsVector.jav b/test/bytecode/types/ExtendsVector.jav new file mode 100644 index 00000000..f029967d --- /dev/null +++ b/test/bytecode/types/ExtendsVector.jav @@ -0,0 +1,5 @@ +import java.util.Vector; + +class ExtendsVector extends Vector{ + +} \ No newline at end of file diff --git a/test/bytecode/types/ExtendsVectorString.jav b/test/bytecode/types/ExtendsVectorString.jav new file mode 100644 index 00000000..7829ecd0 --- /dev/null +++ b/test/bytecode/types/ExtendsVectorString.jav @@ -0,0 +1,5 @@ +import java.util.Vector; + +class ExtendsVectorString extends Vector{ + +} \ No newline at end of file diff --git a/test/bytecode/types/ExtendsVectorStringTest.java b/test/bytecode/types/ExtendsVectorStringTest.java new file mode 100644 index 00000000..8511499b --- /dev/null +++ b/test/bytecode/types/ExtendsVectorStringTest.java @@ -0,0 +1,43 @@ +package bytecode.types; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.junit.Test; + +import bytecode.BytecodeTest; +import plugindevelopment.TypeInsertTester; +import de.dhbwstuttgart.core.MyCompiler; +import de.dhbwstuttgart.core.MyCompilerAPI; +import de.dhbwstuttgart.logger.LoggerConfiguration; +import de.dhbwstuttgart.logger.Section; +import de.dhbwstuttgart.parser.JavaParser.yyException; +import de.dhbwstuttgart.typeinference.ByteCodeResult; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; +import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet; + +public class ExtendsVectorStringTest extends BytecodeTest{ + @Override + protected void init() { + testName = "ExtendsVectorString"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + public void testConstruct() throws Exception{ + Class cls = getClassToTest(); + + Constructor method = cls.getConstructor(new Class[]{}); + method.newInstance(); + assertTrue(true); + } + +} diff --git a/test/bytecode/types/ExtendsVectorTest.java b/test/bytecode/types/ExtendsVectorTest.java new file mode 100644 index 00000000..aefe5d4b --- /dev/null +++ b/test/bytecode/types/ExtendsVectorTest.java @@ -0,0 +1,48 @@ +package bytecode.types; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.junit.Test; + +import bytecode.BytecodeTest; +import plugindevelopment.TypeInsertTester; +import de.dhbwstuttgart.core.MyCompiler; +import de.dhbwstuttgart.core.MyCompilerAPI; +import de.dhbwstuttgart.logger.LoggerConfiguration; +import de.dhbwstuttgart.logger.Section; +import de.dhbwstuttgart.parser.JavaParser.yyException; +import de.dhbwstuttgart.typeinference.ByteCodeResult; +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; +import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertSet; + +public class ExtendsVectorTest extends BytecodeTest{ + @Override + protected void init() { + testName = "ExtendsVector"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + public void testConstruct(){ + try{ + Class cls = getClassToTest(); + + Constructor method = cls.getConstructor(new Class[]{}); + method.newInstance(); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + +} diff --git a/test/bytecode/types/Overloading.jav b/test/bytecode/types/Overloading.jav new file mode 100644 index 00000000..ce94fb7d --- /dev/null +++ b/test/bytecode/types/Overloading.jav @@ -0,0 +1,12 @@ +import java.util.Vector; + +class Overloading{ + + void method(Vector v) { + + } + + void method(Vector v) { + + } +} \ No newline at end of file diff --git a/test/bytecode/types/OverloadingTest.java b/test/bytecode/types/OverloadingTest.java new file mode 100644 index 00000000..9e6b3a22 --- /dev/null +++ b/test/bytecode/types/OverloadingTest.java @@ -0,0 +1,61 @@ +package bytecode.types; + +import static org.junit.Assert.*; + +import java.lang.reflect.Method; +import java.util.Vector; + +import org.junit.Test; + +import bytecode.BytecodeTest; + +public class OverloadingTest extends BytecodeTest{ + @Override + protected void init() { + testName = "Overloading"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + + + @Test + public void testString() { + try{ + Class cls = getClassToTest(); + Object obj = cls.newInstance(); + + Vector stringVector = new Vector(); + + Class[] params = new Class[1]; + params[0] = stringVector.getClass(); + + Method method = cls.getDeclaredMethod("method", params); + method.invoke(obj, stringVector); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + + @Test + public void testInteger() { + try{ + Class cls = getClassToTest(); + Object obj = cls.newInstance(); + + Vector stringVector = new Vector(); + + Class[] params = new Class[1]; + params[0] = stringVector.getClass(); + + Method method = cls.getDeclaredMethod("method", params); + method.invoke(obj, stringVector); + assertTrue(true); + }catch(Exception e){ + e.printStackTrace(); + fail(); + } + } + +} diff --git a/test/bytecode/types/TypedVector.jav b/test/bytecode/types/TypedVector.jav new file mode 100644 index 00000000..d64e4f5f --- /dev/null +++ b/test/bytecode/types/TypedVector.jav @@ -0,0 +1,7 @@ +import java.util.Vector; + +class TypedVector{ + public void method(Vector v) { + + } +} \ No newline at end of file diff --git a/test/bytecode/types/TypedVectorTest.java b/test/bytecode/types/TypedVectorTest.java new file mode 100644 index 00000000..ef32180b --- /dev/null +++ b/test/bytecode/types/TypedVectorTest.java @@ -0,0 +1,41 @@ +package bytecode.types; + +import static org.junit.Assert.*; + +import java.lang.reflect.Method; +import java.util.Stack; +import java.util.Vector; + +import org.junit.Test; + +import bytecode.BytecodeTest; +import de.dhbwstuttgart.logger.Logger; +import de.dhbwstuttgart.logger.Section; + +public class TypedVectorTest extends BytecodeTest{ + @Override + protected void init() { + testName = "TypedVector"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + public void test() { + try{ + Class cls = getClassToTest(); + Object obj = cls.newInstance(); + + Vector stringVector = new Vector(); + + Class[] params = new Class[1]; + params[0] = stringVector.getClass(); + + Method method = cls.getDeclaredMethod("method", params); + method.invoke(obj, stringVector); + assertTrue(true); + }catch(Exception e){ + Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN); + fail(); + } + } +}