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

(-)a/java.source/apichanges.xml (+13 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="CompilationInfo-cache">
112
             <api name="general"/>
113
             <summary>Added <code>CompilationInfo.getCachedValue</code>, <code>CompilationInfo.putCachedValue</code> and <code>CompilationInfo.CacheClearPolicy</code>.</summary>
114
             <version major="0" minor="89"/>
115
             <date day="25" month="10" year="2011"/>
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>CompilationInfo.getCachedValue</code> and <code>CompilationInfo.putCachedValue</code> methods and
120
                 <code>CompilationInfo.CacheClearPolicy</code> enum so that tasks can easily store temporary data with predictable termination.
121
             </description>
122
             <issue number="999999"/>
123
        </change>
111
        <change id="OrganizeImports">
124
        <change id="OrganizeImports">
112
             <api name="general"/>
125
             <api name="general"/>
113
             <summary>Added <code>GeneratorUtilities.addImports</code> and several methods to <code>CodeStyle</code> to support organizing imports.</summary>
126
             <summary>Added <code>GeneratorUtilities.addImports</code> and several methods to <code>CodeStyle</code> to support organizing imports.</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.88.0
49
spec.version.base=0.89.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/CompilationInfo.java (+36 lines)
Lines 56-61 Link Here
56
import java.util.ArrayList;
56
import java.util.ArrayList;
57
import java.util.Collections;
57
import java.util.Collections;
58
import java.util.List;
58
import java.util.List;
59
import java.util.Map;
59
import javax.lang.model.SourceVersion;
60
import javax.lang.model.SourceVersion;
60
import javax.lang.model.element.Element;
61
import javax.lang.model.element.Element;
61
import javax.lang.model.element.TypeElement;
62
import javax.lang.model.element.TypeElement;
Lines 66-71 Link Here
66
import org.netbeans.api.annotations.common.CheckForNull;
67
import org.netbeans.api.annotations.common.CheckForNull;
67
import org.netbeans.api.annotations.common.CheckReturnValue;
68
import org.netbeans.api.annotations.common.CheckReturnValue;
68
import org.netbeans.api.annotations.common.NonNull;
69
import org.netbeans.api.annotations.common.NonNull;
70
import org.netbeans.api.annotations.common.NullAllowed;
69
import org.netbeans.api.annotations.common.NullUnknown;
71
import org.netbeans.api.annotations.common.NullUnknown;
70
import org.netbeans.api.lexer.TokenHierarchy;
72
import org.netbeans.api.lexer.TokenHierarchy;
71
import org.netbeans.modules.java.preprocessorbridge.spi.WrapperFactory;
73
import org.netbeans.modules.java.preprocessorbridge.spi.WrapperFactory;
Lines 412-423 Link Here
412
        return Source.toSourceVersion(Source.instance(impl.getJavacTask().getContext()));
414
        return Source.toSourceVersion(Source.instance(impl.getJavacTask().getContext()));
413
    }
415
    }
414
416
417
    /**Retrieve a value cached under the given key using the
418
     * {@link #putCachedValue(java.lang.Object, java.lang.Object, org.netbeans.api.java.source.CompilationInfo.CacheClearPolicy)} method.
419
     *
420
     * @param key for which the cached value should be retrieved
421
     * @return value originally passed to {@link #putCachedValue(java.lang.Object, java.lang.Object, org.netbeans.api.java.source.CompilationInfo.CacheClearPolicy)}, or null if none
422
     * @since 0.89
423
     */
424
    public @CheckForNull Object getCachedValue(@NonNull Object key) {
425
        Parameters.notNull("key", key);
426
        return impl.getCachedValue(key);
427
    }
428
429
    /**Put a value into a cache under the given key. The {@link #clearPolicy} parameter specifies the latest time the
430
     * references to the key and value should be cleared. The infrastructure is free to clear the references at any earlier time.
431
     * The clients should not depend on this cache for correctness, only to improve performance.
432
     *
433
     * @param key a unique key under which the value should be stored
434
     * @param value the value to store - any value previously set under the same key will be erased from the cache
435
     * @param clearPolicy the latest time when the mapping should be cleared from the cache
436
     * @since 0.89
437
     */
438
    public void putCachedValue(@NonNull Object key, @NullAllowed Object value, @NonNull CacheClearPolicy clearPolicy) {
439
        Parameters.notNull("key", key);
440
        Parameters.notNull("clearPolicy", clearPolicy);
441
        impl.putCachedValue(key, value, clearPolicy);
442
    }
