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

(-)a/java.source/src/org/netbeans/modules/java/source/parsing/JavacParser.java (-12 / +21 lines)
Lines 871-880 Link Here
871
        return task;
871
        return task;
872
    }
872
    }
873
873
874
    private static @NonNull com.sun.tools.javac.code.Source validateSourceLevel(
874
    static @NonNull com.sun.tools.javac.code.Source validateSourceLevel(
875
            @NullAllowed String sourceLevel,
875
            @NullAllowed String sourceLevel,
876
            @NonNull final ClasspathInfo cpInfo) {
876
            @NonNull final ClasspathInfo cpInfo) {
877
        ClassPath bootClassPath = cpInfo.getClassPath(PathKind.BOOT);
877
        ClassPath bootClassPath = cpInfo.getClassPath(PathKind.BOOT);
878
        ClassPath classPath = null;
878
        ClassPath srcClassPath = cpInfo.getClassPath(PathKind.SOURCE);
879
        ClassPath srcClassPath = cpInfo.getClassPath(PathKind.SOURCE);
879
        com.sun.tools.javac.code.Source[] sources = com.sun.tools.javac.code.Source.values();
880
        com.sun.tools.javac.code.Source[] sources = com.sun.tools.javac.code.Source.values();
880
        Level warnLevel;
881
        Level warnLevel;
Lines 889-911 Link Here
889
            if (source.name.equals(sourceLevel)) {
890
            if (source.name.equals(sourceLevel)) {
890
                if (source.compareTo(com.sun.tools.javac.code.Source.JDK1_4) >= 0) {
891
                if (source.compareTo(com.sun.tools.javac.code.Source.JDK1_4) >= 0) {
891
                    if (bootClassPath != null && bootClassPath.findResource("java/lang/AssertionError.class") == null) { //NOI18N
892
                    if (bootClassPath != null && bootClassPath.findResource("java/lang/AssertionError.class") == null) { //NOI18N
892
                        if (srcClassPath != null && srcClassPath.findResource("java/lang/AssertionError.java") == null) {
893
                        if (bootClassPath.findResource("java/lang/Object.class") == null) { // NOI18N
893
                            LOGGER.log(warnLevel,
894
                            // empty bootClassPath also check classpath
894
                                       "Even though the source level of {0} is set to: {1}, java.lang.AssertionError cannot be found on the bootclasspath: {2}\n" +
895
                            classPath = cpInfo.getClassPath(PathKind.COMPILE);
895
                                       "Changing source level to 1.3",
896
                        }
896
                                       new Object[]{cpInfo.getClassPath(PathKind.SOURCE), sourceLevel, bootClassPath}); //NOI18N
897
                        if (srcClassPath != null && srcClassPath.findResource("java/lang/AssertionError.java") == null) { // NOI18N
897
                            return com.sun.tools.javac.code.Source.JDK1_3;
898
                            if (classPath == null || classPath.findResource("java/lang/AssertionError.class") == null) { // NOI18N
899
                                LOGGER.log(warnLevel,
900
                                           "Even though the source level of {0} is set to: {1}, java.lang.AssertionError cannot be found on the bootclasspath: {2}\n" +
901
                                           "Changing source level to 1.3",
902
                                           new Object[]{cpInfo.getClassPath(PathKind.SOURCE), sourceLevel, bootClassPath}); //NOI18N
903
                                return com.sun.tools.javac.code.Source.JDK1_3;
904
                            }
898
                        }
905
                        }
899
                    }
906
                    }
900
                }
907
                }
901
                if (source.compareTo(com.sun.tools.javac.code.Source.JDK1_5) >= 0) {
908
                if (source.compareTo(com.sun.tools.javac.code.Source.JDK1_5) >= 0) {
902
                    if (bootClassPath != null && bootClassPath.findResource("java/lang/StringBuilder.class") == null) { //NOI18N
909
                    if (bootClassPath != null && bootClassPath.findResource("java/lang/StringBuilder.class") == null) { //NOI18N
903
                        if (srcClassPath != null && srcClassPath.findResource("java/lang/StringBuilder.java")==null) {
910
                        if (srcClassPath != null && srcClassPath.findResource("java/lang/StringBuilder.java")==null) {
904
                            LOGGER.log(warnLevel,
911
                            if (classPath == null || classPath.findResource("java/lang/StringBuilder.class") == null) { // NOI18N
905
                                       "Even though the source level of {0} is set to: {1}, java.lang.StringBuilder cannot be found on the bootclasspath: {2}\n" +
912
                                LOGGER.log(warnLevel,
906
                                       "Changing source level to 1.4",
913
                                           "Even though the source level of {0} is set to: {1}, java.lang.StringBuilder cannot be found on the bootclasspath: {2}\n" +
907
                                       new Object[]{cpInfo.getClassPath(PathKind.SOURCE), sourceLevel, bootClassPath}); //NOI18N
914
                                           "Changing source level to 1.4",
908
                            return com.sun.tools.javac.code.Source.JDK1_4;
915
                                           new Object[]{cpInfo.getClassPath(PathKind.SOURCE), sourceLevel, bootClassPath}); //NOI18N
916
                                return com.sun.tools.javac.code.Source.JDK1_4;
917
                            }
909
                        }
918
                        }
910
                    }
919
                    }
911
                }
920
                }
(-)a/java.source/test/unit/src/org/netbeans/modules/java/source/parsing/JavacParserTest.java (+73 lines)
Lines 46-52 Link Here
46
import com.sun.source.tree.Tree;
46
import com.sun.source.tree.Tree;
47
import com.sun.source.util.TreePath;
47
import com.sun.source.util.TreePath;
48
import com.sun.source.util.TreePathScanner;
48
import com.sun.source.util.TreePathScanner;
49
import com.sun.tools.javac.code.Source;
49
import java.io.File;
50
import java.io.File;
51
import java.io.FileOutputStream;
52
import java.io.IOException;
53
import java.io.InputStream;
54
import java.net.URL;
50
import java.util.Arrays;
55
import java.util.Arrays;
51
import java.util.HashSet;
56
import java.util.HashSet;
52
import java.util.Set;
57
import java.util.Set;
Lines 54-59 Link Here
54
import javax.lang.model.element.TypeElement;
59
import javax.lang.model.element.TypeElement;
55
import javax.swing.text.Document;
60
import javax.swing.text.Document;
56
import javax.tools.Diagnostic;
61
import javax.tools.Diagnostic;
62
import static junit.framework.Assert.assertEquals;
63
import org.netbeans.api.java.classpath.ClassPath;
57
import org.netbeans.api.java.lexer.JavaTokenId;
64
import org.netbeans.api.java.lexer.JavaTokenId;
58
import org.netbeans.api.java.source.ClasspathInfo;
65
import org.netbeans.api.java.source.ClasspathInfo;
59
import org.netbeans.api.java.source.CompilationController;
66
import org.netbeans.api.java.source.CompilationController;
Lines 67-72 Link Here
67
import org.netbeans.api.lexer.TokenSequence;
74
import org.netbeans.api.lexer.TokenSequence;
68
import org.netbeans.junit.NbTestCase;
75
import org.netbeans.junit.NbTestCase;
69
import org.netbeans.modules.java.source.tasklist.CompilerSettings;
76
import org.netbeans.modules.java.source.tasklist.CompilerSettings;
77
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
70
import org.openide.cookies.EditorCookie;
78
import org.openide.cookies.EditorCookie;
71
import org.openide.filesystems.FileObject;
79
import org.openide.filesystems.FileObject;
72
import org.openide.filesystems.FileUtil;
80
import org.openide.filesystems.FileUtil;
Lines 284-289 Link Here
284
        }, true);
292
        }, true);
