alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore.parser;

import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.netbeans.jmi.javamodel.Element;
import org.netbeans.jmi.javamodel.Import;
import org.netbeans.lib.java.parser.ASTContext;
import org.netbeans.lib.java.parser.ASTree;
import org.netbeans.lib.java.parser.ASTreeTypes;
import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl;
import org.netbeans.modules.javacore.jmiimpl.javamodel.SemiPersistentElement;

/**
 *
 * @author  Martin Matula
 */
public class ElementInfo {
    public static final int SINGLE_IMPORT_TYPE = ASTreeTypes.SINGLE_TYPE_IMPORT;
    public static final int IMPORT_ON_DEMAND_TYPE = ASTreeTypes.TYPE_IMPORT_ON_DEMAND;

    static final TypeRef[] EMPTY_TYPEREFS = new TypeRef[0];
    static final NameRef[] EMPTY_NAMEREFS = new NameRef[0];
    static final TypeParamRef[] EMPTY_TPREFS = new TypeParamRef[0];

    static final ClassInfo[] EMPTY_FEATURES = new ClassInfo[0];
    static final ParameterInfo[] EMPTY_PARAMETERS = new ParameterInfo[0];
    static final TypeParamInfo[] EMPTY_TYPE_PARAMS = new TypeParamInfo[0];
    static final AnnotationInfo[] EMPTY_ANNOTATIONS = new AnnotationInfo[0];
    static final AnnotationValueInfo[] EMPTY_ANNOTATION_VALUES = new AnnotationValueInfo[0];
    
    protected Reference tree;
    protected final ResourceImpl resource;
    private final int firstToken, lastToken;
    private final int astType;

    public final int infoType;
    public final String name;

    public static Reference createASTReference(ASTree tree) {
        return new CachedReference(tree);
    }

    public ElementInfo(ASTree tree, int infoType, String name) {
        if (tree == null) {
            this.tree = null;
            this.astType = this.firstToken = this.lastToken = 0;
            this.resource = null;
        } else {
            this.resource = (ResourceImpl) ((MDRParser) tree.getASTContext()).getResource();
            this.tree = createASTReference(tree);
            this.firstToken = tree.getFirstToken();
            this.lastToken = tree.getLastToken();
            this.astType = tree.getType();
        }
        this.infoType = infoType;
        this.name = name;
    }
    
    public void hardRefAST() {
        resource.getElementInfo().hardRefAST();
    }

    public final Reference getASTree() {
        return tree;
    }

    public ASTree refreshASTree() {
        ASTree parentTree = resource.getASTree();
        ASTProvider parser = (ASTProvider) parentTree.getASTContext();
//        System.err.println(this.getClass().getName() + "." + name + ": refreshing AST for: " + parser);
        ASTree result = parser.findTree(parentTree, firstToken, lastToken, astType);
        this.tree = createASTReference(result);
        return result;
    }

    public ASTree getTypeAST(SemiPersistentElement owner) {
        ASTree tree = owner.getASTree();
        if (tree != null) {
            int type=tree.getType();
            ASTree parts[]=tree.getSubTrees();
            
            if (type==ASTreeTypes.SINGLE_TYPE_IMPORT || type==ASTreeTypes.TYPE_IMPORT_ON_DEMAND)
                return parts[1];
            return parts[0];
        }
        return null;
    }
    
    static class CachedReference extends WeakReference {
	private static final int CACHE_SIZE = Integer.getInteger("org.netbeans.javacore.ASTCache.size", new Integer(16)).intValue();
        private static final LinkedHashMap CACHE = 
	    // Use smaller loadFactor to speed access, size/loadFactor 
	    // capacity to avoid resizing.
	    new LinkedHashMap(CACHE_SIZE * 2, 0.5f, true) {
		protected boolean removeEldestEntry(Map.Entry eldest) {
		    return size() > CACHE_SIZE;
		}
	    };
        
        CachedReference(ASTree tree) {
            super(tree);
            ASTContext parser = tree.getASTContext();
            synchronized (CACHE) {
                CACHE.put(parser, parser);
            }
        }
        
        public Object get() {
            ASTree result = (ASTree) super.get();
            if (result != null) {
                ASTContext parser = result.getASTContext();
                synchronized (CACHE) {
                    if (CACHE.get(parser) == null) {
                        CACHE.put(parser, parser);
                    }
                }
            }
            return result;
        }
        
        static void dumpCache() {
            synchronized (CACHE) {
		System.err.println("******** Cache dump:");
		for (Iterator i = CACHE.keySet().iterator(); i.hasNext();)
		    System.err.println(i.next().toString());
		System.err.println("*********");
            }
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.