443
415
    /**
444
    /**
416
     * Marks this {@link CompilationInfo} as invalid, may be used to
445
     * Marks this {@link CompilationInfo} as invalid, may be used to
417
     * verify confinement.
446
     * verify confinement.
418
     */
447
     */
419
    final void invalidate () {
448
    final void invalidate () {
420
        this.invalid = true;
449
        this.invalid = true;
450
        this.impl.taskFinished();
421
        doInvalidate();
451
        doInvalidate();
422
    }
452
    }
423
    
453
    
Lines 439-442 Link Here
439
            throw new IllegalStateException (String.format("Access to the shared %s outside a guarded run method.", this.getClass().getSimpleName()));
469
            throw new IllegalStateException (String.format("Access to the shared %s outside a guarded run method.", this.getClass().getSimpleName()));
440
        }
470
        }
441
    }
471
    }
472
473
    public enum CacheClearPolicy {
474
        ON_TASK_END,
475
        ON_CHANGE,
476
        ON_SIGNATURE_CHANGE;
477
    }
442
}
478
}
(-)a/java.source/src/org/netbeans/modules/java/source/parsing/CompilationInfoImpl.java (-1 / +38 lines)
Lines 51-56 Link Here
51
import java.util.ArrayList;
51
import java.util.ArrayList;
52
import java.util.Arrays;
52
import java.util.Arrays;
53
import java.util.Collection;
53
import java.util.Collection;
54
import java.util.EnumMap;
54
import java.util.HashMap;
55
import java.util.HashMap;
55
import java.util.Iterator;
56
import java.util.Iterator;
56
import java.util.List;
57
import java.util.List;
Lines 66-71 Link Here
66
import javax.tools.DiagnosticListener;
67
import javax.tools.DiagnosticListener;
67
import javax.tools.JavaFileObject;
68
import javax.tools.JavaFileObject;
68
import org.netbeans.api.java.source.ClasspathInfo;
69
import org.netbeans.api.java.source.ClasspathInfo;
70
import org.netbeans.api.java.source.CompilationInfo.CacheClearPolicy;
69
import org.netbeans.api.java.source.JavaSource;
71
import org.netbeans.api.java.source.JavaSource;
70
import org.netbeans.api.lexer.TokenHierarchy;
72
import org.netbeans.api.lexer.TokenHierarchy;
71
import org.netbeans.modules.java.source.JavaFileFilterQuery;
73
import org.netbeans.modules.java.source.JavaFileFilterQuery;
Lines 89-95 Link Here
89
    private JavacTaskImpl javacTask;
91
    private JavacTaskImpl javacTask;
90
    private DiagnosticListener<JavaFileObject> diagnosticListener;
92
    private DiagnosticListener<JavaFileObject> diagnosticListener;
91
    private final ClasspathInfo cpInfo;
93
    private final ClasspathInfo cpInfo;
92
    Pair<DocPositionRegion,MethodTree> changedMethod;
94
    private Pair<DocPositionRegion,MethodTree> changedMethod;
93
    private final FileObject file;
95
    private final FileObject file;
94
    private final FileObject root;
96
    private final FileObject root;
95
    final JavaFileObject jfo;
97
    final JavaFileObject jfo;
Lines 99-104 Link Here
99
    private final boolean isClassFile;
101
    private final boolean isClassFile;
100
    private final boolean isDetached;
102
    private final boolean isDetached;
101
    JavaSource.Phase parserCrashed = JavaSource.Phase.UP_TO_DATE;      //When javac throws an error, the moveToPhase sets this to the last safe phase
103
    JavaSource.Phase parserCrashed = JavaSource.Phase.UP_TO_DATE;      //When javac throws an error, the moveToPhase sets this to the last safe phase
104
    private final Map<CacheClearPolicy, Map<Object, Object>> userCache = new EnumMap<CacheClearPolicy, Map<Object, Object>>(CacheClearPolicy.class);
