私はこれを試していて、しばらく試していましたが、私はそれを解決できないようです。Java - ファイルへのインタフェースの抽出
私はIDEで、形状のUMLダイアグラム内のJavaファイルおよびプリントからクラスを抽出することになったり、ファイルに書いています
例えば
public class Complex {
private int re;
private int im;
public Complex(int re, int im) {
this.re = re;
this.im = im;
}
public Complex add(Complex h) {
return new Complex(this.re + h.re, this.im + h.im);
}
public Complex sub(Complex h) {
return new Complex(this.re - h.re, this.im - h.im);
}
public Complex mul(Complex h) {
int a = re;
int b = im;
int c = h.re;
int d = h.im;
return new Complex(a * c - b * d, b * c + a * d);
}
public Complex div(Complex h) {
int a = re;
int b = im;
int c = h.re;
int d = h.im;
return new Complex((a * c + b * d)/(c * c + d * d), (b * c - a * d)
/(c * c + d * d));
}
public String toString() {
if (im >= 0) {
return re + " + " + im + "i";
} else
return re + " " + im + "i";
}
}
のようなものを生成する必要がありますプログラム:
a Complex
b int re
b int re
a Complex(re:int, im:int)
a add():Complex
a sub():Complex
a mul():Complex
a div():Complex
a toString():String
a main()
ファイルから最初の括弧を取り除いて開始しました。
import java.io.*;
public class ClassExtract {
public static void main(String[] args) {
ClassExtract obj = new ClassExtract();
obj.removeBracket("Complexx.txt");
}
public void removeBracket(String filnamn) {
try {
File f = new File(filnamn);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("noparanthesis_" + filnamn);
BufferedWriter bw = new BufferedWriter(fw);
String rad = br.readLine();
while (rad != null) {
rad = rad.replaceAll("\\(", " ");
bw.write(rad);
bw.newLine();
rad = br.readLine();
}
bw.close();
br.close();
} catch (FileNotFoundException e) {
System.out.printf("The file " + filnamn + " was not found.");
} catch (IOException e) {
System.out.printf("Writing error.");
}
}
}
私はこの問題に近づくさまざまな方法を考えました。など
public Complex int re, int im
public Complex add Complex h
public Complex sub Complex h
と同じことを行うとのインデックスを読む:ようになり、ファイルの残りの部分を意味するだろう頭public class Complex
を、収集した後、私は最も簡単なことだと思うの方法は、すべてを取り除くことであろうライン。
私は実際には非常に紛失していると感じています。この問題に取り組むのは難しいです。どんな助けでも本当に感謝しています。
なぜ反射を使用しないのですか? –
あなたはjavaを解析する必要がありますか?コンパイルされたクラスをクラスローダーでロードしてリフレクションを使用できますか? – stew
ええ、私は.javaファイルを読んで結果を持つ.txtファイルを作るか、IDEコンソールに直接持っていなければなりません。 @Qwe 私は今、リフレクションについて読んでいます。それはクラスなどを取得するので便利だと思います。しかし、少し具体的な例が、私がそれを実装できると思う方法をたくさん助けることができます。 – Dannzor