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 199405
Collapse All | Expand All

(-)a/java.source.queries/build.xml (+5 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project basedir="." default="netbeans" name="java.source.queries">
3
    <description>Builds, tests, and runs the project org.netbeans.modules.java.source.queries</description>
4
    <import file="../nbbuild/templates/projectized.xml"/>
5
</project>
(-)a/java.source.queries/manifest.mf (+5 lines)
Line 0 Link Here
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.java.source.queries
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/source/queries/Bundle.properties
4
OpenIDE-Module-Specification-Version: 1.0
5
(-)a/java.source.queries/nbproject/project.properties (+3 lines)
Line 0 Link Here
1
is.autoload=true
2
javac.source=1.6
3
javac.compilerargs=-Xlint -Xlint:-serial
(-)a/java.source.queries/nbproject/project.xml (+50 lines)
Line 0 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://www.netbeans.org/ns/project/1">
3
    <type>org.netbeans.modules.apisupport.project</type>
4
    <configuration>
5
        <data xmlns="http://www.netbeans.org/ns/nb-module-project/3">
6
            <code-name-base>org.netbeans.modules.java.source.queries</code-name-base>
7
            <module-dependencies>
8
                <dependency>
9
                    <code-name-base>org.netbeans.api.annotations.common</code-name-base>
10
                    <build-prerequisite/>
11
                    <compile-dependency/>
12
                    <run-dependency>
13
                        <release-version>1</release-version>
14
                        <specification-version>1.10</specification-version>
15
                    </run-dependency>
16
                </dependency>
17
                <dependency>
18
                    <code-name-base>org.openide.filesystems</code-name-base>
19
                    <build-prerequisite/>
20
                    <compile-dependency/>
21
                    <run-dependency>
22
                        <specification-version>7.48</specification-version>
23
                    </run-dependency>
24
                </dependency>
25
                <dependency>
26
                    <code-name-base>org.openide.util</code-name-base>
27
                    <build-prerequisite/>
28
                    <compile-dependency/>
29
                    <run-dependency>
30
                        <specification-version>8.16</specification-version>
31
                    </run-dependency>
32
                </dependency>
33
                <dependency>
34
                    <code-name-base>org.openide.util.lookup</code-name-base>
35
                    <build-prerequisite/>
36
                    <compile-dependency/>
37
                    <run-dependency>
38
                        <specification-version>8.9</specification-version>
39
                    </run-dependency>
40
                </dependency>
41
            </module-dependencies>
42
            <friend-packages>
43
                <friend>org.netbeans.modules.form</friend>
44
                <friend>org.netbeans.modules.java.source.queriesimpl</friend>
45
                <package>org.netbeans.modules.java.source.queries.api</package>
46
                <package>org.netbeans.modules.java.source.queries.spi</package>
47
            </friend-packages>
48
        </data>
49
    </configuration>
50
</project>
(-)a/java.source.queries/src/org/netbeans/modules/java/source/queries/APIAccessor.java (+77 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.java.source.queries;
43
44
import org.netbeans.api.annotations.common.NonNull;
45
import org.netbeans.modules.java.source.queries.api.Queries;
46
import org.netbeans.modules.java.source.queries.spi.ModelOperations;
47
import org.openide.util.Exceptions;
48
49
/**
50
 *
51
 * @author Tomas Zezula
52
 */
53
public abstract class APIAccessor {
54
    private static volatile APIAccessor instance;
55
56
    public static void setInstance(@NonNull final APIAccessor _instance) {
57
        assert _instance != null;
58
        instance = _instance;
59
    }
60
61
    public static synchronized APIAccessor getInstance() {
62
        if (instance == null) {
63
            try {
64
                Class.forName(
65
                        Queries.class.getName(),
66
                        true,
67
                        APIAccessor.class.getClassLoader());
68
            } catch (ClassNotFoundException e) {
69
                Exceptions.printStackTrace(e);
70
            }
71
        }
72
        return instance;
73
    }
74
75
    public abstract void attach(@NonNull Queries q, @NonNull ModelOperations ops);
76
    public abstract void detach(@NonNull Queries q);
77
}
(-)a/java.source.queries/src/org/netbeans/modules/java/source/queries/Bundle.properties (+1 lines)
Line 0 Link Here
1
OpenIDE-Module-Name=Java Source Queries
(-)a/java.source.queries/src/org/netbeans/modules/java/source/queries/QueryException.java (+60 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.java.source.queries;
43
44
/**
45
 * Exception thrown by queries
46
 * @author Tomas Zezula
47
 */
48
public class QueryException extends Exception {
49
50
    public QueryException() {
51
    }
52
53
    public QueryException(final String message) {
54
        super(message);
55
    }
56
57
    public QueryException(final String message, final Throwable rootCause) {
58
        super (message, rootCause);
59
    }
60
}
(-)a/java.source.queries/src/org/netbeans/modules/java/source/queries/SPIAccessor.java (+83 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.java.source.queries;
43
44
import org.netbeans.api.annotations.common.NonNull;
45
import org.netbeans.modules.java.source.queries.api.Function;
46
import org.netbeans.modules.java.source.queries.api.Queries;
47
import org.netbeans.modules.java.source.queries.spi.QueriesController;
48
import org.openide.util.Exceptions;
49
50
/**
51
 *
52
 * @author Tomas Zezula
53
 */
54
public abstract class SPIAccessor {
55
56
    private static volatile SPIAccessor instance;
57
58
    public static synchronized SPIAccessor getInstance() {
59
        if (instance == null) {
60
            try {
61
                Class.forName(
62
                        QueriesController.Context.class.getName(),
63
                        true,
64
                        SPIAccessor.class.getClassLoader());
65
                assert instance != null;
66
            } catch (ClassNotFoundException e) {
67
                Exceptions.printStackTrace(e);
68
            }
69
        }
70
        return instance;
71
    }
72
73
    public static void setInstance(@NonNull final SPIAccessor _instance) {
74
        assert _instance != null;
75
        instance = _instance;
76
    }
77
78
    public abstract <P extends Queries,R>  QueriesController.Context<P,R> createContext(
79
            final Function<P,R> fnc,
80
            final P param
81
            );
82
83
}
(-)a/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Function.java (+55 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.java.source.queries.api;
43
44
/**
45
 * Query or update function
46
 * @author Tomas Zezula
47
 */
48
public interface Function<P,R> {
49
    /**
50
     * Performs the function on given parameter returning a result
51
     * @param param the function parameter
52
     * @return result
53
     */
54
    R apply (P param);
55
}
(-)a/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Queries.java (+252 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.java.source.queries.api;
43
44
import java.util.Collection;
45
import org.netbeans.api.annotations.common.CheckForNull;
46
import org.netbeans.api.annotations.common.NonNull;
47
import org.netbeans.api.annotations.common.NullAllowed;
48
import org.netbeans.modules.java.source.queries.APIAccessor;
49
import org.netbeans.modules.java.source.queries.QueryException;
50
import org.netbeans.modules.java.source.queries.SPIAccessor;
51
import org.netbeans.modules.java.source.queries.spi.ModelOperations;
52
import org.netbeans.modules.java.source.queries.spi.QueriesController;
53
import org.openide.filesystems.FileObject;
54
import org.openide.util.Lookup;
55
import org.openide.util.Parameters;
56
57
/**
58
 * Support for queries
59
 * @author Tomas Zezula
60
 */
61
public class Queries {
62
63
    static {
64
        APIAccessor.setInstance(new Accessor());
65
    }
66
67
    static final QueriesController ctl = Lookup.getDefault().lookup(QueriesController.class);
68
    private final FileObject forFile;
69
    ModelOperations impl;
70
71
    Queries(@NonNull final FileObject forFile) {
72
        assert forFile != null;
73
        this.forFile = forFile;
74
    }
75
76
    /**
77
     * Returns a file for which the {@link Queries} was created.
78
     * @return a file
79
     */
80
    @NonNull
81
    public final FileObject getFile() {
82
        return forFile;
83
    }
84
85
    /**
86
     * Returns the fully qualified names of top level classes in the
87
     * compilation unit represented by {@link Queries#getFile()}
88
     * @return a collection of fully qualified names
89
     * @throws QueryException in case of failure
90
     */
91
    @NonNull
92
    public final Collection<? extends String> getTopLevelClasses() throws QueryException {
93
        return impl.getTopLevelClasses(getFile());
94
    }
95
96
    /**
97
     * Checks if a class represented by fully qualified name is a
98
     * Java Bean. (has no param public constructor).
99
     * @param cls the fully qualified name of the class to be checked
100
     * @return true if the class is a Java Bean
101
     * @throws QueryException in case of failure
102
     */
103
    public final boolean isJavaBean(final @NonNull String cls) throws QueryException {
104
        Parameters.notNull("cls", cls);     //NOI18N
105
        return impl.isJavaBean(cls);
106
    }
107
108
    /**
109
     * Checks if a class represented given fully qualified name is
110
     * available on class path.
111
     * @param cls the fully qualified name of the class
112
     * @return true if the class is available
113
     * @throws QueryException in case of failure
114
     */
115
    public final boolean isAvailable(final @NonNull String cls) throws QueryException {
116
        Parameters.notNull("cls", cls);     //NOI18N
117
        return impl.isAvailable(cls);
118
    }
119
120
    /**
121
     * Returns a binary name as specified by JLS ยง13.1 for given
122
     * fully qualified name
123
     * @param cls the fully qualified name of the class
124
     * @return the binary name
125
     * @throws QueryException in case of failure
126
     */
127
    @NonNull
128
    public final String getClassBinaryName(
129
            @NonNull final String cls) throws QueryException {
130
        Parameters.notNull("cls", cls);     //NOI18N
131
        return impl.getClassBinaryName(cls);
132
    }
133
134
    /**
135
     * Returns the fully qualified name of the super class for
136
     * for given class.
137
     * @param cls the class fully qualified name
138
     * @return the fully qualified name of the super class
139
     * @throws QueryException in case of failure
140
     */
141
    @NonNull
142
    public final String getSuperClass(final @NonNull String cls) throws QueryException {
143
        Parameters.notNull("cls", cls);     //NOI18N
144
        return impl.getSuperClass(cls);
145
    }
146
147
    /**
148
     * Returns names of the methods complying to given types.
149
     * @param cls the fully qualified name of the class to look up methods in
150
     * @param returnType the return type of the method specified by the name of the
151
     * primitive type or fully qualified name of the declared type (null represents
152
     * any type)
153
     * @param paramteterTypes the types of the method parameters specified by
154
     * name of the primitive type or fully qualified name of the declared type
155
     * (null represent any type)
156
     * @return a collection of method names
157
     * @throws QueryException in case of failure
158
     */
159
    @NonNull
160
    public final Collection<? extends String> getMethodNames(
161
            @NonNull final String cls,
162
            @NullAllowed final String returnType,
163
            @NullAllowed final String... paramteterTypes) throws QueryException {
164
        Parameters.notNull("cls", cls); //NOI18N
165
        return impl.getMethodNames(cls, returnType, paramteterTypes);
166
    }
167
168
    /**
169
     * Returns names of the fields complying to type.
170
     * @param cls the fully qualified name of the class to look up methods in
171
     * @param type the type of the field specified by the name of the
172
     * primitive type or fully qualified name of the declared type
173
     * (null represents any type)
174
     * @return a collection of field names
175
     * @throws QueryException in case of failure
176
     */
177
    @NonNull
178
    public final Collection<? extends String> getFieldNames(
179
            @NonNull final String cls,
180
            @NullAllowed final String type) throws QueryException {
181
        Parameters.notNull("cls", cls); //NOI18N
182
        return impl.getFieldNames(cls, type);
183
    }
184
185
    /**
186
     * Returns annotations annotating given method.
187
     * @param cls the fully qualified name of the class to look up methods in
188
     * @param methodName name of the method
189
     * @param returnType the return type of the method specified by the name of the
190
     * primitive type or fully qualified name of the declared type
191
     * @param paramteterTypes the types of the method parameters specified by
192
     * name of the primitive type or fully qualified name of the declared type
193
     * @return collection of fully qualified names
194
     * @throws QueryException in case of failure
195
     */
196
    @NonNull
197
    public final Collection<? extends String> getMethodAnnotations(
198
            @NonNull final String cls,
199
            @NonNull final String methodName,
200
            @NonNull final String returnType,
201
            @NonNull final String... paramteterTypes) throws QueryException {
202
        Parameters.notNull("cls", cls); //NOI18N
203
        Parameters.notNull("methodName", methodName); //NOI18N
204
        Parameters.notNull("returnType", returnType);     //NOI18N
205
        Parameters.notNull("paramteterTypes", paramteterTypes); //NOI18N
206
        return impl.getMethodAnnotations(cls, methodName, returnType, paramteterTypes);
207
    }
208
209
    /**
210
     * Performs the query
211
     * @param forFile file implying the scope to perform query on
212
     * @param queryFnc the query function
213
     * @return the result of query function
214
     * @throws QueryException in case of exception
215
     */
216
    @CheckForNull
217
    public static <T> T query (
218
        @NonNull final FileObject forFile,
219
        @NonNull final Function<Queries,T> queryFnc) throws QueryException {
220
        Parameters.notNull("forFile", forFile);     //NOI18N
221
        Parameters.notNull("queryFnc", queryFnc);   //NOI18N
222
        if (ctl == null) {
223
            throw new IllegalStateException("No QueriesController found in the Lookup");    //NOI18N
224
        }
225
        final Queries q = new Queries(forFile);
226
        final QueriesController.Context<Queries,T> ctx = SPIAccessor.getInstance().createContext(queryFnc, q);
227
        return ctl.runQuery(ctx);
228
    }
229
230
231
    private void attach(@NonNull final ModelOperations impl) {
232
        assert impl != null;
233
        this.impl = impl;
234
    }
235
236
    private void detach() {
237
        this.impl = null;
238
    }
239
240
    private static class Accessor extends APIAccessor {
241
242
        @Override
243
        public void attach(Queries q, ModelOperations ops) {
244
            q.attach(ops);
245
        }
246
247
        @Override
248
        public void detach(Queries q) {
249
            q.detach();
250
        }
251
    }
252
}
(-)a/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Updates.java (+180 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.java.source.queries.api;
43
44
import java.util.Collection;
45
import org.netbeans.api.annotations.common.NonNull;
46
import org.netbeans.modules.java.source.queries.QueryException;
47
import org.netbeans.modules.java.source.queries.SPIAccessor;
48
import org.netbeans.modules.java.source.queries.spi.QueriesController;
49
import org.openide.filesystems.FileObject;
50
import org.openide.util.Parameters;
51
52
/**
53
 * Support for modifications
54
 * @author Tomas Zezula
55
 */
56
public final class Updates extends Queries {
57
58
    Updates(@NonNull final FileObject forFile) {
59
        super(forFile);
60
    }
61
62
    /**
63
     * Modifies class interfaces.
64
     * @param cls the fully qualified name of class to be changed
65
     * @param toAdd the {@link Collection} of fully qualified names of
66
     * interfaces to be added
67
     * @param toRemove the {@link Collection} of fully qualified names of
68
     * interfaces to be removed
69
     * @throws QueryException in case of failure
70
     */
71
    public void modifyInterfaces(
72
            @NonNull String cls,
73
            @NonNull Collection<? extends String> toAdd,
74
            @NonNull Collection<? extends String> toRemove) throws QueryException {
75
        Parameters.notNull("cls", cls);     //NOI18N
76
        Parameters.notNull("toAdd", toAdd);     //NOI18N
77
        Parameters.notNull("toRemove", toRemove);     //NOI18N
78
        impl.modifyInterfaces(cls,toAdd,toRemove);
79
    }
80
81
    /**
82
     * Modifies method annotations.
83
     * @param cls the fully qualified name of the class to look up methods in
84
     * @param methodName name of the method
85
     * @param returnType the return type of the method specified by the name of the
86
     * primitive type or fully qualified name of the declared type
87
     * @param paramteterTypes the types of the method parameters specified by
88
     * name of the primitive type or fully qualified name of the declared type
89
     * @param toAdd the {@link Collection} of fully qualified names of
90
     * annotations to be added
91
     * @param toRemove the {@link Collection} of fully qualified names of
92
     * annotations to be removed
93
     * @throws QueryException in case of failure
94
     */
95
    public void modifyMethodAnnotations(
96
            @NonNull final String cls,
97
            @NonNull final String methodName,
98
            @NonNull final String returnType,
99
            @NonNull final String[] paramteterTypes,
100
            @NonNull final Collection<? extends String> toAdd,
101
            @NonNull final Collection<? extends String> toRemove) throws QueryException {
102
        Parameters.notNull("cls", cls); //NOI18N
103
        Parameters.notNull("methodName", methodName); //NOI18N
104
        Parameters.notNull("returnType", returnType);     //NOI18N
105
        Parameters.notNull("paramteterTypes", paramteterTypes); //NOI18N
106
        Parameters.notNull("toAdd", toAdd); //NOI18N
107
        Parameters.notNull("toRemove", toRemove); //NOI18N
108
        impl.modifyMethodAnnotations(
109
                cls,
110
                methodName,
111
                returnType,
112
                paramteterTypes,
113
                toAdd,
114
                toRemove);
115
    }
116
117
    /**
118
     * Sets class super class.
119
     * @param cls the fully qualified name of class to be changed
120
     * @param superCls the fully qualified name of super class
121
     * @throws QueryException in case of failure
122
     */
123
    public void setSuperClass(
124
            @NonNull String cls,
125
            @NonNull String superCls) throws QueryException {
126
        Parameters.notNull("cls", cls);     //NOI18N
127
        Parameters.notNull("superCls", superCls);   //NOI18N
128
        impl.setSuperClass(cls, superCls);
129
    }
130
131
    /**
132
     * Adds required imports into the java source if needed.
133
     * @param requiredFQNs the fully qualified names of imports to be added
134
     * @throws QueryException in case of failure
135
     */
136
    public void fixImports(
137
            @NonNull final Collection<? extends String> requiredFQNs) throws QueryException {
138
        Parameters.notNull("requiredFQNs", requiredFQNs);     //NOI18N
139
        impl.fixImports(getFile(),requiredFQNs);
140
    }
141
142
    /**
143
     * Renames a field in a class.
144
     * @param cls the fully qualified name of the class owning the field
145
     * @param oldName the old field name
146
     * @param newName the new field name
147
     * @throws QueryException in case of failure
148
     */
149
    public void renameField(
150
            @NonNull final String cls,
151
            @NonNull final String oldName,
152
            @NonNull final String newName) throws QueryException {
153
        Parameters.notNull("oldName", oldName);     //NOI18N
154
        Parameters.notNull("newName", newName);     //NOI18N
155
        impl.renameField(cls,oldName,newName);
156
    }
157
158
159
    /**
160
     * Performs the update.
161
     * @param forFile file implying the scope to perform query on
162
     * @param updateFnc  the query function. If the function returns true
163
     * the changes are applied otherwise the changes are rolled back.
164
     * @throws QueryException in case of exception
165
     */
166
    public static void update(
167
            @NonNull final FileObject forFile,
168
            @NonNull final Function<Updates,Boolean> updateFnc) throws QueryException {
169
        Parameters.notNull("forFile", forFile);     //NOI18N
170
        Parameters.notNull("updateFnc", forFile);   //NOI18N
171
        if (ctl == null) {
172
            throw new IllegalStateException("No QueriesController found in the Lookup");    //NOI18N
173
        }
174
        final Updates u = new Updates(forFile);
175
        final QueriesController.Context<Updates,Boolean> ctx =
176
                SPIAccessor.getInstance().createContext(updateFnc, u);
177
        ctl.runUpdate(ctx);
178
    }
179
180
}
(-)a/java.source.queries/src/org/netbeans/modules/java/source/queries/spi/ModelOperations.java (+230 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.java.source.queries.spi;
43
44
import java.util.Collection;
45
import org.netbeans.api.annotations.common.NonNull;
46
import org.netbeans.api.annotations.common.NullAllowed;
47
import org.netbeans.modules.java.source.queries.QueryException;
48
import org.netbeans.modules.java.source.queries.api.Queries;
49
import org.netbeans.modules.java.source.queries.api.Updates;
50
import org.openide.filesystems.FileObject;
51
52
/**
53
 * SPI for {@link Queries} and {@link Updates}.
54
 * The instance is created by implementor of {@link QueriesController}
55
 * and passed to {@link QueriesController.Context}
56
 * @author Tomas Zezula
57
 */
58
public interface ModelOperations {
59
60
    /**
61
     * Returns the fully qualified names of top level classes in the
62
     * compilation unit represented by given file
63
     * @param file to get the top level classes
64
     * @return a collection of fully qualified names
65
     * @throws QueryException in case of failure
66
     */
67
    @NonNull
68
    Collection<? extends String> getTopLevelClasses(@NonNull FileObject file) throws QueryException;
69
70
    /**
71
     * Checks if a class represented by fully qualified name is a
72
     * Java Bean (has no param public constructor).
73
     * @param cls the fully qualified name of the class to be checked
74
     * @return true if the class is a Java Bean
75
     * @throws QueryException in case of failure
76
     */
77
    boolean isJavaBean(@NonNull String cls) throws QueryException;
78
79
    /**
80
     * Checks if a class represented given fully qualified name is
81
     * available on class path.
82
     * @param cls the fully qualified name of the class
83
     * @return true if the class is available
84
     * @throws QueryException in case of failure
85
     */
86
    boolean isAvailable(@NonNull String cls) throws QueryException;
87
88
    /**
89
     * Returns the fully qualified name of the super class for
90
     * for given class.
91
     * @param cls the class fully qualified name
92
     * @return the fully qualified name of the super class
93
     * @throws QueryException in case of failure
94
     */
95
    @NonNull
96
    String getSuperClass(@NonNull String cls) throws QueryException;
97
98
    /**
99
     * Returns names of the methods complying to given types.
100
     * @param cls the fully qualified name of the class to look up methods in
101
     * @param returnType the return type of the method specified by the name of the
102
     * primitive type or fully qualified name of the declared type (null represents
103
     * any type)
104
     * @param paramteterTypes the types of the method parameters specified by
105
     * name of the primitive type or fully qualified name of the declared type
106
     * (null represent any type)
107
     * @return a collection of method names
108
     * @throws QueryException in case of failure
109
     */
110
    @NonNull
111
    Collection<? extends String> getMethodNames(
112
            @NonNull String cls,
113
            @NullAllowed String returnType,
114
            @NullAllowed String... parameterTypes) throws QueryException;
115
116
117
    /**
118
     * Returns names of the fields complying to type.
119
     * @param cls the fully qualified name of the class to look up methods in
120
     * @param type the type of the field specified by the name of the
121
     * primitive type or fully qualified name of the declared type
122
     * (null represents any type)
123
     * @return a collection of field names
124
     * @throws QueryException in case of failure
125
     */
126
    @NonNull
127
    Collection<? extends String> getFieldNames(
128
            @NonNull String cls,
129
            @NullAllowed String type) throws QueryException;
130
131
132
    /**
133
     * Returns annotations annotating given method.
134
     * @param cls the fully qualified name of the class to look up methods in
135
     * @param methodName name of the method
136
     * @param returnType the return type of the method specified by the name of the
137
     * primitive type or fully qualified name of the declared type
138
     * @param paramteterTypes the types of the method parameters specified by
139
     * name of the primitive type or fully qualified name of the declared type
140
     * @return collection of fully qualified names
141
     * @throws QueryException in case of failure
142
     */
143
    Collection<? extends String> getMethodAnnotations(
144
            @NonNull final String cls,
145
            @NonNull final String methodName,
146
            @NonNull final String returnType,
147
            @NonNull final String... paramteterTypes) throws QueryException;
148
149
    /**
150
     * Returns a binary name as specified by JLS ยง13.1 for given
151
     * fully qualified name.
152
     * @param cls the fully qualified name of the class
153
     * @return the binary name
154
     * @throws QueryException in case of failure
155
     */
156
    @NonNull
157
    String getClassBinaryName(@NonNull String cls) throws QueryException;
158
159
160
    /**
161
     * Adds required imports into the java source if needed.
162
     * @param file a file to add imports into
163
     * @param requiredFQNs the fully qualified names of imports to be added
164
     * @throws QueryException in case of failure
165
     */
166
    void fixImports(
167
            @NonNull FileObject file,
168
            Collection<? extends String> requiredFQNs) throws QueryException;
169
170
171
    /**
172
     * Modifies class interfaces.
173
     * @param cls the fully qualified name of class to be changed
174
     * @param toAdd the {@link Collection} of fully qualified names of
175
     * interfaces to be added
176
     * @param toRemove the {@link Collection} of fully qualified names of
177
     * interfaces to be removed
178
     * @throws QueryException in case of failure
179
     */
180
    void modifyInterfaces(
181
            @NonNull String cls,
182
            @NonNull Collection<? extends String> toAdd,
183
            @NonNull Collection<? extends String> toRemove) throws QueryException;
184
185
186
    /**
187
     * Modifies method annotations.
188
     * @param cls the fully qualified name of the class to look up methods in
189
     * @param methodName name of the method
190
     * @param returnType the return type of the method specified by the name of the
191
     * primitive type or fully qualified name of the declared type
192
     * @param paramteterTypes the types of the method parameters specified by
193
     * name of the primitive type or fully qualified name of the declared type
194
     * @param toAdd the {@link Collection} of fully qualified names of
195
     * annotations to be added
196
     * @param toRemove the {@link Collection} of fully qualified names of
197
     * annotations to be removed
198
     * @throws QueryException in case of failure
199
     */
200
    void modifyMethodAnnotations(
201
            @NonNull String cls,
202
            @NonNull String methodName,
203
            @NonNull String returnType,
204
            @NonNull String[] paramteterTypes,
205
            @NonNull Collection<? extends String> toAdd,
206
            @NonNull Collection<? extends String> toRemove) throws QueryException;
207
208
    /**
209
     * Sets class super class.
210
     * @param cls the fully qualified name of class to be changed
211
     * @param superCls the fully qualified name of super class
212
     * @throws QueryException in case of failure
213
     */
214
    void setSuperClass(
215
            @NonNull String cls,
216
            @NonNull String superCls) throws QueryException;
217
218
219
    /**
220
     * Renames a field in a class.
221
     * @param cls the fully qualified name of the class owning the field
222
     * @param oldName the old field name
223
     * @param newName the new field name
224
     * @throws QueryException in case of failure
225
     */
226
    void renameField(
227
            @NonNull String cls,
228
            @NonNull String oldName,
229
            @NonNull String newName) throws QueryException;
230
}
(-)a/java.source.queries/src/org/netbeans/modules/java/source/queries/spi/QueriesController.java (+130 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.java.source.queries.spi;
43
44
import org.netbeans.api.annotations.common.NonNull;
45
import org.netbeans.modules.java.source.queries.APIAccessor;
46
import org.netbeans.modules.java.source.queries.QueryException;
47
import org.netbeans.modules.java.source.queries.SPIAccessor;
48
import org.netbeans.modules.java.source.queries.api.Function;
49
import org.netbeans.modules.java.source.queries.api.Queries;
50
import org.netbeans.modules.java.source.queries.api.Updates;
51
import org.openide.filesystems.FileObject;
52
53
/**
54
 * SPI for the {@link Queries} and {@link Updates}.
55
 * The implementation is registered in META-INF/services.
56
 * @author Tomas Zezula
57
 */
58
public interface QueriesController {
59
    /**
60
     * Performs the query.
61
     * @param ctx the query {@link Context} holding the
62
     * query function and query parameter
63
     * @return the result of the query function
64
     * @throws IOException in case of error
65
     */
66
    <R> R runQuery(@NonNull Context<Queries,R> ctx) throws QueryException;
67
68
    /**
69
     * Performs the modification.
70
     * @param ctx the modification {@link Context} holding the
71
     * modification function and modification parameter
72
     * @throws IOException in case of error
73
     */
74
    void runUpdate(@NonNull Context<Updates,Boolean> ctx) throws QueryException;
75
76
    /**
77
     * Context of the query or modification
78
     */
79
    public static final class Context<P extends Queries,R> {
80
        private final Function<P,R> toRun;
81
        private final P param;
82
        private final FileObject forFile;
83
84
        Context (
85
            @NonNull final FileObject forFile,
86
            @NonNull final Function<P,R> toRun,
87
            @NonNull final P param) {
88
            assert forFile != null;
89
            assert toRun != null;
90
            assert param != null;
91
            this.toRun = toRun;
92
            this.param = param;
93
            this.forFile = forFile;
94
        }
95
96
        /**
97
         * Return the file for which the {@link Queries} or
98
         * {@link Updates} instance was created.
99
         * @return the file
100
         */
101
        public FileObject getFile(){
102
            return forFile;
103
        }
104
105
        /**
106
         * Performs the query function on query parameter.
107
         * using the provided {@link ModelOperations}
108
         * @param op the {@link ModelOperations} SPI to used to
109
         * perform the operation.
110
         * @return the query function result
111
         */
112
        public R execute(ModelOperations op) {
113
            APIAccessor.getInstance().attach(param, op);
114
            try {
115
                return toRun.apply(param);
116
            } finally {
117
                APIAccessor.getInstance().detach(param);
118
            }
119
        }
120
121
        private static class Accessor extends SPIAccessor {
122
            @Override
123
            public <P extends Queries,R>  QueriesController.Context<P,R> createContext(
124
                final Function<P,R> fnc,
125
                final P param) {
126
                return new Context<P, R>(param.getFile(), fnc, param);
127
            }
128
        }
129
    }
130
}

Return to bug 199405