Skip to content

Commit 75fb847

Browse files
author
lexi
committed
CODEMODEL-10 Implemented the istanceInit() method, added a unit test using the JavaParser.
git-svn-id: https://svn.java.net/svn/codemodel~svn@325 1b0cd94c-d507-0410-907c-c8211361794a
1 parent fde6449 commit 75fb847

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

trunk/codemodel/codemodel/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,11 @@
6060
<version>4.8.1</version>
6161
<scope>test</scope>
6262
</dependency>
63+
<dependency>
64+
<groupId>com.google.code.javaparser</groupId>
65+
<artifactId>javaparser</artifactId>
66+
<version>1.0.8</version>
67+
<scope>test</scope>
68+
</dependency>
6369
</dependencies>
6470
</project>

trunk/codemodel/codemodel/src/main/java/com/sun/codemodel/JDefinedClass.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public class JDefinedClass
8585

8686
/** Static initializer, if this class has one */
8787
private JBlock init = null;
88+
89+
/** Instance initializer, if this class has one */
90+
private JBlock instanceInit = null;
8891

8992
/** class javadoc */
9093
private JDocComment jdoc = null;
@@ -532,6 +535,18 @@ public JBlock init() {
532535
return init;
533536
}
534537

538+
/**
539+
* Creates, if necessary, and returns the instance initializer
540+
* for this class.
541+
*
542+
* @return JBlock containing initialization statements for this class
543+
*/
544+
public JBlock instanceInit() {
545+
if (instanceInit == null)
546+
instanceInit = new JBlock();
547+
return instanceInit;
548+
}
549+
535550
/**
536551
* Adds a constructor to this class.
537552
*
@@ -808,6 +823,8 @@ protected void declareBody(JFormatter f) {
808823
f.d(field);
809824
if (init != null)
810825
f.nl().p("static").s(init);
826+
if (instanceInit != null)
827+
f.nl().s(instanceInit);
811828
for (JMethod m : constructors) {
812829
f.nl().d(m);
813830
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
/**
42+
* Output all source files into a single stream.
43+
*
44+
* This is primarily for test purposes.
45+
*
46+
* @author
47+
* Aleksei Valikov (valikov@gmx.net)
48+
*/
49+
package com.sun.codemodel.writer;
50+
51+
import java.io.FilterOutputStream;
52+
import java.io.IOException;
53+
import java.io.OutputStream;
54+
import java.io.PrintStream;
55+
import java.io.UnsupportedEncodingException;
56+
57+
import com.sun.codemodel.CodeWriter;
58+
import com.sun.codemodel.JPackage;
59+
60+
public class OutputStreamCodeWriter extends CodeWriter {
61+
private final PrintStream out;
62+
63+
/**
64+
* @param os
65+
* This stream will be closed at the end of the code generation.
66+
*/
67+
public OutputStreamCodeWriter(OutputStream os, String encoding) {
68+
try {
69+
this.out = new PrintStream(os, false, encoding);
70+
} catch (UnsupportedEncodingException ueex) {
71+
throw new IllegalArgumentException(ueex);
72+
}
73+
this.encoding = encoding;
74+
}
75+
76+
public OutputStream openBinary(JPackage pkg, String fileName)
77+
throws IOException {
78+
return new FilterOutputStream(out) {
79+
public void close() {
80+
// don't let this stream close
81+
}
82+
};
83+
}
84+
85+
public void close() throws IOException {
86+
out.close();
87+
}
88+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.sun.codemodel.tests;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import japa.parser.JavaParser;
5+
import japa.parser.ast.CompilationUnit;
6+
import japa.parser.ast.body.ClassOrInterfaceDeclaration;
7+
import japa.parser.ast.body.InitializerDeclaration;
8+
import japa.parser.ast.body.TypeDeclaration;
9+
10+
import java.io.ByteArrayInputStream;
11+
import java.io.ByteArrayOutputStream;
12+
13+
import org.junit.Test;
14+
15+
import com.sun.codemodel.JCodeModel;
16+
import com.sun.codemodel.JDefinedClass;
17+
import com.sun.codemodel.JExpr;
18+
import com.sun.codemodel.JFieldVar;
19+
import com.sun.codemodel.JMod;
20+
import com.sun.codemodel.writer.OutputStreamCodeWriter;
21+
22+
public class JDefinedClassInstanceInitTest {
23+
24+
@Test
25+
public void generatesInstanceInit() throws Exception {
26+
JCodeModel cm = new JCodeModel();
27+
JDefinedClass c = cm._package("myPackage")._class(0, "MyClass");
28+
JFieldVar myField = c.field(JMod.PRIVATE, String.class, "myField");
29+
c.instanceInit().assign(JExpr._this().ref(myField),
30+
JExpr.lit("myValue"));
31+
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
32+
final String encoding = "UTF-8";
33+
cm.build(new OutputStreamCodeWriter(bos, encoding));
34+
bos.close();
35+
36+
final ByteArrayInputStream bis = new ByteArrayInputStream(
37+
bos.toByteArray());
38+
39+
CompilationUnit compilationUnit = JavaParser.parse(bis, encoding);
40+
41+
TypeDeclaration typeDeclaration = compilationUnit.getTypes().get(0);
42+
ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
43+
44+
final InitializerDeclaration initializerDeclaration = (InitializerDeclaration) classDeclaration
45+
.getMembers().get(1);
46+
47+
assertNotNull(initializerDeclaration);
48+
}
49+
}

0 commit comments

Comments
 (0)