diff --git a/java.source.queries/build.xml b/java.source.queries/build.xml new file mode 100644 --- /dev/null +++ b/java.source.queries/build.xml @@ -0,0 +1,5 @@ + + + Builds, tests, and runs the project org.netbeans.modules.java.source.queries + + diff --git a/java.source.queries/manifest.mf b/java.source.queries/manifest.mf new file mode 100644 --- /dev/null +++ b/java.source.queries/manifest.mf @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +OpenIDE-Module: org.netbeans.modules.java.source.queries +OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/source/queries/Bundle.properties +OpenIDE-Module-Specification-Version: 1.0 + diff --git a/java.source.queries/nbproject/project.properties b/java.source.queries/nbproject/project.properties new file mode 100644 --- /dev/null +++ b/java.source.queries/nbproject/project.properties @@ -0,0 +1,3 @@ +is.autoload=true +javac.source=1.6 +javac.compilerargs=-Xlint -Xlint:-serial diff --git a/java.source.queries/nbproject/project.xml b/java.source.queries/nbproject/project.xml new file mode 100644 --- /dev/null +++ b/java.source.queries/nbproject/project.xml @@ -0,0 +1,50 @@ + + + org.netbeans.modules.apisupport.project + + + org.netbeans.modules.java.source.queries + + + org.netbeans.api.annotations.common + + + + 1 + 1.10 + + + + org.openide.filesystems + + + + 7.48 + + + + org.openide.util + + + + 8.16 + + + + org.openide.util.lookup + + + + 8.9 + + + + + org.netbeans.modules.form + org.netbeans.modules.java.source.queriesimpl + org.netbeans.modules.java.source.queries.api + org.netbeans.modules.java.source.queries.spi + + + + diff --git a/java.source.queries/src/org/netbeans/modules/java/source/queries/APIAccessor.java b/java.source.queries/src/org/netbeans/modules/java/source/queries/APIAccessor.java new file mode 100644 --- /dev/null +++ b/java.source.queries/src/org/netbeans/modules/java/source/queries/APIAccessor.java @@ -0,0 +1,77 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.source.queries; + +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.modules.java.source.queries.api.Queries; +import org.netbeans.modules.java.source.queries.spi.ModelOperations; +import org.openide.util.Exceptions; + +/** + * + * @author Tomas Zezula + */ +public abstract class APIAccessor { + private static volatile APIAccessor instance; + + public static void setInstance(@NonNull final APIAccessor _instance) { + assert _instance != null; + instance = _instance; + } + + public static synchronized APIAccessor getInstance() { + if (instance == null) { + try { + Class.forName( + Queries.class.getName(), + true, + APIAccessor.class.getClassLoader()); + } catch (ClassNotFoundException e) { + Exceptions.printStackTrace(e); + } + } + return instance; + } + + public abstract void attach(@NonNull Queries q, @NonNull ModelOperations ops); + public abstract void detach(@NonNull Queries q); +} diff --git a/java.source.queries/src/org/netbeans/modules/java/source/queries/Bundle.properties b/java.source.queries/src/org/netbeans/modules/java/source/queries/Bundle.properties new file mode 100644 --- /dev/null +++ b/java.source.queries/src/org/netbeans/modules/java/source/queries/Bundle.properties @@ -0,0 +1,1 @@ +OpenIDE-Module-Name=Java Source Queries diff --git a/java.source.queries/src/org/netbeans/modules/java/source/queries/QueryException.java b/java.source.queries/src/org/netbeans/modules/java/source/queries/QueryException.java new file mode 100644 --- /dev/null +++ b/java.source.queries/src/org/netbeans/modules/java/source/queries/QueryException.java @@ -0,0 +1,60 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.source.queries; + +/** + * Exception thrown by queries + * @author Tomas Zezula + */ +public class QueryException extends Exception { + + public QueryException() { + } + + public QueryException(final String message) { + super(message); + } + + public QueryException(final String message, final Throwable rootCause) { + super (message, rootCause); + } +} diff --git a/java.source.queries/src/org/netbeans/modules/java/source/queries/SPIAccessor.java b/java.source.queries/src/org/netbeans/modules/java/source/queries/SPIAccessor.java new file mode 100644 --- /dev/null +++ b/java.source.queries/src/org/netbeans/modules/java/source/queries/SPIAccessor.java @@ -0,0 +1,83 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.source.queries; + +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.modules.java.source.queries.api.Function; +import org.netbeans.modules.java.source.queries.api.Queries; +import org.netbeans.modules.java.source.queries.spi.QueriesController; +import org.openide.util.Exceptions; + +/** + * + * @author Tomas Zezula + */ +public abstract class SPIAccessor { + + private static volatile SPIAccessor instance; + + public static synchronized SPIAccessor getInstance() { + if (instance == null) { + try { + Class.forName( + QueriesController.Context.class.getName(), + true, + SPIAccessor.class.getClassLoader()); + assert instance != null; + } catch (ClassNotFoundException e) { + Exceptions.printStackTrace(e); + } + } + return instance; + } + + public static void setInstance(@NonNull final SPIAccessor _instance) { + assert _instance != null; + instance = _instance; + } + + public abstract

QueriesController.Context createContext( + final Function fnc, + final P param + ); + +} diff --git a/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Function.java b/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Function.java new file mode 100644 --- /dev/null +++ b/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Function.java @@ -0,0 +1,55 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.source.queries.api; + +/** + * Query or update function + * @author Tomas Zezula + */ +public interface Function { + /** + * Performs the function on given parameter returning a result + * @param param the function parameter + * @return result + */ + R apply (P param); +} diff --git a/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Queries.java b/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Queries.java new file mode 100644 --- /dev/null +++ b/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Queries.java @@ -0,0 +1,252 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.source.queries.api; + +import java.util.Collection; +import org.netbeans.api.annotations.common.CheckForNull; +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.api.annotations.common.NullAllowed; +import org.netbeans.modules.java.source.queries.APIAccessor; +import org.netbeans.modules.java.source.queries.QueryException; +import org.netbeans.modules.java.source.queries.SPIAccessor; +import org.netbeans.modules.java.source.queries.spi.ModelOperations; +import org.netbeans.modules.java.source.queries.spi.QueriesController; +import org.openide.filesystems.FileObject; +import org.openide.util.Lookup; +import org.openide.util.Parameters; + +/** + * Support for queries + * @author Tomas Zezula + */ +public class Queries { + + static { + APIAccessor.setInstance(new Accessor()); + } + + static final QueriesController ctl = Lookup.getDefault().lookup(QueriesController.class); + private final FileObject forFile; + ModelOperations impl; + + Queries(@NonNull final FileObject forFile) { + assert forFile != null; + this.forFile = forFile; + } + + /** + * Returns a file for which the {@link Queries} was created. + * @return a file + */ + @NonNull + public final FileObject getFile() { + return forFile; + } + + /** + * Returns the fully qualified names of top level classes in the + * compilation unit represented by {@link Queries#getFile()} + * @return a collection of fully qualified names + * @throws QueryException in case of failure + */ + @NonNull + public final Collection getTopLevelClasses() throws QueryException { + return impl.getTopLevelClasses(getFile()); + } + + /** + * Checks if a class represented by fully qualified name is a + * Java Bean. (has no param public constructor). + * @param cls the fully qualified name of the class to be checked + * @return true if the class is a Java Bean + * @throws QueryException in case of failure + */ + public final boolean isJavaBean(final @NonNull String cls) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + return impl.isJavaBean(cls); + } + + /** + * Checks if a class represented given fully qualified name is + * available on class path. + * @param cls the fully qualified name of the class + * @return true if the class is available + * @throws QueryException in case of failure + */ + public final boolean isAvailable(final @NonNull String cls) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + return impl.isAvailable(cls); + } + + /** + * Returns a binary name as specified by JLS §13.1 for given + * fully qualified name + * @param cls the fully qualified name of the class + * @return the binary name + * @throws QueryException in case of failure + */ + @NonNull + public final String getClassBinaryName( + @NonNull final String cls) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + return impl.getClassBinaryName(cls); + } + + /** + * Returns the fully qualified name of the super class for + * for given class. + * @param cls the class fully qualified name + * @return the fully qualified name of the super class + * @throws QueryException in case of failure + */ + @NonNull + public final String getSuperClass(final @NonNull String cls) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + return impl.getSuperClass(cls); + } + + /** + * Returns names of the methods complying to given types. + * @param cls the fully qualified name of the class to look up methods in + * @param returnType the return type of the method specified by the name of the + * primitive type or fully qualified name of the declared type (null represents + * any type) + * @param paramteterTypes the types of the method parameters specified by + * name of the primitive type or fully qualified name of the declared type + * (null represent any type) + * @return a collection of method names + * @throws QueryException in case of failure + */ + @NonNull + public final Collection getMethodNames( + @NonNull final String cls, + @NullAllowed final String returnType, + @NullAllowed final String... paramteterTypes) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + return impl.getMethodNames(cls, returnType, paramteterTypes); + } + + /** + * Returns names of the fields complying to type. + * @param cls the fully qualified name of the class to look up methods in + * @param type the type of the field specified by the name of the + * primitive type or fully qualified name of the declared type + * (null represents any type) + * @return a collection of field names + * @throws QueryException in case of failure + */ + @NonNull + public final Collection getFieldNames( + @NonNull final String cls, + @NullAllowed final String type) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + return impl.getFieldNames(cls, type); + } + + /** + * Returns annotations annotating given method. + * @param cls the fully qualified name of the class to look up methods in + * @param methodName name of the method + * @param returnType the return type of the method specified by the name of the + * primitive type or fully qualified name of the declared type + * @param paramteterTypes the types of the method parameters specified by + * name of the primitive type or fully qualified name of the declared type + * @return collection of fully qualified names + * @throws QueryException in case of failure + */ + @NonNull + public final Collection getMethodAnnotations( + @NonNull final String cls, + @NonNull final String methodName, + @NonNull final String returnType, + @NonNull final String... paramteterTypes) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + Parameters.notNull("methodName", methodName); //NOI18N + Parameters.notNull("returnType", returnType); //NOI18N + Parameters.notNull("paramteterTypes", paramteterTypes); //NOI18N + return impl.getMethodAnnotations(cls, methodName, returnType, paramteterTypes); + } + + /** + * Performs the query + * @param forFile file implying the scope to perform query on + * @param queryFnc the query function + * @return the result of query function + * @throws QueryException in case of exception + */ + @CheckForNull + public static T query ( + @NonNull final FileObject forFile, + @NonNull final Function queryFnc) throws QueryException { + Parameters.notNull("forFile", forFile); //NOI18N + Parameters.notNull("queryFnc", queryFnc); //NOI18N + if (ctl == null) { + throw new IllegalStateException("No QueriesController found in the Lookup"); //NOI18N + } + final Queries q = new Queries(forFile); + final QueriesController.Context ctx = SPIAccessor.getInstance().createContext(queryFnc, q); + return ctl.runQuery(ctx); + } + + + private void attach(@NonNull final ModelOperations impl) { + assert impl != null; + this.impl = impl; + } + + private void detach() { + this.impl = null; + } + + private static class Accessor extends APIAccessor { + + @Override + public void attach(Queries q, ModelOperations ops) { + q.attach(ops); + } + + @Override + public void detach(Queries q) { + q.detach(); + } + } +} diff --git a/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Updates.java b/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Updates.java new file mode 100644 --- /dev/null +++ b/java.source.queries/src/org/netbeans/modules/java/source/queries/api/Updates.java @@ -0,0 +1,180 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.source.queries.api; + +import java.util.Collection; +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.modules.java.source.queries.QueryException; +import org.netbeans.modules.java.source.queries.SPIAccessor; +import org.netbeans.modules.java.source.queries.spi.QueriesController; +import org.openide.filesystems.FileObject; +import org.openide.util.Parameters; + +/** + * Support for modifications + * @author Tomas Zezula + */ +public final class Updates extends Queries { + + Updates(@NonNull final FileObject forFile) { + super(forFile); + } + + /** + * Modifies class interfaces. + * @param cls the fully qualified name of class to be changed + * @param toAdd the {@link Collection} of fully qualified names of + * interfaces to be added + * @param toRemove the {@link Collection} of fully qualified names of + * interfaces to be removed + * @throws QueryException in case of failure + */ + public void modifyInterfaces( + @NonNull String cls, + @NonNull Collection toAdd, + @NonNull Collection toRemove) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + Parameters.notNull("toAdd", toAdd); //NOI18N + Parameters.notNull("toRemove", toRemove); //NOI18N + impl.modifyInterfaces(cls,toAdd,toRemove); + } + + /** + * Modifies method annotations. + * @param cls the fully qualified name of the class to look up methods in + * @param methodName name of the method + * @param returnType the return type of the method specified by the name of the + * primitive type or fully qualified name of the declared type + * @param paramteterTypes the types of the method parameters specified by + * name of the primitive type or fully qualified name of the declared type + * @param toAdd the {@link Collection} of fully qualified names of + * annotations to be added + * @param toRemove the {@link Collection} of fully qualified names of + * annotations to be removed + * @throws QueryException in case of failure + */ + public void modifyMethodAnnotations( + @NonNull final String cls, + @NonNull final String methodName, + @NonNull final String returnType, + @NonNull final String[] paramteterTypes, + @NonNull final Collection toAdd, + @NonNull final Collection toRemove) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + Parameters.notNull("methodName", methodName); //NOI18N + Parameters.notNull("returnType", returnType); //NOI18N + Parameters.notNull("paramteterTypes", paramteterTypes); //NOI18N + Parameters.notNull("toAdd", toAdd); //NOI18N + Parameters.notNull("toRemove", toRemove); //NOI18N + impl.modifyMethodAnnotations( + cls, + methodName, + returnType, + paramteterTypes, + toAdd, + toRemove); + } + + /** + * Sets class super class. + * @param cls the fully qualified name of class to be changed + * @param superCls the fully qualified name of super class + * @throws QueryException in case of failure + */ + public void setSuperClass( + @NonNull String cls, + @NonNull String superCls) throws QueryException { + Parameters.notNull("cls", cls); //NOI18N + Parameters.notNull("superCls", superCls); //NOI18N + impl.setSuperClass(cls, superCls); + } + + /** + * Adds required imports into the java source if needed. + * @param requiredFQNs the fully qualified names of imports to be added + * @throws QueryException in case of failure + */ + public void fixImports( + @NonNull final Collection requiredFQNs) throws QueryException { + Parameters.notNull("requiredFQNs", requiredFQNs); //NOI18N + impl.fixImports(getFile(),requiredFQNs); + } + + /** + * Renames a field in a class. + * @param cls the fully qualified name of the class owning the field + * @param oldName the old field name + * @param newName the new field name + * @throws QueryException in case of failure + */ + public void renameField( + @NonNull final String cls, + @NonNull final String oldName, + @NonNull final String newName) throws QueryException { + Parameters.notNull("oldName", oldName); //NOI18N + Parameters.notNull("newName", newName); //NOI18N + impl.renameField(cls,oldName,newName); + } + + + /** + * Performs the update. + * @param forFile file implying the scope to perform query on + * @param updateFnc the query function. If the function returns true + * the changes are applied otherwise the changes are rolled back. + * @throws QueryException in case of exception + */ + public static void update( + @NonNull final FileObject forFile, + @NonNull final Function updateFnc) throws QueryException { + Parameters.notNull("forFile", forFile); //NOI18N + Parameters.notNull("updateFnc", forFile); //NOI18N + if (ctl == null) { + throw new IllegalStateException("No QueriesController found in the Lookup"); //NOI18N + } + final Updates u = new Updates(forFile); + final QueriesController.Context ctx = + SPIAccessor.getInstance().createContext(updateFnc, u); + ctl.runUpdate(ctx); + } + +} diff --git a/java.source.queries/src/org/netbeans/modules/java/source/queries/spi/ModelOperations.java b/java.source.queries/src/org/netbeans/modules/java/source/queries/spi/ModelOperations.java new file mode 100644 --- /dev/null +++ b/java.source.queries/src/org/netbeans/modules/java/source/queries/spi/ModelOperations.java @@ -0,0 +1,230 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.source.queries.spi; + +import java.util.Collection; +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.api.annotations.common.NullAllowed; +import org.netbeans.modules.java.source.queries.QueryException; +import org.netbeans.modules.java.source.queries.api.Queries; +import org.netbeans.modules.java.source.queries.api.Updates; +import org.openide.filesystems.FileObject; + +/** + * SPI for {@link Queries} and {@link Updates}. + * The instance is created by implementor of {@link QueriesController} + * and passed to {@link QueriesController.Context} + * @author Tomas Zezula + */ +public interface ModelOperations { + + /** + * Returns the fully qualified names of top level classes in the + * compilation unit represented by given file + * @param file to get the top level classes + * @return a collection of fully qualified names + * @throws QueryException in case of failure + */ + @NonNull + Collection getTopLevelClasses(@NonNull FileObject file) throws QueryException; + + /** + * Checks if a class represented by fully qualified name is a + * Java Bean (has no param public constructor). + * @param cls the fully qualified name of the class to be checked + * @return true if the class is a Java Bean + * @throws QueryException in case of failure + */ + boolean isJavaBean(@NonNull String cls) throws QueryException; + + /** + * Checks if a class represented given fully qualified name is + * available on class path. + * @param cls the fully qualified name of the class + * @return true if the class is available + * @throws QueryException in case of failure + */ + boolean isAvailable(@NonNull String cls) throws QueryException; + + /** + * Returns the fully qualified name of the super class for + * for given class. + * @param cls the class fully qualified name + * @return the fully qualified name of the super class + * @throws QueryException in case of failure + */ + @NonNull + String getSuperClass(@NonNull String cls) throws QueryException; + + /** + * Returns names of the methods complying to given types. + * @param cls the fully qualified name of the class to look up methods in + * @param returnType the return type of the method specified by the name of the + * primitive type or fully qualified name of the declared type (null represents + * any type) + * @param paramteterTypes the types of the method parameters specified by + * name of the primitive type or fully qualified name of the declared type + * (null represent any type) + * @return a collection of method names + * @throws QueryException in case of failure + */ + @NonNull + Collection getMethodNames( + @NonNull String cls, + @NullAllowed String returnType, + @NullAllowed String... parameterTypes) throws QueryException; + + + /** + * Returns names of the fields complying to type. + * @param cls the fully qualified name of the class to look up methods in + * @param type the type of the field specified by the name of the + * primitive type or fully qualified name of the declared type + * (null represents any type) + * @return a collection of field names + * @throws QueryException in case of failure + */ + @NonNull + Collection getFieldNames( + @NonNull String cls, + @NullAllowed String type) throws QueryException; + + + /** + * Returns annotations annotating given method. + * @param cls the fully qualified name of the class to look up methods in + * @param methodName name of the method + * @param returnType the return type of the method specified by the name of the + * primitive type or fully qualified name of the declared type + * @param paramteterTypes the types of the method parameters specified by + * name of the primitive type or fully qualified name of the declared type + * @return collection of fully qualified names + * @throws QueryException in case of failure + */ + Collection getMethodAnnotations( + @NonNull final String cls, + @NonNull final String methodName, + @NonNull final String returnType, + @NonNull final String... paramteterTypes) throws QueryException; + + /** + * Returns a binary name as specified by JLS §13.1 for given + * fully qualified name. + * @param cls the fully qualified name of the class + * @return the binary name + * @throws QueryException in case of failure + */ + @NonNull + String getClassBinaryName(@NonNull String cls) throws QueryException; + + + /** + * Adds required imports into the java source if needed. + * @param file a file to add imports into + * @param requiredFQNs the fully qualified names of imports to be added + * @throws QueryException in case of failure + */ + void fixImports( + @NonNull FileObject file, + Collection requiredFQNs) throws QueryException; + + + /** + * Modifies class interfaces. + * @param cls the fully qualified name of class to be changed + * @param toAdd the {@link Collection} of fully qualified names of + * interfaces to be added + * @param toRemove the {@link Collection} of fully qualified names of + * interfaces to be removed + * @throws QueryException in case of failure + */ + void modifyInterfaces( + @NonNull String cls, + @NonNull Collection toAdd, + @NonNull Collection toRemove) throws QueryException; + + + /** + * Modifies method annotations. + * @param cls the fully qualified name of the class to look up methods in + * @param methodName name of the method + * @param returnType the return type of the method specified by the name of the + * primitive type or fully qualified name of the declared type + * @param paramteterTypes the types of the method parameters specified by + * name of the primitive type or fully qualified name of the declared type + * @param toAdd the {@link Collection} of fully qualified names of + * annotations to be added + * @param toRemove the {@link Collection} of fully qualified names of + * annotations to be removed + * @throws QueryException in case of failure + */ + void modifyMethodAnnotations( + @NonNull String cls, + @NonNull String methodName, + @NonNull String returnType, + @NonNull String[] paramteterTypes, + @NonNull Collection toAdd, + @NonNull Collection toRemove) throws QueryException; + + /** + * Sets class super class. + * @param cls the fully qualified name of class to be changed + * @param superCls the fully qualified name of super class + * @throws QueryException in case of failure + */ + void setSuperClass( + @NonNull String cls, + @NonNull String superCls) throws QueryException; + + + /** + * Renames a field in a class. + * @param cls the fully qualified name of the class owning the field + * @param oldName the old field name + * @param newName the new field name + * @throws QueryException in case of failure + */ + void renameField( + @NonNull String cls, + @NonNull String oldName, + @NonNull String newName) throws QueryException; +} diff --git a/java.source.queries/src/org/netbeans/modules/java/source/queries/spi/QueriesController.java b/java.source.queries/src/org/netbeans/modules/java/source/queries/spi/QueriesController.java new file mode 100644 --- /dev/null +++ b/java.source.queries/src/org/netbeans/modules/java/source/queries/spi/QueriesController.java @@ -0,0 +1,130 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.java.source.queries.spi; + +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.modules.java.source.queries.APIAccessor; +import org.netbeans.modules.java.source.queries.QueryException; +import org.netbeans.modules.java.source.queries.SPIAccessor; +import org.netbeans.modules.java.source.queries.api.Function; +import org.netbeans.modules.java.source.queries.api.Queries; +import org.netbeans.modules.java.source.queries.api.Updates; +import org.openide.filesystems.FileObject; + +/** + * SPI for the {@link Queries} and {@link Updates}. + * The implementation is registered in META-INF/services. + * @author Tomas Zezula + */ +public interface QueriesController { + /** + * Performs the query. + * @param ctx the query {@link Context} holding the + * query function and query parameter + * @return the result of the query function + * @throws IOException in case of error + */ + R runQuery(@NonNull Context ctx) throws QueryException; + + /** + * Performs the modification. + * @param ctx the modification {@link Context} holding the + * modification function and modification parameter + * @throws IOException in case of error + */ + void runUpdate(@NonNull Context ctx) throws QueryException; + + /** + * Context of the query or modification + */ + public static final class Context

{ + private final Function toRun; + private final P param; + private final FileObject forFile; + + Context ( + @NonNull final FileObject forFile, + @NonNull final Function toRun, + @NonNull final P param) { + assert forFile != null; + assert toRun != null; + assert param != null; + this.toRun = toRun; + this.param = param; + this.forFile = forFile; + } + + /** + * Return the file for which the {@link Queries} or + * {@link Updates} instance was created. + * @return the file + */ + public FileObject getFile(){ + return forFile; + } + + /** + * Performs the query function on query parameter. + * using the provided {@link ModelOperations} + * @param op the {@link ModelOperations} SPI to used to + * perform the operation. + * @return the query function result + */ + public R execute(ModelOperations op) { + APIAccessor.getInstance().attach(param, op); + try { + return toRun.apply(param); + } finally { + APIAccessor.getInstance().detach(param); + } + } + + private static class Accessor extends SPIAccessor { + @Override + public

QueriesController.Context createContext( + final Function fnc, + final P param) { + return new Context(param.getFile(), fnc, param); + } + } + } +}