Java Code Examples for org.netbeans.api.java.source.CompilationInfo#putCachedValue()

The following examples show how to use org.netbeans.api.java.source.CompilationInfo#putCachedValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImportClass.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static UseFQN createShared(CompilationInfo info, 
        FileObject file, 
        String fqn, 
        ElementHandle<Element> toImport, 
        String sortText, TreePath replacePath, boolean isValid) {
    
    String k = UseFQN.class.getName() + "#" + fqn; // NOI18N
    Object o = info.getCachedValue(k);
    UseFQN inst;
    if (o instanceof UseFQN) {
        inst = (UseFQN)o;
        inst.addTreePath(TreePathHandle.create(replacePath, info));
    } else {
        inst = new UseFQN(info, file, fqn, toImport, sortText, replacePath, isValid, true);
        info.putCachedValue(k, inst, CompilationInfo.CacheClearPolicy.ON_TASK_END);
    }
    return inst;
}
 
Example 2
Source File: BreadCrumbsScanningTask.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void run(Result result, SchedulerEvent event) {
    cancel.set(false);

    CompilationInfo info = CompilationInfo.get(result);
    if (info == null) {
        return;
    }

    Document doc = info.getSnapshot().getSource().getDocument(false);
    if (doc == null) {
        return;
    }

    if (!BreadcrumbsController.areBreadCrumsEnabled(doc)) return ;
    
    int caretPosition = event instanceof CursorMovedSchedulerEvent
            ? ((CursorMovedSchedulerEvent) event).getCaretOffset()
            : CaretAwareJavaSourceTaskFactory.getLastPosition(result.getSnapshot().getSource().getFileObject()); //XXX

    if (cancel.get()) {
        return;
    }

    BreadcrumbsElement[] rootAndSelection = rootAndSelection(info, caretPosition, cancel);
    
    if (cancel.get() || rootAndSelection == null) {
        return ;
    }

    BreadcrumbsController.setBreadcrumbs(doc, rootAndSelection[1]);
    
    info.putCachedValue(BreadCrumbsScanningTask.class, rootAndSelection[0], CacheClearPolicy.ON_CHANGE);
}
 
Example 3
Source File: ArithmeticUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Map<Object, ElementValue> getValueCache(CompilationInfo info) {
    Map<Object, ElementValue> cache = (Map)info.getCachedValue(CONST_EVAL_KEY);
    if (cache == null) {
        cache = new HashMap<Object, ElementValue>(7);
        info.putCachedValue(CONST_EVAL_KEY, cache, CompilationInfo.CacheClearPolicy.ON_TASK_END);
    }
    return cache;
}
 
Example 4
Source File: SuspiciousNamesCombination.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Map<String, Integer> ensureNameMapLoaded(CompilationInfo info) {
    Map<String, Integer> mapNameToGroup = (Map<String, Integer>)info.getCachedValue(KEY);
    if (mapNameToGroup != null) {
        return mapNameToGroup;
    }
    mapNameToGroup = new HashMap<String, Integer>();
    Preferences prefs = getPreferences(null);
    String value = prefs.get(GROUP_KEY, DEFAULT_GROUPS);
    if (value == null) {
        info.putCachedValue(KEY, mapNameToGroup, CompilationInfo.CacheClearPolicy.ON_TASK_END);
        return mapNameToGroup;
    }
    String[] groups = value.split(Pattern.quote(GROUP_SEPARATOR));
    int idx = 0;
    for (String g : groups) {
        String[] names = g.split(SEPARATORS_REGEX);
        for (String n : names) {
            if (n.isEmpty()) {
                continue;
            }
            mapNameToGroup.put(n.toLowerCase(), idx);
        }
        idx++;
    }
    info.putCachedValue(KEY, mapNameToGroup, CompilationInfo.CacheClearPolicy.ON_TASK_END);
    return mapNameToGroup;
}
 
Example 5
Source File: Unbalanced.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void record(CompilationInfo info, VariableElement el, State... states) {
    Map<Element, Set<State>> cache = (Map<Element, Set<State>>)info.getCachedValue(SEEN_KEY);

    if (cache == null) {
        info.putCachedValue(SEEN_KEY, cache = new HashMap<>(), CompilationInfo.CacheClearPolicy.ON_CHANGE);
    }

    Set<State> state = cache.get(el);

    if (state == null) {
        cache.put(el, state = EnumSet.noneOf(State.class));
    }
    
    state.addAll(Arrays.asList(states));
}
 
Example 6
Source File: NPECheck.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Map<Tree, State> computeExpressionsState(CompilationInfo info, HintContext ctx) {
    Map<Tree, State> result = (Map<Tree, State>) info.getCachedValue(KEY_EXPRESSION_STATE);
    
    if (result != null) {
        return result;
    }
    
    VisitorImpl v = new VisitorImpl(ctx, info, null);
    
    v.scan(info.getCompilationUnit(), null);

    result = v.expressionState;
    info.putCachedValue(KEY_EXPRESSION_STATE, result, CompilationInfo.CacheClearPolicy.ON_TASK_END);
    return result;
}
 
