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

(-)a/java.source/apichanges.xml (+14 lines)
Lines 108-113 Link Here
108
    <!-- ACTUAL CHANGES BEGIN HERE: -->
108
    <!-- ACTUAL CHANGES BEGIN HERE: -->
109
109
110
    <changes>
110
    <changes>
111
        <change id="TypeUtilities.getTypeName">
112
             <api name="general"/>
113
             <summary>Added TypeUtilities.getTypeName method.</summary>
114
             <version major="0" minor="62"/>
115
             <date day="6" month="7" year="2010"/>
116
             <author login="jlahoda"/>
117
             <compatibility addition="yes" binary="compatible" deletion="no" deprecation="no" modification="no" semantic="compatible" source="compatible"/>
118
             <description>
119
                 Added <code>TypeUtilities.getTypeName</code> method, which
120
                 allows to print type to string.
121
             </description>
122
             <class package="org.netbeans.api.java.source" name="TypeUtilities"/>
123
             <issue number="104194"/>
124
        </change>
111
        <change id="SourceUtils.getAttributeValueCompletions">
125
        <change id="SourceUtils.getAttributeValueCompletions">
112
             <api name="general"/>
126
             <api name="general"/>
113
             <summary>Added SourceUtils.getAttributeValueCompletions method.</summary>
127
             <summary>Added SourceUtils.getAttributeValueCompletions method.</summary>
(-)a/java.source/nbproject/project.properties (-1 / +1 lines)
Lines 46-52 Link Here
46
javadoc.title=Java Source
46
javadoc.title=Java Source
47
javadoc.arch=${basedir}/arch.xml
47
javadoc.arch=${basedir}/arch.xml
48
javadoc.apichanges=${basedir}/apichanges.xml
48
javadoc.apichanges=${basedir}/apichanges.xml
49
spec.version.base=0.61.0
49
spec.version.base=0.62.0
50
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/javac-api-nb-7.0-b07.jar
50
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/javac-api-nb-7.0-b07.jar
51
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
51
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
52
    ${o.n.core.dir}/lib/boot.jar:\
52
    ${o.n.core.dir}/lib/boot.jar:\
(-)a/java.source/src/org/netbeans/api/java/source/TypeUtilities.java (-1 / +157 lines)
Lines 27-33 Link Here
27
 * Contributor(s):
27
 * Contributor(s):
28
 *
28
 *
29
 * The Original Software is NetBeans. The Initial Developer of the Original
29
 * The Original Software is NetBeans. The Initial Developer of the Original
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2010 Sun
31
 * Microsystems, Inc. All Rights Reserved.
31
 * Microsystems, Inc. All Rights Reserved.
32
 *
32
 *
33
 * If you wish your version of this file to be governed by only the CDDL
33
 * If you wish your version of this file to be governed by only the CDDL
Lines 45-53 Link Here
45
45
46
import com.sun.tools.javac.code.Type;
46
import com.sun.tools.javac.code.Type;
47
import com.sun.tools.javac.code.Types;
47
import com.sun.tools.javac.code.Types;
48
import java.util.Arrays;
49
import java.util.EnumSet;
50
import java.util.Iterator;
48
import java.util.List;
51
import java.util.List;
52
import java.util.Set;
53
import javax.lang.model.element.Element;
54
import javax.lang.model.element.TypeElement;
55
import javax.lang.model.type.ArrayType;
56
import javax.lang.model.type.DeclaredType;
57
import javax.lang.model.type.ErrorType;
49
import javax.lang.model.type.TypeKind;
58
import javax.lang.model.type.TypeKind;
50
import javax.lang.model.type.TypeMirror;
59
import javax.lang.model.type.TypeMirror;
60
import javax.lang.model.type.TypeVariable;
61
import javax.lang.model.type.WildcardType;
62
import javax.lang.model.util.SimpleTypeVisitor6;
63
import org.netbeans.api.annotations.common.CheckReturnValue;
64
import org.netbeans.api.annotations.common.NonNull;
65
import org.netbeans.api.annotations.common.NullAllowed;
51
66
52
/**Various utilities related to the {@link TypeMirror}s.
67
/**Various utilities related to the {@link TypeMirror}s.
53
 * 
68
 * 
Lines 114-117 Link Here
114
        return Types.instance(info.impl.getJavacTask().getContext()).subst((Type)type, l1, l2);
129
        return Types.instance(info.impl.getJavacTask().getContext()).subst((Type)type, l1, l2);
115
    }
130
    }
116
    
131
    
132
    /**Get textual representation of the given type.
133
     * 
134
     * @param type to print
135
     * @param options allows to specify various adjustments to the output text
136
     * @return textual representation of the given type
137
     * @since 0.62
138
     */
