This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 227285
Collapse All | Expand All

(-)a/java.hints.declarative/src/org/netbeans/modules/java/hints/declarative/conditionapi/Context.java (+27 lines)
Lines 40-45 Link Here
40
package org.netbeans.modules.java.hints.declarative.conditionapi;
40
package org.netbeans.modules.java.hints.declarative.conditionapi;
41
41
42
import com.sun.source.tree.CompilationUnitTree;
42
import com.sun.source.tree.CompilationUnitTree;
43
import com.sun.source.tree.Scope;
43
import com.sun.source.tree.Tree.Kind;
44
import com.sun.source.tree.Tree.Kind;
44
import com.sun.source.util.TreePath;
45
import com.sun.source.util.TreePath;
45
import java.util.ArrayList;
46
import java.util.ArrayList;
Lines 57-62 Link Here
57
import javax.lang.model.element.ElementKind;
58
import javax.lang.model.element.ElementKind;
58
import javax.lang.model.element.Modifier;
59
import javax.lang.model.element.Modifier;
59
import javax.lang.model.element.TypeElement;
60
import javax.lang.model.element.TypeElement;
61
import javax.lang.model.type.DeclaredType;
60
import javax.lang.model.type.TypeKind;
62
import javax.lang.model.type.TypeKind;
61
import javax.lang.model.type.TypeMirror;
63
import javax.lang.model.type.TypeMirror;
62
import org.netbeans.api.annotations.common.CheckForNull;
64
import org.netbeans.api.annotations.common.CheckForNull;
Lines 292-297 Link Here
292
294
293
        return cut.getPackageName() != null ? cut.getPackageName().toString() : "";
295
        return cut.getPackageName() != null ? cut.getPackageName().toString() : "";
294
    }
296
    }
297
    
298
    /**Checks whether the given Java element is available in the particular source
299
     * code or not.
300
     * 
301
     * The <code>elementDescription</code> format is as follows:
302
     * <dl>
303
     *   <dt>for type (class, enum, interface or annotation type)</dt>
304
     *     <dd><em>the FQN of the type</em></dd>
305
     *   <dt>for field or enum constant</dt>
306
     *     <dd><em>the FQN of the enclosing type</em><code>.</code><em>field name</em></dd>
307
     *   <dt>for method</dt>
308
     *     <dd><em>the FQN of the enclosing type</em><code>.</code><em>method name</em><code>(</code><em>comma separated parameter types</em><code>)</code><br>
309
     *         The parameter types may include type parameters, but these are ignored. The last parameter type can use ellipsis (...) to denote vararg method.</dd>
310
     *   <dt>for constructor</dt>
311
     *     <dd><em>the FQN of the enclosing type</em><code>.</code><em>simple name of enclosing type</em><code>(</code><em>comma separated parameter types</em><code>)</code><br>
312
     *         See method format for more details on parameter types.</dd>
313
     * </dl>
314
     * 
315
     * @param elementDescription the description of the element that should be checked for existence
316
     * @return true if and only the specified element exists while processing the current source
317
     * @since nb74
318
     */
319
    public boolean isAvailable(@NonNull String description) {
320
        return ctx.getInfo().getElementUtilities().findElement(description) != null;
321
    }
295
322
296
    static final class APIAccessorImpl extends APIAccessor {
323
    static final class APIAccessorImpl extends APIAccessor {
297
324
(-)a/java.hints.declarative/src/org/netbeans/modules/java/hints/declarative/conditionapi/DefaultRuleUtilities.java (+25 lines)
Lines 162-165 Link Here
162
        Pattern p = Pattern.compile(regexp.toString());
162
        Pattern p = Pattern.compile(regexp.toString());
163
        return p;
163
        return p;
164
    }
164
    }
