Introducing ConditionAPIHacks.getDeclaration.
1.1 --- a/file/src/org/netbeans/modules/jackpot30/file/APIAccessor.java Sun Jan 23 13:59:19 2011 +0100
1.2 +++ b/file/src/org/netbeans/modules/jackpot30/file/APIAccessor.java Sun Jan 23 16:49:34 2011 +0100
1.3 @@ -72,4 +72,6 @@
1.4 public abstract Map<String, Collection<? extends TreePath>> getMultiVariables(Context ctx);
1.5 public abstract Map<String, String> getVariableNames(Context ctx);
1.6
1.7 + public abstract Variable enterAuxiliaryVariable(Context ctx, TreePath source);
1.8 +
1.9 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/file/src/org/netbeans/modules/jackpot30/file/ConditionAPIHacks.java Sun Jan 23 16:49:34 2011 +0100
2.3 @@ -0,0 +1,74 @@
2.4 +/*
2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2.6 + *
2.7 + * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
2.8 + *
2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
2.10 + * Other names may be trademarks of their respective owners.
2.11 + *
2.12 + * The contents of this file are subject to the terms of either the GNU
2.13 + * General Public License Version 2 only ("GPL") or the Common
2.14 + * Development and Distribution License("CDDL") (collectively, the
2.15 + * "License"). You may not use this file except in compliance with the
2.16 + * License. You can obtain a copy of the License at
2.17 + * http://www.netbeans.org/cddl-gplv2.html
2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
2.19 + * specific language governing permissions and limitations under the
2.20 + * License. When distributing the software, include this License Header
2.21 + * Notice in each file and include the License file at
2.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
2.23 + * particular file as subject to the "Classpath" exception as provided
2.24 + * by Oracle in the GPL Version 2 section of the License file that
2.25 + * accompanied this code. If applicable, add the following below the
2.26 + * License Header, with the fields enclosed by brackets [] replaced by
2.27 + * your own identifying information:
2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
2.29 + *
2.30 + * If you wish your version of this file to be governed by only the CDDL
2.31 + * or only the GPL Version 2, indicate your decision by adding
2.32 + * "[Contributor] elects to include this software in this distribution
2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
2.34 + * single choice of license, a recipient has the option to distribute
2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
2.36 + * to extend the choice of license to its licensees as provided above.
2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
2.38 + * Version 2 license, then the option applies only if the new code is
2.39 + * made subject to such option by the copyright holder.
2.40 + *
2.41 + * Contributor(s):
2.42 + *
2.43 + * Portions Copyrighted 2011 Sun Microsystems, Inc.
2.44 + */
2.45 +
2.46 +package org.netbeans.modules.jackpot30.file;
2.47 +
2.48 +import com.sun.source.util.TreePath;
2.49 +import javax.lang.model.element.Element;
2.50 +import org.netbeans.api.annotations.common.CheckForNull;
2.51 +import org.netbeans.api.annotations.common.NonNull;
2.52 +import org.netbeans.api.annotations.common.NullAllowed;
2.53 +import org.netbeans.api.java.source.CompilationInfo;
2.54 +import org.netbeans.modules.jackpot30.file.conditionapi.Context;
2.55 +import org.netbeans.modules.jackpot30.file.conditionapi.Variable;
2.56 +
2.57 +/**Methods that can be used from declarative hints' conditions, but that are not
2.58 + * a good solution for the problem they solve.
2.59 + *
2.60 + * @author lahvac
2.61 + */
2.62 +public class ConditionAPIHacks {
2.63 +
2.64 + public @CheckForNull Variable getDeclaration(@NonNull Context ctx, @NonNull Variable forVar) {
2.65 + CompilationInfo info = APIAccessor.IMPL.getHintContext(ctx).getInfo();
2.66 + TreePath path = APIAccessor.IMPL.getSingleVariable(ctx, forVar);
2.67 + Element el = info.getTrees().getElement(path);
2.68 +
2.69 + if (el == null) return null;
2.70 +
2.71 + TreePath source = info.getTrees().getPath(el);
2.72 +
2.73 + if (source == null) return null;
2.74 +
2.75 + return APIAccessor.IMPL.enterAuxiliaryVariable(ctx, source);
2.76 + }
2.77 +}
3.1 --- a/file/src/org/netbeans/modules/jackpot30/file/conditionapi/Context.java Sun Jan 23 13:59:19 2011 +0100
3.2 +++ b/file/src/org/netbeans/modules/jackpot30/file/conditionapi/Context.java Sun Jan 23 16:49:34 2011 +0100
3.3 @@ -144,13 +144,17 @@
3.4 return null;
3.5 }
3.6
3.7 + return enterAuxiliaryVariable(tp.getParentPath());
3.8 + }
3.9 +
3.10 + private Variable enterAuxiliaryVariable(TreePath path) {
3.11 String output = "*" + auxiliaryVariableCounter.getAndIncrement();
3.12
3.13 - variables.get(0).put(output, tp.getParentPath());
3.14 + variables.get(0).put(output, path);
3.15
3.16 return new Variable(output);
3.17 }
3.18 -
3.19 +
3.20 public @NonNull Variable variableForName(@NonNull String variableName) {
3.21 Variable result = new Variable(variableName);
3.22
3.23 @@ -301,5 +305,10 @@
3.24 return result;
3.25 }
3.26
3.27 + @Override
3.28 + public Variable enterAuxiliaryVariable(Context ctx, TreePath source) {
3.29 + return ctx.enterAuxiliaryVariable(source);
3.30 + }
3.31 +
3.32 }
3.33 }