139
    public @NonNull @CheckReturnValue CharSequence getTypeName(@NullAllowed TypeMirror type, @NonNull TypeNameOptions... options) {
140
	if (type == null)
141
            return ""; //NOI18N
142
        Set<TypeNameOptions> opt = EnumSet.noneOf(TypeNameOptions.class);
143
        opt.addAll(Arrays.asList(options));
144
        return new TypeNameVisitor(opt.contains(TypeNameOptions.PRINT_AS_VARARG)).visit(type, opt.contains(TypeNameOptions.PRINT_FQN)).toString();
145
    }
146
    
147
    /**Options for the {@link #getTypeName(javax.lang.model.type.TypeMirror, org.netbeans.api.java.source.TypeUtilities.TypeNameOptions[]) } method.
148
     * @since 0.62
149
     */
150
    public enum TypeNameOptions {
151
        /**
152
         * Print declared types as fully qualified names.
153
         */
154
        PRINT_FQN,
155
        /**
156
         * Print "..." instead of "[]".
157
         */
158
        PRINT_AS_VARARG;
159
    }
160
161
    private static final String UNKNOWN = "<unknown>"; //NOI18N
162
    private static final String CAPTURED_WILDCARD = "<captured wildcard>"; //NOI18N
163
    private static class TypeNameVisitor extends SimpleTypeVisitor6<StringBuilder,Boolean> {
164
        
165
        private boolean varArg;
166
        private boolean insideCapturedWildcard = false;
167
        
168
        private TypeNameVisitor(boolean varArg) {
169
            super(new StringBuilder());
170
            this.varArg = varArg;
171
        }
172
        
173
        @Override
174
        public StringBuilder defaultAction(TypeMirror t, Boolean p) {
175
            return DEFAULT_VALUE.append(t);
176
        }
177
        
178
        @Override
179
        public StringBuilder visitDeclared(DeclaredType t, Boolean p) {
180
            Element e = t.asElement();
181
            if (e instanceof TypeElement) {
182
                TypeElement te = (TypeElement)e;
183
                DEFAULT_VALUE.append((p ? te.getQualifiedName() : te.getSimpleName()).toString());
184
                Iterator<? extends TypeMirror> it = t.getTypeArguments().iterator();
185
                if (it.hasNext()) {
186
                    DEFAULT_VALUE.append("<"); //NOI18N
187
                    while(it.hasNext()) {
188
                        visit(it.next(), p);
189
                        if (it.hasNext())
190
                            DEFAULT_VALUE.append(", "); //NOI18N
191
                    }
192
                    DEFAULT_VALUE.append(">"); //NOI18N
193
                }
194
                return DEFAULT_VALUE;                
195
            } else {
196
                return DEFAULT_VALUE.append(UNKNOWN); //NOI18N
197
            }
198
        }
199
                        
200
        @Override
201
        public StringBuilder visitArray(ArrayType t, Boolean p) {
202
            boolean isVarArg = varArg;
203
            varArg = false;
204
            visit(t.getComponentType(), p);
205
            return DEFAULT_VALUE.append(isVarArg ? "..." : "[]"); //NOI18N
206
        }
207
208
        @Override
209
        public StringBuilder visitTypeVariable(TypeVariable t, Boolean p) {
210
            Element e = t.asElement();
211
            if (e != null) {
212
                String name = e.getSimpleName().toString();
213
                if (!CAPTURED_WILDCARD.equals(name))
214
                    return DEFAULT_VALUE.append(name);
215
            }
216
            DEFAULT_VALUE.append("?"); //NOI18N
217
            if (!insideCapturedWildcard) {
218
                insideCapturedWildcard = true;
219
                TypeMirror bound = t.getLowerBound();
220
                if (bound != null && bound.getKind() != TypeKind.NULL) {
221
                    DEFAULT_VALUE.append(" super "); //NOI18N
222
                    visit(bound, p);
223
                } else {
224
                    bound = t.getUpperBound();
225
                    if (bound != null && bound.getKind() != TypeKind.NULL) {
226
                        DEFAULT_VALUE.append(" extends "); //NOI18N
227
                        if (bound.getKind() == TypeKind.TYPEVAR)
228
                            bound = ((TypeVariable)bound).getLowerBound();
229
                        visit(bound, p);
230
                    }
231
                }
232
                insideCapturedWildcard = false;
233
            }
234
            return DEFAULT_VALUE;
235
        }
236
237
        @Override
238
        public StringBuilder visitWildcard(WildcardType t, Boolean p) {
239
            int len = DEFAULT_VALUE.length();
240
            DEFAULT_VALUE.append("?"); //NOI18N
241
            TypeMirror bound = t.getSuperBound();
242
            if (bound == null) {
243
                bound = t.getExtendsBound();
244
                if (bound != null) {
245
                    DEFAULT_VALUE.append(" extends "); //NOI18N
246
                    if (bound.getKind() == TypeKind.WILDCARD)
247
                        bound = ((WildcardType)bound).getSuperBound();
248
                    visit(bound, p);
249
                } else if (len == 0) {
250
                    bound = SourceUtils.getBound(t);
251
                    if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N
252
                        DEFAULT_VALUE.append(" extends "); //NOI18N
253
                        visit(bound, p);
254
                    }
255
                }
256
            } else {
257
                DEFAULT_VALUE.append(" super "); //NOI18N
258
                visit(bound, p);
259
            }
260
            return DEFAULT_VALUE;
261
        }