Example 7
Source File: Flow.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static FlowResult assignmentsForUse(CompilationInfo info, Cancel cancel) {
    FlowResult result = (FlowResult) info.getCachedValue(KEY_FLOW);
    
    if (result == null) {
        result = assignmentsForUse(info, new TreePath(info.getCompilationUnit()), cancel);
        
        if (result != null && !cancel.isCanceled()) {
            info.putCachedValue(KEY_FLOW, result, CacheClearPolicy.ON_TASK_END);
        }
    }
    
    return result;
}
 
Example 8
Source File: HideField.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected static synchronized Iterable<? extends Element> getAllMembers(CompilationInfo info, TypeElement clazz) {
    Map<TypeElement, Iterable<? extends Element>> map = (Map<TypeElement, Iterable<? extends Element>>) info.getCachedValue(KEY_MEMBERS_CACHE);

    if (map == null) {
        info.putCachedValue(KEY_MEMBERS_CACHE, map = new HashMap<TypeElement, Iterable<? extends Element>>(), CacheClearPolicy.ON_SIGNATURE_CHANGE);
    }

    Iterable<? extends Element> members = map.get(clazz);

    if (members == null) {
        map.put(clazz, members = info.getElements().getAllMembers(clazz));
    }

    return members;
}
 
Example 9
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static Scope constructScope(CompilationInfo info, Map<String, TypeMirror> constraints, Iterable<? extends String> auxiliaryImports) {
        ScopeDescription desc = new ScopeDescription(constraints, auxiliaryImports);
        Scope result = (Scope) info.getCachedValue(desc);

        if (result != null) return result;
        
        StringBuilder clazz = new StringBuilder();

        clazz.append("package $$;");

        for (String i : auxiliaryImports) {
            clazz.append(i);
        }

        long count = inc++;

        String classname = "$$scopeclass$constraints$" + count;

        clazz.append("public class " + classname + "{");

        for (Entry<String, TypeMirror> e : constraints.entrySet()) {
            if (e.getValue() != null) {
                clazz.append("private ");
                clazz.append(e.getValue().toString()); //XXX
                clazz.append(" ");
                clazz.append(e.getKey());
                clazz.append(";\n");
            }
        }

        clazz.append("private void test() {\n");
        clazz.append("}\n");
        clazz.append("}\n");

        JavacTaskImpl jti = JavaSourceAccessor.getINSTANCE().getJavacTask(info);
        Context context = jti.getContext();
        JavaCompiler compiler = JavaCompiler.instance(context);
        Modules modules = Modules.instance(context);
        Log log = Log.instance(context);
        NBResolve resolve = NBResolve.instance(context);
        Annotate annotate = Annotate.instance(context);
        Names names = Names.instance(context);
        Symtab syms = Symtab.instance(context);
        Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(compiler.log);

        JavaFileObject jfo = FileObjects.memoryFileObject("$", "$", new File("/tmp/$$scopeclass$constraints$" + count + ".java").toURI(), System.currentTimeMillis(), clazz.toString());

        try {
            resolve.disableAccessibilityChecks();
            if (compiler.isEnterDone()) {
                annotate.blockAnnotations();
//                try {
//                    Field f = compiler.getClass().getDeclaredField("enterDone");
//                    f.setAccessible(true);
//                    f.set(compiler, false);
//                } catch (Throwable t) {
//                    Logger.getLogger(Utilities.class.getName()).log(Level.FINE, null, t);
//                }
                //was:
//                compiler.resetEnterDone();
            }
            
            JCCompilationUnit cut = compiler.parse(jfo);
            ClassSymbol enteredClass = syms.enterClass(modules.getDefaultModule(), names.fromString("$$." + classname));
            modules.enter(com.sun.tools.javac.util.List.of(cut), enteredClass);
            compiler.enterTrees(com.sun.tools.javac.util.List.of(cut));

            Todo todo = compiler.todo;
            ListBuffer<Env<AttrContext>> defer = new ListBuffer<Env<AttrContext>>();
            
            while (todo.peek() != null) {
                Env<AttrContext> env = todo.remove();

                if (env.toplevel == cut)
                    compiler.attribute(env);
                else
                    defer = defer.append(env);
            }

            todo.addAll(defer);

            Scope res = new ScannerImpl().scan(cut, info);

            info.putCachedValue(desc, res, CacheClearPolicy.ON_SIGNATURE_CHANGE);

            return res;
        } finally {
            resolve.restoreAccessbilityChecks();
            log.popDiagnosticHandler(discardHandler);
        }
    }
 
Example 10
Source File: UnusedImports.java    From netbeans with Apache License 2.0 3 votes vote down vote up
public static Collection<TreePath> process(CompilationInfo info, AtomicBoolean cancel) {
    Collection<TreePath> result = (Collection<TreePath>) info.getCachedValue(KEY_CACHE);
    
    if (result != null) return result;
    
    DetectorVisitor v = new DetectorVisitor(info, cancel);
    
    CompilationUnitTree cu = info.getCompilationUnit();
    
    v.scan(cu, null);
    
    if (cancel.get())
        return null;
    
    List<TreePath> allUnusedImports = new ArrayList<TreePath>();

    for (TreePath tree : v.getUnusedImports().values()) {
        if (cancel.get()) {
            return null;
        }

        allUnusedImports.add(tree);
    }
    
    allUnusedImports = Collections.unmodifiableList(allUnusedImports);
    
    info.putCachedValue(KEY_CACHE, allUnusedImports, CacheClearPolicy.ON_CHANGE);
    
    return allUnusedImports;
}