165
166
    /**Checks whether the given Java element is available in the particular source
167
     * code or not.
168
     * 
169
     * The <code>elementDescription</code> format is as follows:
170
     * <dl>
171
     *   <dt>for type (class, enum, interface or annotation type)</dt>
172
     *     <dd><em>the FQN of the type</em></dd>
173
     *   <dt>for field or enum constant</dt>
174
     *     <dd><em>the FQN of the enclosing type</em><code>.</code><em>field name</em></dd>
175
     *   <dt>for method</dt>
176
     *     <dd><em>the FQN of the enclosing type</em><code>.</code><em>method name</em><code>(</code><em>comma separated parameter types</em><code>)</code><br>
177
     *         The parameter types may include type parameters, but these are ignored. The last parameter type can use ellipsis (...) to denote vararg method.</dd>
178
     *   <dt>for constructor</dt>
179
     *     <dd><em>the FQN of the enclosing type</em><code>.</code><em>simple name of enclosing type</em><code>(</code><em>comma separated parameter types</em><code>)</code><br>
180
     *         See method format for more details on parameter types.</dd>
181
     * </dl>
182
     * 
183
     * @param elementDescription the description of the element that should be checked for existence
184
     * @return true if and only the specified element exists while processing the current source
185
     * @since nb74
186
     */
187
    public boolean isAvailable(@NonNull String elementDescription) {
188
        return context.isAvailable(elementDescription);
189
    }
165
}
190
}
(-)a/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/conditionapi/matchWithBind.hint (-3 / +6 lines)
Lines 1-4 Link Here
1
   return $expr;
1
1 + 1 :: isAvailable("java.lang.ThreadLocal.initialValue()")
2
=> return $o != $t; :: matchesWithBind($expr, "$o == $t")
2
=> 2
3
=> return $t == $o; :: matchesWithBind($expr, "$t != $o")
4
;;
3
;;
4
    
5
2 + 2 :: isAvailable("unknown.Class")
6
=> 2
7
;;
(-)a/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/conditionapi/matchWithBind.test (-17 / +10 lines)
Lines 1-28 Link Here
1
%%TestCase bind-works-1
1
%%TestCase is-available-1
2
package test;
2
package test;
3
public class Test {
3
public class Test extends ThreadLocal {
4
    private boolean t(int v1, int v2) {
4
    private int t() {
5
        return v1 == v2;
5
        return 1 + 1;
6
    }
6
    }
7
}
7
}
8
%%=>
8
%%=>
9
package test;
9
package test;
10
public class Test {
10
public class Test extends ThreadLocal {
11
    private boolean t(int v1, int v2) {
11
    private int t() {
12
        return v1 != v2;
12
        return 2;
13
    }
13
    }
14
}
14
}
15
%%TestCase scope-works-1
15
%%TestCase is-not-available-1
16
package test;
16
package test;
17
public class Test {
17
public class Test {
18
    private boolean t(int v1, int v2) {
18
    private int t() {
19
        return v1 != v2;
19
        return 2 + 2;
20
    }
20
    }
21
}
21
}
22
%%=>
23
package test;
24
public class Test {
25
    private boolean t(int v1, int v2) {
26
        return v1 == v2;
27
    }
28
}
(-)a/java.hints/src/org/netbeans/modules/java/hints/bugs/CollectionRemove.java (-48 / +1 lines)
Lines 264-318 Link Here
264
        return info.getTypeUtilities().isCastable(type1, type2);
264
        return info.getTypeUtilities().isCastable(type1, type2);
265
    }
265
    }
266
266
267
    private static final Pattern SPLIT = Pattern.compile("(.+)\\.([^.]+)\\((.*)\\)");
268
    
269
    private static ExecutableElement resolveMethod(CompilationInfo info, String name) {
267
    private static ExecutableElement resolveMethod(CompilationInfo info, String name) {
270
        Matcher m = SPLIT.matcher(name);
268
        return (ExecutableElement) info.getElementUtilities().findElement(name);
271
272
        if (!m.matches()) {
273
            throw new IllegalArgumentException();
274
        }
275
276
        String className = m.group(1);
277
        String methodName = m.group(2);
278
        String paramsSpec = m.group(3);
279
280
        TypeElement te = info.getElements().getTypeElement(className);
281
282
        if (te == null) {
283
            return null;
284
        }
285
286
        String[] paramList = paramsSpec.split(",");
287
        List<TypeMirror> params = new LinkedList<TypeMirror>();
288
289
        TypeElement topLevel = info.getTopLevelElements().get(0);
290
291
        for (String t : paramList) {
292
            params.add(info.getTreeUtilities().parseType(t, topLevel));
293
        }
294
295
        for (ExecutableElement ee : ElementFilter.methodsIn(te.getEnclosedElements())) {
296
            if (!methodName.equals(ee.getSimpleName().toString()) || ee.getParameters().size() != params.size()) {
297
                continue;
298
            }
299
            
300
            Iterator<TypeMirror> designed = params.iterator();
301
            boolean found = true;
302
303
            for (VariableElement param : ee.getParameters()) {
304
                if (!info.getTypes().isSameType(info.getTypes().erasure(param.asType()), designed.next())) {
305
                    found = false;
306
                    break;
307
                }
308
            }
309
310
            if (found) {
311
                return ee;
312
            }
313
        }
314
        
315
        return null;
316
    }
269
    }
