/**Return variables declared in the given scope. */ public Iterable<? extends Element> getLocalVars(Scope scope, ElementAcceptor acceptor) { ArrayList<Element> members = new ArrayList<Element>(); Elements elements = JavacElements.instance(ctx); Types types = JavacTypes.instance(ctx); while(scope != null && scope.getEnclosingClass() != null) { for (Element local : scope.getLocalElements()) { if (acceptor == null || acceptor.accept(local, null)) { if (!isHidden(local, members, elements, types)) { members.add(local); } } } scope = scope.getEnclosingScope(); } return members; }
public void testDisableAccessRightsCrash() throws Exception { ClassPath boot = ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL[0])); FileObject testFile = FileUtil.createData(FileUtil.createMemoryFileSystem().getRoot(), "Test.java"); try (Writer w = new OutputStreamWriter(testFile.getOutputStream())) { w.append("public class Test {}"); } JavaSource js = JavaSource.create(ClasspathInfo.create(boot, ClassPath.EMPTY, ClassPath.EMPTY), testFile); js.runUserActionTask(new Task<CompilationController>() { @Override public void run(CompilationController parameter) throws Exception { parameter.toPhase(Phase.RESOLVED); TreePath clazzPath = new TreePath(new TreePath(parameter.getCompilationUnit()), parameter.getCompilationUnit().getTypeDecls().get(0)); Scope scope = parameter.getTrees().getScope(clazzPath); Scope disableScope = parameter.getTreeUtilities().toScopeWithDisabledAccessibilityChecks(scope); ExpressionTree et = parameter.getTreeUtilities().parseExpression("1 + 1", new SourcePositions[1]); parameter.getTreeUtilities().attributeTree(et, disableScope); } }, true); }
private Iterable<? extends Element> scopeContent(AnalyzeTask at, Scope scope, Function<Element, Iterable<? extends Element>> elementConvertor) { Iterable<Scope> scopeIterable = () -> new Iterator<Scope>() { private Scope currentScope = scope; @Override public boolean hasNext() { return currentScope != null; } @Override public Scope next() { if (!hasNext()) throw new NoSuchElementException(); try { return currentScope; } finally { currentScope = currentScope.getEnclosingScope(); } } }; @SuppressWarnings("unchecked") List<Element> result = Util.stream(scopeIterable) .flatMap(this::localElements) .flatMap(el -> Util.stream((Iterable<Element>)elementConvertor.apply(el))) .collect(toCollection(ArrayList :: new)); result.addAll(listPackages(at, "")); return result; }
public void testParseAndAttributeMultipleStatements() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "String $2 = $1; int $l = $2.length(); System.err.println($l);", s); assertTrue(result.getKind().name(), result.getKind() == Kind.BLOCK); String golden = "{\n" + " $$1$;\n" + " String $2 = $1;\n" + " int $l = $2.length();\n" + " System.err.println($l);\n" + " $$2$;\n" + "}"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
public void testParseAndAttributeMultipleClassMembers() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.singletonMap("$1", info.getTreeUtilities().parseType("int", info.getTopLevelElements().get(0)))); String code = "private int i; private int getI() { return i; } private void setI(int i) { this.i = i; }"; Tree result = Utilities.parseAndAttribute(info, code, s); String golden = "class $ {\n" + " $$1$;\n" + " private int i;\n" + " private int getI() {\n" + " return i;\n" + " }\n" + " private void setI(int i) {\n" + " this.i = i;\n" + " }\n" + "}"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ").trim()); }
public void testParseAndAttributeMultipleStatements2() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "$type $name = $map.get($key); if ($name == null) { $map.put($key, $name = $init); }", s); assertTrue(result.getKind().name(), result.getKind() == Kind.BLOCK); String golden = "{" + " $$1$;" + " $type $name = $map.get($key);" + " if ($name == null) {" + " $map.put($key, $name = $init);" + " }" + " $$2$;\n" + "}"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
public void testLambdaPattern() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "new $type() {\n $mods$ $resultType $methodName($args$) {\n $statements$;\n }\n }\n", s); assertTrue(result.getKind().name(), result.getKind() == Kind.NEW_CLASS); String golden = "new $type(){ $mods$ $resultType $methodName($args$) { $statements$; } }"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); Collection<Diagnostic<? extends JavaFileObject>> errors = new LinkedList<Diagnostic<? extends JavaFileObject>>(); result = Utilities.parseAndAttribute(info, "new $type() {\n $mods$ $resultType $methodName($args$) {\n $statements$;\n }\n }\n", null, errors); assertTrue(result.getKind().name(), result.getKind() == Kind.NEW_CLASS); golden = "new $type(){ $mods$ $resultType $methodName($args$) { $statements$; } }"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); assertTrue(errors.toString(), errors.isEmpty()); }
public void testParseAndAttributeMultipleStatementsWithBefore() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "$before$; String $2 = $1; int $l = $2.length(); System.err.println($l);", s); assertTrue(result.getKind().name(), result.getKind() == Kind.BLOCK); String golden = "{\n" + " $before$;\n" + " String $2 = $1;\n" + " int $l = $2.length();\n" + " System.err.println($l);\n" + " $$2$;\n" + "}"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
public void testParseAndAttributeMultipleStatementsWithAfter() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "String $2 = $1; int $l = $2.length(); System.err.println($l); $after$;", s); assertTrue(result.getKind().name(), result.getKind() == Kind.BLOCK); String golden = "{\n" + " $$1$;\n" + " String $2 = $1;\n" + " int $l = $2.length();\n" + " System.err.println($l);\n" + " $after$;\n" + "}"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
public void testBrokenPlatform226678() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); JavaSource.create(ClasspathInfo.create(ClassPath.EMPTY, ClassPath.EMPTY, ClassPath.EMPTY), info.getFileObject()).runUserActionTask(new Task<CompilationController>() { @Override public void run(CompilationController parameter) throws Exception { parameter.toPhase(Phase.RESOLVED); info = parameter; } }, true); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); String methodCode = "private int test(int i) { return i; }"; Tree result = Utilities.parseAndAttribute(info, methodCode, s); assertEquals(Kind.METHOD, result.getKind()); assertEquals(methodCode.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ").trim()); }
@Override public void invoke() { Object o = component.getDocument().getProperty(Document.StreamDescriptionProperty); if (o instanceof DataObject) { DataObject d = (DataObject) o; JButton ok = new JButton(LBL_ButtonOK()); FileObject primaryFile = d.getPrimaryFile(); TypeElement element = handle.resolve(javac); TreePath path = javac.getTrees().getPath(element); Scope scope = javac.getTrees().getScope(path); final AddPropertyPanel addPropertyPanel = new AddPropertyPanel(javac, scope, existingFields, ok); DialogDescriptor dd = new DialogDescriptor(addPropertyPanel, CAP_AddProperty(), true, new Object[] {ok, LBL_ButtonCancel()}, ok, DialogDescriptor.DEFAULT_ALIGN, null, null); if (DialogDisplayer.getDefault().notify(dd) == ok) { perform(primaryFile, component, addPropertyPanel.getAddPropertyConfig(), scope); } } }
private static BlockTree createDefaultMethodBody(WorkingCopy wc, TreePath targetTree, TypeMirror returnType, String name) { TreeUtilities tu = wc.getTreeUtilities(); TypeElement targetClazz = (TypeElement)wc.getTrees().getElement(targetTree); StatementTree st = tu.parseStatement("{class ${abstract " + (returnType != null ? returnType.toString() : "void") + " " + ("<init>".equals(name) ? targetClazz.getSimpleName() : name) + "();}}", new SourcePositions[1]); //NOI18N Trees trees = wc.getTrees(); List<? extends Tree> members = ((ClassTree) targetTree.getLeaf()).getMembers(); Scope scope = members.isEmpty() ? trees.getScope(targetTree) : trees.getScope(new TreePath(targetTree, members.get(0))); tu.attributeTree(st, scope); Tree first = null; for(Tree t : ((ClassTree)((BlockTree)st).getStatements().get(0)).getMembers()) { if (t.getKind() == Tree.Kind.METHOD && !"<init>".contentEquals(((MethodTree)t).getName())) { //NOI19N first = t; break; } } ExecutableElement ee = (ExecutableElement) wc.getTrees().getElement(new TreePath(targetTree, first)); return GeneratorUtilities.get(wc).createAbstractMethodImplementation(targetClazz, ee).getBody(); }
private void addInnerClasses(TypeElement te, EnumSet<ElementKind> kinds, DeclaredType baseType, Set<? extends Element> toExclude, String prefix, int substitutionOffset, JavadocContext jdctx) { CompilationInfo controller = jdctx.javac; Element srcEl = jdctx.handle.resolve(controller); Elements elements = controller.getElements(); Types types = controller.getTypes(); Trees trees = controller.getTrees(); TreeUtilities tu = controller.getTreeUtilities(); TreePath docpath = srcEl != null ? trees.getPath(srcEl) : null; Scope scope = docpath != null ? trees.getScope(docpath) : tu.scopeFor(caretOffset); for (Element e : controller.getElementUtilities().getMembers(te.asType(), null)) { if ((e.getKind().isClass() || e.getKind().isInterface()) && (toExclude == null || !toExclude.contains(e))) { String name = e.getSimpleName().toString(); if (Utilities.startsWith(name, prefix) && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) && trees.isAccessible(scope, (TypeElement)e) && isOfKindAndType(e.asType(), e, kinds, baseType, scope, trees, types)) { items.add(JavadocCompletionItem.createTypeItem(jdctx.javac, (TypeElement) e, substitutionOffset, null, elements.isDeprecated(e)/*, isOfSmartType(env, e.asType(), smartTypes)*/)); } } } }
private void addPackageContent(PackageElement pe, EnumSet<ElementKind> kinds, DeclaredType baseType, Set<? extends Element> toExclude, String prefix, int substitutionOffset, JavadocContext jdctx) { CompilationInfo controller = jdctx.javac; Element srcEl = jdctx.handle.resolve(controller); Elements elements = controller.getElements(); Types types = controller.getTypes(); Trees trees = controller.getTrees(); TreeUtilities tu = controller.getTreeUtilities(); ElementUtilities eu = controller.getElementUtilities(); TreePath docpath = srcEl != null ? trees.getPath(srcEl) : null; Scope scope = docpath != null ? trees.getScope(docpath) : tu.scopeFor(caretOffset); for(Element e : pe.getEnclosedElements()) { if ((e.getKind().isClass() || e.getKind().isInterface()) && (toExclude == null || !toExclude.contains(e))) { String name = e.getSimpleName().toString(); if (Utilities.startsWith(name, prefix) && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(e)) && trees.isAccessible(scope, (TypeElement)e) && isOfKindAndType(e.asType(), e, kinds, baseType, scope, trees, types) && !Utilities.isExcluded(eu.getElementName(e, true))) { items.add(JavadocCompletionItem.createTypeItem(jdctx.javac, (TypeElement) e, substitutionOffset, null, elements.isDeprecated(e)/*, isOfSmartType(env, e.asType(), smartTypes)*/)); } } } }
private boolean isOfKindAndType(TypeMirror type, Element e, EnumSet<ElementKind> kinds, TypeMirror base, Scope scope, Trees trees, Types types) { if (type.getKind() != TypeKind.ERROR && kinds.contains(e.getKind())) { if (base == null) return true; if (types.isSubtype(type, base)) return true; } if ((e.getKind().isClass() || e.getKind().isInterface()) && (kinds.contains(ANNOTATION_TYPE) || kinds.contains(CLASS) || kinds.contains(ENUM) || kinds.contains(INTERFACE))) { DeclaredType dt = (DeclaredType)e.asType(); for (Element ee : e.getEnclosedElements()) if (trees.isAccessible(scope, ee, dt) && isOfKindAndType(ee.asType(), ee, kinds, base, scope, trees, types)) return true; } return false; }
private static MethodTree createHashCodeMethod(WorkingCopy wc, Iterable<? extends VariableElement> hashCodeFields, Scope scope) { TreeMaker make = wc.getTreeMaker(); Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC); int startNumber = generatePrimeNumber(2, 10); int multiplyNumber = generatePrimeNumber(10, 100); List<StatementTree> statements = new ArrayList<>(); //int hash = <startNumber>; statements.add(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "hash", make.PrimitiveType(TypeKind.INT), make.Literal(startNumber))); //NOI18N for (VariableElement ve : hashCodeFields) { TypeMirror tm = ve.asType(); ExpressionTree variableRead = prepareExpression(wc, HASH_CODE_PATTERNS, tm, ve, scope); statements.add(make.ExpressionStatement(make.Assignment(make.Identifier("hash"), make.Binary(Tree.Kind.PLUS, make.Binary(Tree.Kind.MULTIPLY, make.Literal(multiplyNumber), make.Identifier("hash")), variableRead)))); //NOI18N } statements.add(make.Return(make.Identifier("hash"))); //NOI18N BlockTree body = make.Block(statements, false); ModifiersTree modifiers = prepareModifiers(wc, mods,make); return make.Method(modifiers, "hashCode", make.PrimitiveType(TypeKind.INT), Collections.<TypeParameterTree> emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), body, null); //NOI18N }
public static MethodArgument[] computeMethodArguments(CompilationController ci, EditorContext.Operation operation, ASTOperationCreationDelegate opCreationDelegate) throws IOException { EditorContext.MethodArgument args[]; if (!PreferredCCParser.toPhase(ci, JavaSource.Phase.RESOLVED, LOG)) { return null; } int offset = operation.getMethodEndPosition().getOffset(); Scope scope = ci.getTreeUtilities().scopeFor(offset); Element method = scope.getEnclosingMethod(); if (method == null) { return null; } Tree methodTree = ci.getTrees().getTree(method); CompilationUnitTree cu = ci.getCompilationUnit(); MethodArgumentsScanner scanner = new MethodArgumentsScanner(offset, cu, ci.getTrees().getSourcePositions(), true, opCreationDelegate); args = methodTree.accept(scanner, null); args = scanner.getArguments(); return args; }
public static MethodArgument[] computeMethodArguments(CompilationController ci, int methodLineNumber, int offset, ASTOperationCreationDelegate opCreationDelegate) throws IOException { MethodArgument args[]; if (!PreferredCCParser.toPhase(ci, JavaSource.Phase.RESOLVED, LOG)) { return null; } Scope scope = ci.getTreeUtilities().scopeFor(offset); Element clazz = scope.getEnclosingClass(); if (clazz == null) { return null; } Tree methodTree = ci.getTrees().getTree(clazz); CompilationUnitTree cu = ci.getCompilationUnit(); MethodArgumentsScanner scanner = new MethodArgumentsScanner(methodLineNumber, cu, ci.getTrees().getSourcePositions(), false, opCreationDelegate); args = methodTree.accept(scanner, null); args = scanner.getArguments(); return args; }
public static Scope constructScope(CompilationInfo info, String... importedClasses) { Collection<String> imports = new LinkedList<String>(); for (String i : importedClasses) { imports.add("import " + i + ";\n"); } return Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap(), imports); }
private static int validateOffset(FileObject editorDoc, final int toValidate) { final int[] validated = new int[]{-1}; JavaSource js = JavaSource.forFileObject(editorDoc); if (js != null) { try { js.runUserActionTask(new Task<CompilationController>() { public void run(CompilationController controller) throws Exception { controller.toPhase(JavaSource.Phase.RESOLVED); validated[0] = -1; // non-validated default Scope sc = controller.getTreeUtilities().scopeFor(toValidate); if (sc.getEnclosingClass() != null) { validated[0] = toValidate; } } }, true); } catch (IOException ex) { Exceptions.printStackTrace(ex); } } return validated[0]; }
@Override public boolean isOffsetValid(FileObject fo, final int offset) { final Boolean[] validated = new Boolean[1]; JavaSource js = JavaSource.forFileObject(fo); if (js != null) { try { js.runUserActionTask(new CancellableTask<CompilationController>() { @Override public void cancel() { } public void run(CompilationController controller) throws Exception { controller.toPhase(JavaSource.Phase.RESOLVED); validated[0] = false; // non-validated default Scope sc = controller.getTreeUtilities().scopeFor(offset); if (sc.getEnclosingClass() != null) { validated[0] = true; } } }, true); } catch (IOException ex) { ProfilerLogger.log(ex); } } return validated[0]; }
public static Scope constructScope(CompilationInfo info, String... importedClasses) { StringBuilder clazz = new StringBuilder(); clazz.append("package $$;\n"); for (String i : importedClasses) { clazz.append("import ").append(i).append(";\n"); } clazz.append("public class $").append(inc++).append("{"); clazz.append("private void test() {\n"); clazz.append("}\n"); clazz.append("}\n"); JavacTaskImpl jti = JavaSourceAccessor.getINSTANCE().getJavacTask(info); Context context = jti.getContext(); JavaCompiler jc = JavaCompiler.instance(context); Log.instance(context).nerrors = 0; JavaFileObject jfo = FileObjects.memoryFileObject("$$", "$", new File("/tmp/t.java").toURI(), System.currentTimeMillis(), clazz.toString()); try { CompilationUnitTree cut = ParserFactory.instance(context).newParser(jfo.getCharContent(true), true, true, true).parseCompilationUnit(); jti.analyze(jti.enter(Collections.singletonList(cut))); return new ScannerImpl().scan(cut, info); } catch (IOException ex) { Exceptions.printStackTrace(ex); return null; } finally { } }
@Override public Scope visitMethod(MethodTree node, CompilationInfo p) { if (node.getReturnType() == null) { return null; } return super.visitMethod(node, p); }
public void testParseAndAttributeExpressionStatement() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.singletonMap("$1", info.getTreeUtilities().parseType("int", info.getTopLevelElements().get(0)))); Tree result = Utilities.parseAndAttribute(info, "$1 = 1;", s); assertTrue(result.getKind().name(), result.getKind() == Kind.EXPRESSION_STATEMENT); }
public void testParseAndAttributeVariable() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.singletonMap("$1", info.getTreeUtilities().parseType("int", info.getTopLevelElements().get(0)))); Tree result = Utilities.parseAndAttribute(info, "int $2 = $1;", s); assertTrue(result.getKind().name(), result.getKind() == Kind.VARIABLE); }
public void testParseAndAttributeMethod() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.singletonMap("$1", info.getTreeUtilities().parseType("int", info.getTopLevelElements().get(0)))); String methodCode = "private int test(int i) { return i; }"; Tree result = Utilities.parseAndAttribute(info, methodCode, s); assertEquals(Kind.METHOD, result.getKind()); assertEquals(methodCode.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ").trim()); }
public void testParseAndAttributeFieldModifiersVariable() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); String code = "$mods$ java.lang.String $name;"; Tree result = Utilities.parseAndAttribute(info, code, s); String golden = "$mods$ java.lang.String $name"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ").trim()); }
public void testParseAndAttributeMethodModifiersVariable() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); String code = "$mods$ $type $name() { $r$; }"; Tree result = Utilities.parseAndAttribute(info, code, s); String golden = "$mods$ $type $name() { $r$; }"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ").trim()); }
public void testSimpleExpression() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "$1.isDirectory()", s); assertTrue(result.getKind().name(), result.getKind() == Kind.METHOD_INVOCATION); String golden = "$1.isDirectory()"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
public void testARMResourceVariable2() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "try ($t$; $type $name = $init) { $stmts$; } catch $catches$", s); assertTrue(result.getKind().name(), result.getKind() == Kind.TRY); String golden = "try ($t$; final $type $name = $init) { $stmts$; }$catches$"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
public void testARMResourceNotVariable() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "try ($t $n = $init$) { $stmts$; } catch $catches$", s); assertTrue(result.getKind().name(), result.getKind() == Kind.TRY); String golden = "try (final $t $n = $init$) { $stmts$; }$catches$"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
public void testParseAndAttributeType() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "\njava. util \n.List \n", s); assertTrue(result.getKind().name(), result.getKind() == Kind.MEMBER_SELECT); assertEquals(ElementKind.INTERFACE, info.getTrees().getElement(new TreePath(new TreePath(info.getCompilationUnit()), result)).getKind()); assertEquals(info.getElements().getTypeElement("java.util.List"), info.getTrees().getElement(new TreePath(new TreePath(info.getCompilationUnit()), result))); }
/** @see com.sun.tools.javadoc.ClassDocImpl#findConstructor */ MethodSymbol findConstructor(ClassSymbol tsym, List<Type> paramTypes) { for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(names.init); e.scope != null; e = e.next()) { if (e.sym.kind == Kinds.MTH) { if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) { return (MethodSymbol) e.sym; } } } return null; }
public void testOrdinaryCatch() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "try {\n }\n catch (NullPointerException ex) { } finally {\n }\n", s); assertTrue(result.getKind().name(), result.getKind() == Kind.TRY); String golden = "try {\n } catch (NullPointerException ex) { } finally {\n }"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
public void testClassPattern() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "$mods$ class $name extends java.util.LinkedList { $methods$; }\n", s); assertTrue(result.getKind().name(), result.getKind() == Kind.CLASS); String golden = " $mods$ class $name extends java.util.LinkedList { $name() { super(); } $methods$ }"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
@RandomlyFails public void testErrorsForPatterns2() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); SourcePositions[] positions = new SourcePositions[1]; Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Collection<Diagnostic<? extends JavaFileObject>> errors = new LinkedList<Diagnostic<? extends JavaFileObject>>(); String code = "$1.isDirectory()"; Tree result = Utilities.parseAndAttribute(info, code, s, positions, errors); assertDiagnostics(errors, "0-0:compiler.err.cant.resolve.location"); assertPositions(result, positions[0], code, "$1", "$1.isDirectory", "$1.isDirectory()"); }
public void DtestMultiStatementVarWithModifiers() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "$mods$ $type $name; $name = $init;", s); assertTrue(result.getKind().name(), result.getKind() == Kind.BLOCK); String golden = "{ $$1$; $mods$$type $name; $name = $init; $$2$; }"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }
public void testAttributionErrors233526() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); SourcePositions[] positions = new SourcePositions[1]; String code = "{\n $4\n};;"; Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Collection<Diagnostic<? extends JavaFileObject>> errors = new LinkedList<>(); Tree result = Utilities.parseAndAttribute(info, code, s, positions, errors); assertDiagnostics(errors, "7-7:compiler.err.expected", "5-7:compiler.err.cant.resolve"); }
public void testAnnotation() throws Exception { prepareTest("test/Test.java", "package test; public class Test{}"); Scope s = Utilities.constructScope(info, Collections.<String, TypeMirror>emptyMap()); Tree result = Utilities.parseAndAttribute(info, "@$annotation($args$)", s); assertTrue(result.getKind().name(), result.getKind() == Kind.ANNOTATION); String golden = "@$annotation(value = $args$)"; assertEquals(golden.replaceAll("[ \n\r]+", " "), result.toString().replaceAll("[ \n\r]+", " ")); }