262
263
        @Override
264
        public StringBuilder visitError(ErrorType t, Boolean p) {
265
            Element e = t.asElement();
266
            if (e instanceof TypeElement) {
267
                TypeElement te = (TypeElement)e;
268
                return DEFAULT_VALUE.append((p ? te.getQualifiedName() : te.getSimpleName()).toString());
269
            }
270
            return DEFAULT_VALUE;
271
        }
272
    }
117
}
273
}
(-)a/java.source/test/unit/src/org/netbeans/api/java/source/TypeUtilitiesTest.java (-1 / +23 lines)
Lines 27-33 Link Here
27
 * Contributor(s):
27
 * Contributor(s):
28
 *
28
 *
29
 * The Original Software is NetBeans. The Initial Developer of the Original
29
 * The Original Software is NetBeans. The Initial Developer of the Original
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2010 Sun
31
 * Microsystems, Inc. All Rights Reserved.
31
 * Microsystems, Inc. All Rights Reserved.
32
 *
32
 *
33
 * If you wish your version of this file to be governed by only the CDDL
33
 * If you wish your version of this file to be governed by only the CDDL
Lines 44-49 Link Here
44
package org.netbeans.api.java.source;
44
package org.netbeans.api.java.source;
45
45
46
import java.io.File;
46
import java.io.File;
47
import java.io.IOException;
47
import java.net.URL;
48
import java.net.URL;
48
import java.util.Collections;
49
import java.util.Collections;
49
import javax.lang.model.element.TypeElement;
50
import javax.lang.model.element.TypeElement;
Lines 53-58 Link Here
53
import org.netbeans.junit.NbTestCase;
54
import org.netbeans.junit.NbTestCase;
54
import org.netbeans.modules.java.source.usages.IndexUtil;
55
import org.netbeans.modules.java.source.usages.IndexUtil;
55
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
56
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
57
import org.openide.filesystems.FileObject;
58
import org.openide.filesystems.FileUtil;
56
59
57
/**
60
/**
58
 *
61
 *
Lines 143-146 Link Here
143
        
146
        
144
    }
147
    }
145
    
148
    
149
    public void testTypeName() throws Exception {
150
        FileObject root = FileUtil.createMemoryFileSystem().getRoot();
151
        FileObject src  = root.createData("Test.java");
152
        TestUtilities.copyStringToFile(src, "package test; public class Test {}");
153
        JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL[0])), ClassPathSupport.createClassPath(new URL[0]), ClassPathSupport.createClassPath(new URL[0])), src);
154
        
155
        js.runUserActionTask(new Task<CompilationController>() {
156
            public void run(CompilationController info) throws IOException  {
157
                info.toPhase(JavaSource.Phase.RESOLVED);
158
                TypeElement context = info.getTopLevelElements().get(0);
159
                assertEquals("java.util.List<java.lang.String>[]", info.getTypeUtilities().getTypeName(info.getTreeUtilities().parseType("java.util.List<java.lang.String>[]", context), TypeUtilities.TypeNameOptions.PRINT_FQN));
160
                assertEquals("List<String>[]", info.getTypeUtilities().getTypeName(info.getTreeUtilities().parseType("java.util.List<java.lang.String>[]", context)));
161
                assertEquals("java.util.List<java.lang.String>...", info.getTypeUtilities().getTypeName(info.getTreeUtilities().parseType("java.util.List<java.lang.String>[]", context), TypeUtilities.TypeNameOptions.PRINT_FQN, TypeUtilities.TypeNameOptions.PRINT_AS_VARARG));
162
                assertEquals("List<String>...", info.getTypeUtilities().getTypeName(info.getTreeUtilities().parseType("java.util.List<java.lang.String>[]", context), TypeUtilities.TypeNameOptions.PRINT_AS_VARARG));
163
            }
164
        }, true);
165
        
166
    }
167
    
146
}
168
}

Return to bug 104194