317
270
318
    private static ExecutableElement findEnclosingMethod(CompilationInfo info, TreePath path) {
271
    private static ExecutableElement findEnclosingMethod(CompilationInfo info, TreePath path) {
(-)a/java.source/apichanges.xml (+13 lines)
Lines 108-113 Link Here
108
    <!-- ACTUAL CHANGES BEGIN HERE: -->
108
    <!-- ACTUAL CHANGES BEGIN HERE: -->
109
109
110
    <changes>
110
    <changes>
111
         <change id="ElementUtilities-findElement">
112
             <api name="general"/>
113
             <summary>Added ElementUtilities.findElement</summary>
114
             <version major="0" minor="114"/>
115
             <date day="18" month="3" year="2013"/>
116
             <author login="jlahoda"/>
117
            <compatibility addition="yes" binary="compatible" deletion="no" deprecation="no" modification="no" semantic="compatible" source="compatible"/>
118
             <description>
119
                 Added a new method to find an element based on its description:
120
                 <code>ElementUtilities.findElement</code>.
121
             </description>
122
             <issue number="227285"/>
123
        </change>
111
         <change id="LambdaEnhancements">
124
         <change id="LambdaEnhancements">
112
             <api name="general"/>
125
             <api name="general"/>
113
             <summary>Added several methods to support Java 8 features.</summary>
126
             <summary>Added several methods to support Java 8 features.</summary>
(-)a/java.source/nbproject/project.properties (-1 / +1 lines)
Lines 46-52 Link Here
46
javadoc.title=Java Source
46
javadoc.title=Java Source
47
javadoc.arch=${basedir}/arch.xml
47
javadoc.arch=${basedir}/arch.xml
48
javadoc.apichanges=${basedir}/apichanges.xml
48
javadoc.apichanges=${basedir}/apichanges.xml
49
spec.version.base=0.113
49
spec.version.base=0.114
50
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar
50
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar
51
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
51
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
52
    ${o.n.core.dir}/lib/boot.jar:\
52
    ${o.n.core.dir}/lib/boot.jar:\
(-)a/java.source/src/org/netbeans/api/java/source/ElementUtilities.java (-1 / +105 lines)
Lines 46-51 Link Here
46
import com.sun.javadoc.Doc;
46
import com.sun.javadoc.Doc;
47
import com.sun.source.tree.CompilationUnitTree;
47
import com.sun.source.tree.CompilationUnitTree;
48
import com.sun.source.tree.Scope;
48
import com.sun.source.tree.Scope;
49
import com.sun.source.tree.Tree;
49
import com.sun.source.util.TreePath;
50
import com.sun.source.util.TreePath;
50
import com.sun.source.util.Trees;
51
import com.sun.source.util.Trees;
51
import com.sun.tools.javac.api.JavacScope;
52
import com.sun.tools.javac.api.JavacScope;
Lines 94-104 Link Here
94
import javax.lang.model.type.TypeKind;
95
import javax.lang.model.type.TypeKind;
95
import javax.lang.model.type.TypeMirror;
96
import javax.lang.model.type.TypeMirror;
96
import javax.lang.model.type.TypeVariable;
97
import javax.lang.model.type.TypeVariable;
98
import javax.lang.model.util.ElementFilter;
97
import javax.lang.model.util.Elements;
99
import javax.lang.model.util.Elements;
98
import javax.lang.model.util.Types;
100
import javax.lang.model.util.Types;
101
import org.netbeans.api.annotations.common.CheckForNull;
99
102
100
import org.netbeans.api.annotations.common.NonNull;
103
import org.netbeans.api.annotations.common.NonNull;
101
import org.netbeans.api.annotations.common.NullAllowed;
104
import org.netbeans.api.annotations.common.NullAllowed;
105
import org.netbeans.modules.java.source.ElementHandleAccessor;
102
import org.netbeans.modules.java.source.builder.ElementsService;
106
import org.netbeans.modules.java.source.builder.ElementsService;
103
import org.netbeans.modules.java.source.JavadocEnv;
107
import org.netbeans.modules.java.source.JavadocEnv;
104
108
Lines 547-553 Link Here
547
     */
551
     */
548
    public boolean isEffectivelyFinal(VariableElement e) {
552
    public boolean isEffectivelyFinal(VariableElement e) {
549
        return (((Symbol) e).flags() & (Flags.EFFECTIVELY_FINAL | Flags.FINAL)) != 0;
553
        return (((Symbol) e).flags() & (Flags.EFFECTIVELY_FINAL | Flags.FINAL)) != 0;
550
    };
554
    }
555
    
556
    /**Looks up the given Java element.
557
     * 
558
     * The <code>elementDescription</code> format is as follows:
559
     * <dl>
560
     *   <dt>for type (class, enum, interface or annotation type)</dt>
561
     *     <dd><em>the FQN of the type</em></dd>
562
     *   <dt>for field or enum constant</dt>
563
     *     <dd><em>the FQN of the enclosing type</em><code>.</code><em>field name</em></dd>
564
     *   <dt>for method</dt>
565
     *     <dd><em>the FQN of the enclosing type</em><code>.</code><em>method name</em><code>(</code><em>comma separated parameter types</em><code>)</code><br>
566
     *         The parameter types may include type parameters, but these are ignored. The last parameter type can use ellipsis (...) to denote vararg method.</dd>
567
     *   <dt>for constructor</dt>
568
     *     <dd><em>the FQN of the enclosing type</em><code>.</code><em>simple name of enclosing type</em><code>(</code><em>comma separated parameter types</em><code>)</code><br>
569
     *         See method format for more details on parameter types.</dd>
570
     * </dl>
571
     * 
572
     * @param elementDescription the description of the element that should be checked for existence
573
     * @return the found element, or null if not available
574
     * @since 0.114
575
     */
576
    public @CheckForNull Element findElement(@NonNull String description) {
577
        if (description.contains("(")) {
578
            //method:
579
            String methodFullName = description.substring(0, description.indexOf('('));
580
            String className = methodFullName.substring(0, methodFullName.lastIndexOf('.'));
581
            TypeElement clazz = info.getElements().getTypeElement(className);
582
            
583
            if (clazz == null) return null;
584
            
585
            String methodSimpleName = methodFullName.substring(methodFullName.lastIndexOf('.') + 1);
586
            boolean constructor = clazz.getSimpleName().contentEquals(methodSimpleName);
587
            String parameters = description.substring(description.indexOf('(') + 1, description.lastIndexOf(')') + 1);
588
            
589
            int paramIndex = 0;
590
            int lastParamStart = 0;
591
            int angleDepth = 0;
592
            //XXX:
593
            List<TypeMirror> types = new ArrayList<TypeMirror>();
594
            
595
            while (paramIndex < parameters.length()) {
596
                switch (parameters.charAt(paramIndex)) {
597
                    case '<': angleDepth++; break;
598
                    case '>': angleDepth--; break; //TODO: check underflow
599
                    case ',':
600
                        if (angleDepth > 0) break;
601
                    case ')':
602
                        if (paramIndex > lastParamStart) {
603
                            String type = parameters.substring(lastParamStart, paramIndex).replace("...", "[]");
604
                            //TODO: handle varargs
605
                            types.add(info.getTypes().erasure(info.getTreeUtilities().parseType(type, info.getTopLevelElements().get(0)/*XXX*/)));
606
                            lastParamStart = paramIndex + 1;
607
                        }
608
                        break;
609
                }
610
                
611
                paramIndex++;
612
            }
613
            
614
            OUTER: for (ExecutableElement ee : constructor ? ElementFilter.constructorsIn(clazz.getEnclosedElements()) : ElementFilter.methodsIn(clazz.getEnclosedElements())) {
615
                if ((constructor || ee.getSimpleName().contentEquals(methodSimpleName)) && ee.getParameters().size() == types.size()) {
616
                    Iterator<? extends TypeMirror> real = ((ExecutableType) info.getTypes().erasure(ee.asType())).getParameterTypes().iterator();
617
                    Iterator<TypeMirror> expected = types.iterator();
618
                    
619
                    while (real.hasNext() && expected.hasNext()) {
620
                        if (!info.getTypes().isSameType(real.next(), expected.next())) {
621
                            continue OUTER;
622
                        }
623
                    }
624
                    
625
                    assert real.hasNext() == expected.hasNext();
626
                    
627
                    return ee;
628
                }
629
            }
630
        }
631
        
632
        //field or class:
633
        TypeElement el = info.getElements().getTypeElement(description);
634
        
635
        if (el != null) return el;
636
        
637
        int dot = description.lastIndexOf('.');
638
        
639
        if (dot != (-1)) {
640
            String simpleName = description.substring(dot + 1);
641
            
642
            el = info.getElements().getTypeElement(description.substring(0, dot));
643
            
644
            if (el != null) {
645
                for (VariableElement var : ElementFilter.fieldsIn(el.getEnclosedElements())) {
646
                    if (var.getSimpleName().contentEquals(simpleName)) {
647
                        return var;
648
                    }
649
                }
650
            }
651
        }
652
        
653
        return null;
654
    }
551
    
655
    
552
    // private implementation --------------------------------------------------
656
    // private implementation --------------------------------------------------
553
657
(-)a/java.source/test/unit/src/org/netbeans/api/java/source/ElementsTest.java (-72 / +77 lines)
Lines 1-7 Link Here
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
3
 *
4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
4
 * Copyright 1997-2013 Oracle and/or its affiliates. All rights reserved.
5
 *
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
7
 * Other names may be trademarks of their respective owners.
Lines 26-48 Link Here
26
 * 
26
 * 
27
 * Contributor(s):
27
 * Contributor(s):
28
 * 
28
 * 
29
 * Portions Copyrighted 2007 Sun Microsystems, Inc.
29
 * Portions Copyrighted 2007-2013 Sun Microsystems, Inc.
30
 */
30
 */
31
31
32
package org.netbeans.api.java.source;
32
package org.netbeans.api.java.source;
33
import com.sun.source.tree.AnnotationTree;
34
import com.sun.source.tree.MethodTree;
35
import com.sun.source.util.TreePath;
36
import java.io.File;
33
import java.io.File;
37
import java.io.IOException;
34
import java.io.IOException;
38
import java.nio.charset.Charset;
39
import java.util.List;
40
import javax.lang.model.element.Element;
35
import javax.lang.model.element.Element;
41
import javax.lang.model.element.TypeElement;
36
import javax.lang.model.element.ElementKind;
42
import javax.lang.model.type.DeclaredType;
37
import javax.lang.model.element.ExecutableElement;
43
import javax.lang.model.type.TypeMirror;
38
import javax.lang.model.type.TypeKind;
44
import org.netbeans.junit.NbTestCase;
39
import org.netbeans.junit.NbTestCase;
45
import org.netbeans.spi.queries.FileEncodingQueryImplementation;
46
import org.openide.filesystems.FileObject;
40
import org.openide.filesystems.FileObject;
47
import org.openide.filesystems.FileUtil;
41
import org.openide.filesystems.FileUtil;
48
42
Lines 50-72 Link Here
50
 *
44
 *
51
 * @author Jan Lahoda
45
 * @author Jan Lahoda
52
 */
46
 */
53
public class ElementsTest extends NbTestCase {
47
public class ElementUtilitiesTest extends NbTestCase {
54
48
55
    public ElementsTest(String name) {
49
    public ElementUtilitiesTest(String name) {
56
        super(name);
50
        super(name);
57
    }
51
    }
58
52
59
    protected void setUp() throws Exception {
53
    protected void setUp() throws Exception {
60
        clearWorkDir();
54
        clearWorkDir();
61
        SourceUtilsTestUtil.prepareTest(new String[0], new Object[] {new FileEncodingQueryImplementation() {
55
        SourceUtilsTestUtil.prepareTest(new String[0], new Object[0]);
62
            @Override
63
            public Charset getEncoding(FileObject file) {
64
                if (file.equals(testFO))
65
                    return Charset.forName("UTF-8");
66
                else
67
                    return null;
68
            }
69
        }});
70
    }
56
    }
71
    
57
    
72
    private FileObject sourceRoot;
58
    private FileObject sourceRoot;
Lines 91-153 Link Here
91
        prepareTest();
77
        prepareTest();
92
        
78
        
93
        TestUtilities.copyStringToFile(FileUtil.toFile(testFO),
79
        TestUtilities.copyStringToFile(FileUtil.toFile(testFO),
94
                "public class Vecernicek {" +
80
                "public class Test {" +
95
                "}");
81
                "}");
96
        JavaSource javaSource = JavaSource.forFileObject(testFO);
82
        JavaSource javaSource = JavaSource.forFileObject(testFO);
97
        javaSource.runUserActionTask(new Task<CompilationController>() {
83
        javaSource.runUserActionTask(new Task<CompilationController>() {
98
            public void run(CompilationController controller) throws IOException {
84
            public void run(CompilationController controller) throws IOException {
99
                controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
85
                controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
100
                TypeElement typeElement = controller.getElements().getTypeElement("Vecernicek");
86
                
101
                assertNotNull(typeElement);
87
                {
102
            }
88
                    Element wait = controller.getElementUtilities().findElement("java.lang.Object.wait(long)");
103
        }, true);
89
                    assertNotNull(wait);
104
        
90
                    assertEquals(ElementKind.METHOD, wait.getKind());
105
        TestUtilities.copyStringToFile(FileUtil.toFile(testFO),
91
                    ExecutableElement waitMethod = (ExecutableElement) wait;
106
                "public class Večerníček {" +
92
                    assertEquals("wait", waitMethod.getSimpleName().toString());
107
                "}");
93
                    assertEquals(1, waitMethod.getParameters().size());
108
        testFO.refresh();
94
                    assertEquals(TypeKind.LONG, waitMethod.getParameters().get(0).asType().getKind());
109
        javaSource = JavaSource.forFileObject(testFO);
95
                    assertEquals(controller.getElements().getTypeElement("java.lang.Object"), waitMethod.getEnclosingElement());
110
        javaSource.runUserActionTask(new Task<CompilationController>() {
96
                }
111
            public void run(CompilationController controller) throws IOException {
97
                
112
                controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
98
                {
113
                TypeElement typeElement = controller.getElements().getTypeElement("Večerníček");
99
                    Element arrayListInit = controller.getElementUtilities().findElement("java.util.ArrayList.ArrayList(java.util.Collection)");
114
                assertNotNull(typeElement);
100
                    assertNotNull(arrayListInit);
101
                    assertEquals(ElementKind.CONSTRUCTOR, arrayListInit.getKind());
102
                    ExecutableElement arrayListInitMethod = (ExecutableElement) arrayListInit;
103
                    assertEquals("<init>", arrayListInitMethod.getSimpleName().toString());
104
                    assertEquals(1, arrayListInitMethod.getParameters().size());
105
                    assertEquals("java.util.Collection", controller.getTypes().erasure(arrayListInitMethod.getParameters().get(0).asType()).toString());
106
                    assertEquals(controller.getElements().getTypeElement("java.util.ArrayList"), arrayListInitMethod.getEnclosingElement());
107
                }
108
                
109
                {
110
                    Element arrayListAdd = controller.getElementUtilities().findElement("java.util.ArrayList.add(int, Object)");
111
                    assertNotNull(arrayListAdd);
112
                    assertEquals(ElementKind.METHOD, arrayListAdd.getKind());
113
                    ExecutableElement arrayListAddMethod = (ExecutableElement) arrayListAdd;
114
                    assertEquals("add", arrayListAddMethod.getSimpleName().toString());
115
                    assertEquals(2, arrayListAddMethod.getParameters().size());
116
                    assertEquals(TypeKind.INT, arrayListAddMethod.getParameters().get(0).asType().getKind());
117
                    assertEquals("java.lang.Object", controller.getTypes().erasure(arrayListAddMethod.getParameters().get(1).asType()).toString());
118
                    assertEquals(controller.getElements().getTypeElement("java.util.ArrayList"), arrayListAddMethod.getEnclosingElement());
119
                }
120
                
121
                {
122
                    Element arraysAsList = controller.getElementUtilities().findElement("java.util.Arrays.asList(Object...)");
123
                    assertNotNull(arraysAsList);
124
                    assertEquals(ElementKind.METHOD, arraysAsList.getKind());
125
                    ExecutableElement arraysAsListMethod = (ExecutableElement) arraysAsList;
126
                    assertEquals("asList", arraysAsListMethod.getSimpleName().toString());
127
                    assertEquals(1, arraysAsListMethod.getParameters().size());
128
                    assertEquals(TypeKind.ARRAY, arraysAsListMethod.getParameters().get(0).asType().getKind());
129
                    assertEquals(controller.getElements().getTypeElement("java.util.Arrays"), arraysAsListMethod.getEnclosingElement());
130
                }
131
                
132
                {
133
                    Element hashCode = controller.getElementUtilities().findElement("java.lang.Object.hashCode()");
134
                    assertNotNull(hashCode);
135
                    assertEquals(ElementKind.METHOD, hashCode.getKind());
136
                    ExecutableElement hashCodeMethod = (ExecutableElement) hashCode;
137
                    assertEquals("hashCode", hashCodeMethod.getSimpleName().toString());
138
                    assertEquals(0, hashCodeMethod.getParameters().size());
139
                    assertEquals(controller.getElements().getTypeElement("java.lang.Object"), hashCodeMethod.getEnclosingElement());
140
                }
141
                
142
                {
143
                    Element bigIntegerOne = controller.getElementUtilities().findElement("java.math.BigInteger.ONE");
144
                    assertNotNull(bigIntegerOne);
145
                    assertEquals(ElementKind.FIELD, bigIntegerOne.getKind());
146
                    assertEquals("ONE", bigIntegerOne.getSimpleName().toString());
147
                    assertEquals(controller.getElements().getTypeElement("java.math.BigInteger"), bigIntegerOne.getEnclosingElement());
148
                }
149
                
150
                {
151
                    Element bigInteger = controller.getElementUtilities().findElement("java.math.BigInteger");
152
                    assertEquals(controller.getElements().getTypeElement("java.math.BigInteger"), bigInteger);
153
                }
115
            }
154
            }
116
        }, true);
155
        }, true);
117
    }
156
    }
118
157
119
    public void test175535() throws Exception {
120
        prepareTest();
121
        FileObject otherFO = FileUtil.createData(sourceRoot, "test/A.java");
122
        TestUtilities.copyStringToFile(FileUtil.toFile(otherFO),
123
                "package test;" +
124
                "public class A implements Runnable {" +
125
                "    @Override" +
126
                "    public void run() {}" +
127
                "}");
128
        TestUtilities.copyStringToFile(FileUtil.toFile(testFO),
129
                "public class Test {" +
130
                "}");
131
        SourceUtilsTestUtil.compileRecursively(sourceRoot);
132
        JavaSource javaSource = JavaSource.forFileObject(testFO);
133
        javaSource.runUserActionTask(new Task<CompilationController>() {
134
            public void run(CompilationController controller) throws IOException {
135
                controller.toPhase(JavaSource.Phase.RESOLVED);
136
                TypeElement typeElement = controller.getElements().getTypeElement("test.A");
137
                assertNotNull(typeElement);
138
                Element el = typeElement.getEnclosedElements().get(1);
139
                assertNotNull(el);
140
                assertEquals("run", el.getSimpleName().toString());
141
                TreePath mpath = controller.getTrees().getPath(el);
142
                MethodTree mtree = (MethodTree) mpath.getLeaf();
143
                assertNotNull(mtree);
144
                List<? extends AnnotationTree> annotations = mtree.getModifiers().getAnnotations();
145
                TypeMirror annotation = controller.getTrees().getTypeMirror(new TreePath(mpath, annotations.get(0)));
146
                assertNotNull(annotation);
147
                Element e = controller.getTrees().getElement(new TreePath(mpath, annotations.get(0)));
148
                assertNotNull(e);
149
                assertEquals(((DeclaredType)annotation).asElement(), e);
150
            }
151
        }, true);
152
    }
153
}
158
}

Return to bug 227285