102
105
103
    /**
106
    /**
104
     * Creates a new CompilationInfoImpl for given source file
107
     * Creates a new CompilationInfoImpl for given source file
Lines 387-392 Link Here
387
	return javacTask;
390
	return javacTask;
388
    }
391
    }
389
392
393
    public Object getCachedValue(Object key) {
394
        for (Map<Object, Object> c : userCache.values()) {
395
            Object res = c.get(key);
396
397
            if (res != null) return res;
398
        }
399
400
        return null;
401
    }
402
403
    public void putCachedValue(Object key, Object value, CacheClearPolicy clearPolicy) {
404
        for (Map<Object, Object> c : userCache.values()) {
405
            c.remove(key);
406
        }
407
408
        Map<Object, Object> c = userCache.get(clearPolicy);
409
410
        if (c == null) {
411
            userCache.put(clearPolicy, c = new HashMap<Object, Object>());
412
        }
413
414
        c.put(key, value);
415
    }
416
417
    public void taskFinished() {
418
        userCache.remove(CacheClearPolicy.ON_TASK_END);
419
    }
420
421
    public void invalidate() {
422
        userCache.clear();
423
    }
424
    
390
    /**
425
    /**
391
     * Returns current {@link DiagnosticListener}
426
     * Returns current {@link DiagnosticListener}
392
     * @return listener
427
     * @return listener
Lines 410-415 Link Here
410
     */
445
     */
411
    void setChangedMethod (final Pair<DocPositionRegion,MethodTree> changedMethod) {
446
    void setChangedMethod (final Pair<DocPositionRegion,MethodTree> changedMethod) {
412
        this.changedMethod = changedMethod;
447
        this.changedMethod = changedMethod;
448
        userCache.remove(CacheClearPolicy.ON_TASK_END);
449
        userCache.remove(CacheClearPolicy.ON_CHANGE);
413
    }
450
    }
414
    
451
    