285
    }
293
    }
286
    
294
    
295
    public void testIfMissingObjectOnBootCPUseCPToGuessSourceLevel() throws Exception {
296
        Source ret = guessSourceLevel(false, false);
297
        assertEquals("Downgraded to 1.4", Source.JDK1_4, ret);
298
    }
299
300
    public void testIfObjectPresentOnBootDontUseCPToGuessSourceLevel() throws Exception {
301
        Source ret = guessSourceLevel(true, false);
302
        assertEquals("Downgraded to 1.4, as Object on bootCP, but no AssertError", Source.JDK1_3, ret);
303
    }
304
305
    public void testIfMissingObjectOnBootCPUseCPToGuessSourceLevelWithStringBuilder() throws Exception {
306
        Source ret = guessSourceLevel(false, true);
307
        assertEquals("Kept to 1.7", Source.JDK1_7, ret);
308
    }
309
    
310
    private Source guessSourceLevel(boolean objectOnBCP, boolean sbOnCP) throws Exception {
311
        clearWorkDir();
312
        File bcp = new File(getWorkDir(), "bootcp");
313
        bcp.mkdirs();
314
        
315
        File cp = new File(getWorkDir(), "cp");
316
        cp.mkdirs();
317
        
318
        File src = new File(getWorkDir(), "src");
319
        src.mkdirs();
320
321
        if (objectOnBCP) {
322
            copyResource(
323
                JavacParserTest.class.getResource("/java/lang/Object.class"),
324
                new File(new File(new File(bcp, "java"), "lang"), "Object.class")
325
            );
326
        }
327
328
        copyResource(
329
            JavacParserTest.class.getResource("/java/lang/AssertionError.class"),
330
            new File(new File(new File(cp, "java"), "lang"), "AssertionError.class")
331
        );
332
        
333
        if (sbOnCP) {
334
            copyResource(
335
                JavacParserTest.class.getResource("/java/lang/StringBuilder.class"),
336
                new File(new File(new File(cp, "java"), "lang"), "StringBuilder.class")
337
            );
338
        }
339
        
340
        ClasspathInfo info = ClasspathInfo.create(
341
            ClassPathSupport.createClassPath(bcp.toURI().toURL()), 
342
            ClassPathSupport.createClassPath(cp.toURI().toURL()), 
343
            ClassPathSupport.createClassPath(src.toURI().toURL())
344
        );
345
        
346
        return JavacParser.validateSourceLevel("1.7", info);
347
    }
348
    
287
    private FileObject createFile(String path, String content) throws Exception {
349
    private FileObject createFile(String path, String content) throws Exception {
288
        FileObject file = FileUtil.createData(sourceRoot, path);
350
        FileObject file = FileUtil.createData(sourceRoot, path);
289
        TestUtilities.copyStringToFile(file, content);
351
        TestUtilities.copyStringToFile(file, content);
Lines 305-308 Link Here
305
        SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);
367
        SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);
306
    }
368
    }
307
369
370
    private static void copyResource(URL resource, File file) throws IOException {
371
        assertNotNull("Resource found", resource);
372
        file.getParentFile().mkdirs();
373
        assertTrue("New file " + file + " created", file.createNewFile());
374
        FileOutputStream os = new FileOutputStream(file);
375
        InputStream is = resource.openStream();
376
        FileUtil.copy(is, os);
377
        is.close();
378
        os.close();
379
    }
380
308
}
381
}

Return to bug 225261