use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class JavadocGenerator method getSourceIndent.
/**
* Fetch the leading whitespace from the comment line. Since the JDT
* strips leading and trailing whitespace from lines, the original
* source is fetched and is walked backwards from the fragment's start
* until the previous new line, then moved forward if there is a leading
* "* ".
*/
private String getSourceIndent(TreeNode fragment) {
int index = fragment.getStartPosition();
if (index < 1) {
return "";
}
TreeNode node = fragment.getParent();
while (node != null && node.getKind() != TreeNode.Kind.COMPILATION_UNIT) {
node = node.getParent();
}
if (node instanceof CompilationUnit) {
String source = ((CompilationUnit) node).getSource();
int i = index - 1;
char c;
while (i >= 0 && (c = source.charAt(i)) != '\n') {
if (c != '*' && !Character.isWhitespace(c)) {
// Pre tag embedded in other text, so no indent.
return "";
}
--i;
}
String lineStart = source.substring(i + 1, index);
i = lineStart.indexOf('*');
if (i == -1) {
return lineStart;
}
// Indent could end with '*' instead of "* ", if there's no text after it.
return (++i + 1) < lineStart.length() ? lineStart.substring(i + 1) : lineStart.substring(i);
}
return "";
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class SwitchRewriterTest method testVariableDeclarationsInSwitchStatement2.
public void testVariableDeclarationsInSwitchStatement2() throws IOException {
CompilationUnit unit = translateType("A", "public class A { public void doSomething(int i) { switch (i) { " + "case 1: int j = i * 2; log(j); break; " + "case 2: log(i); break; " + "case 3: log(i); int k = i, l = 42; break; }}" + "private void log(int i) {}}");
TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
// First MethodDeclaration is the implicit default constructor.
MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(1);
List<Statement> stmts = method.getBody().getStatements();
assertEquals(1, stmts.size());
Block block = (Block) stmts.get(0);
stmts = block.getStatements();
if (options.isJDT()) {
assertEquals(3, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof SwitchStatement);
} else {
assertEquals(4, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(3) instanceof SwitchStatement);
}
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class CompoundTypeTest method testIsCompound.
// Test TypeUtil.isIntersection(TypeMirror).
public void testIsCompound() throws Exception {
String source = "interface Test<T> extends java.util.Comparator<T> {" + " default Test<T> thenTesting(Test<? super T> other) { " + " return (Test<T> & java.io.Serializable) (c1, c2) -> { " + " int res = compare(c1, c2); " + " return (res != 0) ? res : other.compare(c1, c2); }; }}";
CompilationUnit unit = compileType("Test", source);
AbstractTypeDeclaration decl = unit.getTypes().get(0);
int methodsFound = 0;
for (BodyDeclaration body : decl.getBodyDeclarations()) {
if (body instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) body;
if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
// Verify a normal type isn't marked as compound.
TypeMirror returnType = method.getReturnTypeMirror();
assertFalse(TypeUtil.isIntersection(returnType));
// The method's return type isn't compound, but the cast expression in
// its return statement is.
ReturnStatement stmt = (ReturnStatement) method.getBody().getStatements().get(0);
assertTrue(TypeUtil.isIntersection(stmt.getExpression().getTypeMirror()));
methodsFound++;
}
}
}
assertEquals(1, methodsFound);
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class CompoundTypeTest method testCompoundTypeFullName.
// Test NameTable.getObjCType(TypeMirror).
public void testCompoundTypeFullName() throws IOException {
String source = "package foo.bar; interface Test<T> extends java.util.Comparator<T> {" + " default Test<T> thenTesting(Test<? super T> other) { " + " return (Test<T> & java.io.Serializable) (c1, c2) -> { " + " int res = compare(c1, c2); " + " return (res != 0) ? res : other.compare(c1, c2); }; }}";
CompilationUnit unit = compileType("Test", source);
AbstractTypeDeclaration decl = unit.getTypes().get(0);
for (BodyDeclaration body : decl.getBodyDeclarations()) {
if (body instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) body;
if (ElementUtil.getName(method.getExecutableElement()).equals("thenTesting")) {
// The method's return type isn't compound, but the cast expression in
// its return statement is.
ReturnStatement stmt = (ReturnStatement) method.getBody().getStatements().get(0);
TypeMirror mirror = stmt.getExpression().getTypeMirror();
String typeName = unit.getEnv().nameTable().getObjCType(mirror);
assertEquals("id<FooBarTest, JavaIoSerializable>", typeName);
return;
}
}
}
fail("thenTesting method not found");
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class ElementUtilTest method testGetAnnotation.
public void testGetAnnotation() throws IOException {
CompilationUnit unit = translateType("Example", "@com.google.j2objc.annotations.ObjectiveCName(\"E\") class Example {}");
AbstractTypeDeclaration decl = unit.getTypes().get(0);
TypeElement element = decl.getTypeElement();
AnnotationMirror annotation = ElementUtil.getAnnotation(element, ObjectiveCName.class);
assertEquals("com.google.j2objc.annotations.ObjectiveCName", annotation.getAnnotationType().toString());
}
Aggregations