415
    /**
452
    /**
(-)a/java.source/src/org/netbeans/modules/java/source/parsing/JavacParser.java (+7 lines)
Lines 338-343 Link Here
338
        LOGGER.log(Level.FINE, "parse: task: {0}\n{1}", new Object[]{   //NOI18N
338
        LOGGER.log(Level.FINE, "parse: task: {0}\n{1}", new Object[]{   //NOI18N
339
            task.toString(),
339
            task.toString(),
340
            snapshot == null ? "null" : snapshot.getText()});      //NOI18N
340
            snapshot == null ? "null" : snapshot.getText()});      //NOI18N
341
        CompilationInfoImpl oldInfo = ciImpl;
341
        switch (this.sourceCount) {
342
        switch (this.sourceCount) {
342
            case 0:
343
            case 0:
343
                ClasspathInfo _tmpInfo = null;
344
                ClasspathInfo _tmpInfo = null;
Lines 358-363 Link Here
358
                    if (_changedMethod != null && ciImpl != null) {
359
                    if (_changedMethod != null && ciImpl != null) {
359
                        LOGGER.log(Level.FINE, "\t:trying partial reparse:\n{0}", _changedMethod.first.getText());                           //NOI18N
360
                        LOGGER.log(Level.FINE, "\t:trying partial reparse:\n{0}", _changedMethod.first.getText());                           //NOI18N
360
                        needsFullReparse = !reparseMethod(ciImpl, snapshot, _changedMethod.second, _changedMethod.first.getText());
361
                        needsFullReparse = !reparseMethod(ciImpl, snapshot, _changedMethod.second, _changedMethod.first.getText());
362
                        if (!needsFullReparse) {
363
                            ciImpl.setChangedMethod(_changedMethod);
364
                        }
361
                    }
365
                    }
362
                }
366
                }
363
                if (needsFullReparse) {
367
                if (needsFullReparse) {
Lines 372-377 Link Here
372
                    ciImpl == null ? null : ciImpl.getJavacTask(),
376
                    ciImpl == null ? null : ciImpl.getJavacTask(),
373
                    ciImpl == null ? null : ciImpl.getDiagnosticListener());
377
                    ciImpl == null ? null : ciImpl.getDiagnosticListener());
374
        }
378
        }
379
        if (oldInfo != ciImpl && oldInfo != null) {
380
            oldInfo.invalidate();
381
        }
375
        cachedSnapShot = snapshot;
382
        cachedSnapShot = snapshot;
376
    }
383
    }
377
384
(-)a/java.source/test/unit/src/org/netbeans/api/java/source/CompilationInfoTest.java (-1 / +71 lines)
Lines 45-56 Link Here
45
import java.io.FileWriter;
45
import java.io.FileWriter;
46
import java.io.IOException;
46
import java.io.IOException;
47
import java.io.PrintWriter;
47
import java.io.PrintWriter;
48
import java.lang.ref.Reference;
49
import java.lang.ref.WeakReference;
48
import java.text.MessageFormat;
50
import java.text.MessageFormat;
49
import javax.lang.model.SourceVersion;
51
import javax.lang.model.SourceVersion;
52
import javax.swing.text.Document;
50
import org.junit.Test;
53
import org.junit.Test;
54
import org.netbeans.api.java.lexer.JavaTokenId;
55
import org.netbeans.api.java.source.CompilationInfo.CacheClearPolicy;
56
import org.netbeans.api.java.source.JavaSource.Phase;
57
import org.netbeans.api.lexer.Language;
58
import org.netbeans.api.lexer.TokenHierarchy;
51
import org.netbeans.junit.NbTestCase;
59
import org.netbeans.junit.NbTestCase;
60
import org.openide.cookies.EditorCookie;
52
import org.openide.filesystems.FileObject;
61
import org.openide.filesystems.FileObject;
53
import org.openide.filesystems.FileUtil;
62
import org.openide.filesystems.FileUtil;
63
import org.openide.loaders.DataObject;
54
64
55
/**
65
/**
56
 *
66
 *
Lines 67-73 Link Here
67
77
68
    @Override
78
    @Override
69
    public void setUp() throws Exception {
79
    public void setUp() throws Exception {
70
        SourceUtilsTestUtil.prepareTest(new String[0], new Object[0]);
80
        SourceUtilsTestUtil.prepareTest(new String[] {"META-INF/generated-layer.xml"}, new Object[0]);
71
    }
81
    }
72
82
73
    /**
83
    /**
Lines 105-108 Link Here
105
            return null;
115
            return null;
106
        }
116
        }
107
    }
117
    }
118
119
    public void testCacheEviction() throws Exception {
120
        clearWorkDir();
121
122
        FileObject source = FileUtil.createData(new File(getWorkDir(), "Test.java"));
123
        DataObject sourceDO = DataObject.find(source);
124
        EditorCookie ec = sourceDO.getLookup().lookup(EditorCookie.class);
125
126
        assertNotNull(ec);
127
128
        Document doc = ec.openDocument();
129
130
        doc.putProperty(Language.class, JavaTokenId.language());
131
132
        TokenHierarchy.get(doc).tokenSequence().tokenCount();
133
134
        TestUtilities.copyStringToFile(source, "public class Test {\n void test() {\n  //whatever\n }\n}\n");
135
136
        JavaSource js = JavaSource.forDocument(doc);
137
        
138
        js.runUserActionTask(new Task<CompilationController>() {
139
            public void run(CompilationController parameter) throws Exception {
140
                parameter.toPhase(Phase.RESOLVED);
141
                parameter.putCachedValue("1", 1, CacheClearPolicy.ON_TASK_END);
142
                parameter.putCachedValue("2", 2, CacheClearPolicy.ON_CHANGE);
143
                parameter.putCachedValue("3", 3, CacheClearPolicy.ON_SIGNATURE_CHANGE);
144
            }
145
        }, true);
146
147
        js.runUserActionTask(new Task<CompilationController>() {
148
            public void run(CompilationController parameter) throws Exception {
149
                parameter.toPhase(Phase.RESOLVED);
150
                assertNull(parameter.getCachedValue("1"));
151
                assertEquals(2, parameter.getCachedValue("2"));
152
                assertEquals(3, parameter.getCachedValue("3"));
153
            }
154
        }, true);
155
156
        doc.insertString(41, "a", null);
157
158
        js.runUserActionTask(new Task<CompilationController>() {
159
            public void run(CompilationController parameter) throws Exception {
160
                parameter.toPhase(Phase.RESOLVED);
161
                assertNull(parameter.getCachedValue("1"));
162
                assertNull(parameter.getCachedValue("2"));
163
                assertEquals(3, parameter.getCachedValue("3"));
164
            }
165
        }, true);
166
167
        doc.insertString(20, "void t2() {}", null);
168
169
        js.runUserActionTask(new Task<CompilationController>() {
170
            public void run(CompilationController parameter) throws Exception {
171
                parameter.toPhase(Phase.RESOLVED);
172
                assertNull(parameter.getCachedValue("1"));
173
                assertNull(parameter.getCachedValue("2"));
174
                assertNull(parameter.getCachedValue("3"));
175
            }
176
        }, true);
177
    }
108
}
178
}

Return to bug 203890