Example usage for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding getPackage

List of usage examples for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding getPackage

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding getPackage.

Prototype

@Override
    public PackageBinding getPackage() 

Source Link

Usage

From source file:com.redhat.ceylon.eclipse.core.model.JDTModelLoader.java

License:Open Source License

public static boolean doWithReferenceBinding(final IType typeModel, final ReferenceBinding binding,
        final ActionOnClassBinding action) {
    if (typeModel == null) {
        throw new ModelResolutionException("Resolving action requested on a missing declaration");
    }// w  ww . j  a  v  a2s.c o  m

    if (binding == null) {
        return false;
    }

    PackageBinding packageBinding = binding.getPackage();
    if (packageBinding == null) {
        return false;
    }
    LookupEnvironment lookupEnvironment = packageBinding.environment;
    if (lookupEnvironment == null) {
        return false;
    }
    JDTModelLoader modelLoader = getModelLoader(typeModel);
    if (modelLoader == null) {
        throw new ModelResolutionException(
                "The Model Loader corresponding the type '" + typeModel.getFullyQualifiedName() + "'");
    }

    synchronized (modelLoader.lookupEnvironmentMutex) {
        if (modelLoader.lookupEnvironment != lookupEnvironment) {
            return false;
        }
        action.doWithBinding(typeModel, binding);
        return true;
    }
}

From source file:com.redhat.ceylon.eclipse.core.model.JDTModelLoader.java

License:Open Source License

public static boolean doWithMethodBinding(final IType declaringClassModel, final MethodBinding binding,
        final ActionOnMethodBinding action) {
    if (declaringClassModel == null) {
        throw new ModelResolutionException("Resolving action requested on a missing declaration");
    }/*from w  ww.  j a  v a 2s. c o m*/

    if (binding == null) {
        return false;
    }
    ReferenceBinding declaringClassBinding = binding.declaringClass;
    if (declaringClassBinding == null) {
        return false;
    }
    PackageBinding packageBinding = declaringClassBinding.getPackage();
    if (packageBinding == null) {
        return false;
    }
    LookupEnvironment lookupEnvironment = packageBinding.environment;
    if (lookupEnvironment == null) {
        return false;
    }

    JDTModelLoader modelLoader = getModelLoader(declaringClassModel);
    if (modelLoader == null) {
        throw new ModelResolutionException("The Model Loader corresponding the type '"
                + declaringClassModel.getFullyQualifiedName() + "' doesn't exist");
    }

    synchronized (modelLoader.lookupEnvironmentMutex) {
        if (modelLoader.lookupEnvironment != lookupEnvironment) {
            return false;
        }
        action.doWithBinding(declaringClassModel, declaringClassBinding, binding);
        return true;
    }
}

From source file:com.redhat.ceylon.eclipse.core.model.JDTModelLoader.java

License:Open Source License

public static IType toType(ReferenceBinding binding) {
    ModelLoaderNameEnvironment nameEnvironment = (ModelLoaderNameEnvironment) binding
            .getPackage().environment.nameEnvironment;
    char[][] compoundName = ((ReferenceBinding) binding).compoundName;
    IType typeModel = nameEnvironment.findTypeInNameLookup(compoundName);

    if (typeModel == null && !(binding instanceof MissingTypeBinding)) {
        throw new ModelResolutionException("JDT reference binding without a JDT IType element !");
    }/*from  w w w.  j a va 2s.  com*/
    return typeModel;
}

From source file:com.redhat.ceylon.eclipse.core.model.mirror.UnknownClassMirror.java

License:Open Source License

public JDTClass(ReferenceBinding klass, IType type) {
    this.type = type;
    bindingRef = new WeakReference<ReferenceBinding>(klass);
    pkg = new JDTPackage(klass.getPackage());
    simpleName = new String(klass.sourceName());
    qualifiedName = JDTUtils.getFullyQualifiedName(klass);
    isPublic = klass.isPublic();//from  ww  w  .  ja  v a  2  s  . co  m
    isInterface = klass.isInterface();
    isAbstract = klass.isAbstract();
    isProtected = klass.isProtected();
    isDefaultAccess = klass.isDefault();
    isLocalType = klass.isLocalType();
    isStatic = (klass.modifiers & ClassFileConstants.AccStatic) != 0;
    isFinal = klass.isFinal();
    isEnum = klass.isEnum();
    isBinary = klass.isBinaryBinding();
    isAnonymous = klass.isAnonymousType();
    isJavaSource = (klass instanceof SourceTypeBinding)
            && new String(((SourceTypeBinding) klass).getFileName()).endsWith(".java");
    isAnnotationType = klass.isAnnotationType();
    bindingKey = klass.computeUniqueKey();

    char[] bindingFileName = klass.getFileName();
    int start = CharOperation.lastIndexOf('/', bindingFileName) + 1;
    if (start == 0 || start < CharOperation.lastIndexOf('\\', bindingFileName))
        start = CharOperation.lastIndexOf('\\', bindingFileName) + 1;
    fileName = new String(CharOperation.subarray(bindingFileName, start, -1));

    int jarFileEntrySeparatorIndex = CharOperation.indexOf(IDependent.JAR_FILE_ENTRY_SEPARATOR,
            bindingFileName);
    if (jarFileEntrySeparatorIndex > 0) {
        char[] jarPart = CharOperation.subarray(bindingFileName, 0, jarFileEntrySeparatorIndex);
        IJavaElement jarPackageFragmentRoot = JavaCore.create(new String(jarPart));
        String jarPath = jarPackageFragmentRoot.getPath().toOSString();
        char[] entryPart = CharOperation.subarray(bindingFileName, jarFileEntrySeparatorIndex + 1,
                bindingFileName.length);
        fullPath = new StringBuilder(jarPath).append("!/").append(entryPart).toString();
    } else {
        fullPath = new String(bindingFileName);
    }

    ReferenceBinding sourceOrClass = klass;
    if (!klass.isBinaryBinding()) {
        sourceOrClass = klass.outermostEnclosingType();
    }
    char[] classFullName = new char[0];
    for (char[] part : sourceOrClass.compoundName) {
        classFullName = CharOperation.concat(classFullName, part, '/');
    }
    char[][] temp = CharOperation.splitOn('.', sourceOrClass.getFileName());
    String extension = temp.length > 1 ? "." + new String(temp[temp.length - 1]) : "";
    javaModelPath = new String(classFullName) + extension;

    if (type == null) {
        annotations = new HashMap<>();
        methods = Collections.emptyList();
        interfaces = Collections.emptyList();
        typeParams = Collections.emptyList();
        fields = Collections.emptyList();
        innerClasses = Collections.emptyList();
    }
}

From source file:com.redhat.ceylon.eclipse.core.model.mirror.JDTMethod.java

License:Open Source License

private boolean isDefinedInType(ReferenceBinding superClass, MethodBinding method) {
    MethodVerifier methodVerifier = superClass.getPackage().environment.methodVerifier();
    for (MethodBinding inheritedMethod : superClass.methods()) {
        // skip ignored methods
        if (ignoreMethodInAncestorSearch(inheritedMethod)) {
            continue;
        }/*from   w  w w.  java  2s . co  m*/

        if (methodVerifier.doesMethodOverride(method, inheritedMethod)) {
            return true;
        }
    }
    return false;
}

From source file:com.redhat.ceylon.eclipse.core.model.mirror.JDTMethod.java

License:Open Source License

private boolean isOverloadingInType(ReferenceBinding superClass, MethodBinding method) {
    MethodVerifier methodVerifier = superClass.getPackage().environment.methodVerifier();
    for (MethodBinding inheritedMethod : superClass.methods()) {
        if (inheritedMethod.isPrivate() || inheritedMethod.isStatic() || inheritedMethod.isConstructor()
                || inheritedMethod.isBridge() || inheritedMethod.isSynthetic()
                || !Arrays.equals(inheritedMethod.constantPoolName(), method.selector))
            continue;

        // skip ignored methods
        if (ignoreMethodInAncestorSearch(inheritedMethod)) {
            continue;
        }/*from  w w w . j  a  v a 2s. com*/

        // if it does not override it and has the same name, it's overloading
        if (!methodVerifier.doesMethodOverride(method, inheritedMethod)) {
            return true;
        }
    }
    return false;
}

From source file:com.redhat.ceylon.eclipse.core.model.mirror.JDTUtils.java

License:Open Source License

public static ReferenceBinding inferTypeParametersFromSuperClass(ReferenceBinding declaringClass,
        ReferenceBinding superClass) {
    if (superClass instanceof RawTypeBinding && declaringClass instanceof ParameterizedTypeBinding) {
        LookupEnvironment lookupEnvironment = superClass.getPackage().environment;
        ParameterizedTypeBinding rawSuperType = (ParameterizedTypeBinding) superClass;
        ParameterizedTypeBinding declaringType = (ParameterizedTypeBinding) declaringClass;
        superClass = lookupEnvironment.createParameterizedType(rawSuperType.genericType(),
                declaringType.arguments, rawSuperType.enclosingType());
    }//from w  w w.  j  a  va  2 s. c o  m
    return superClass;
}

From source file:io.gige.compiler.internal.HookedJavaFileObject.java

License:Open Source License

protected void closed() {
    if (!_closed) {
        _closed = true;/*from w  w  w .j ava 2s. c  om*/
        // TODO: support encoding
        switch (this.getKind()) {
        case SOURCE:
            CompilationUnit unit = new CompilationUnit(null, _fileName, null /* encoding */);
            _filer.addNewUnit(unit);
            break;
        case CLASS:
            IBinaryType binaryType = null;
            try {
                binaryType = ClassFileReader.read(_fileName);
            } catch (ClassFormatException e) {
                /*
                 * When the annotation processor produces garbage, javac
                 * seems to show some resilience, by hooking the source
                 * type, which since is resolved can answer annotations
                 * during discovery - Not sure if this sanctioned by the
                 * spec, to be taken up with Oracle. Here we mimic the bug,
                 * see that addNewClassFile is simply collecting
                 * ReferenceBinding's, so adding a SourceTypeBinding works
                 * just fine.
                 */
                ReferenceBinding type = this._filer._env.getCompiler().lookupEnvironment
                        .getType(CharOperation.splitOn('.', _typeName.toCharArray()));
                if (type != null)
                    _filer.addNewClassFile(type);
            } catch (IOException e) {
                // ignore
            }
            if (binaryType != null) {
                char[] name = binaryType.getName();
                ReferenceBinding type = this._filer._env.getCompiler().lookupEnvironment
                        .getType(CharOperation.splitOn('/', name));
                if (type != null && type.isValidBinding()) {
                    if (type.isBinaryBinding()) {
                        _filer.addNewClassFile(type);
                    } else {
                        BinaryTypeBinding binaryBinding = new BinaryTypeBinding(type.getPackage(), binaryType,
                                this._filer._env.getCompiler().lookupEnvironment, true);
                        if (binaryBinding != null)
                            _filer.addNewClassFile(binaryBinding);
                    }
                }
            }
            break;
        case HTML:
        case OTHER:
            break;
        }
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.smap.AbstractSmapGenerator.java

License:Open Source License

protected String getPackagePathFromRefBinding(ReferenceBinding toplevelBinding) {
    PackageBinding pkgBinding = null;//from   w w  w .  j av a 2s .com
    if (toplevelBinding.enclosingType() != null)
        pkgBinding = toplevelBinding.enclosingType().teamPackage;
    if (pkgBinding == null)
        pkgBinding = toplevelBinding.getPackage();
    String pkgName = String.valueOf(pkgBinding.readableName());
    pkgName = pkgName.replace('.', ISMAPConstants.OTJ_PATH_DELIMITER_CHAR);

    if (pkgName != null && pkgName.length() > 0)
        return pkgName + ISMAPConstants.OTJ_PATH_DELIMITER;

    return DEFAULT_PACKAGE;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.TypeAnalyzer.java

License:Open Source License

/**
 * Split the qualified name of a member type into a compound name.
 * (the field compoundName merges Outer$Inner into one element
 *  of the compound, which is not what we want here).
 * @param tb/*from   www.ja v  a2s .  c o m*/
 * @param includePackage should the package name(s) be included?
 * @param createTeamAnchor should a possible team anchor be included in the compound name?
 *        TODO(SH) saying yes here causes as to create old syntax AST.
 * @return non-null array of at least one component
 */
public static char[][] compoundNameOfReferenceType(ReferenceBinding tb, boolean includePackage,
        boolean createTeamAnchor) {
    if (tb instanceof ProblemReferenceBinding) {
        ReferenceBinding closestMatch = (ReferenceBinding) tb.closestMatch();
        if (closestMatch != null)
            tb = closestMatch;
    }
    if (!tb.isValidBinding()) { // no further processing possible
        if (includePackage)
            return tb.compoundName;
        int l = tb.compoundName.length;
        return new char[][] { tb.compoundName[l - 1] };
    }
    if (tb instanceof UnresolvedReferenceBinding) {
        LookupEnvironment env = Config.getLookupEnvironment();
        if (env == null)
            throw new InternalCompilerError("No lookup environment configured"); //$NON-NLS-1$
        tb = ((UnresolvedReferenceBinding) tb).resolve(env, false);
    }
    if (createTeamAnchor && tb instanceof DependentTypeBinding
            && ((DependentTypeBinding) tb).hasExplicitAnchor()) {
        DependentTypeBinding roleTypeBinding = (DependentTypeBinding) tb;

        // for role types the prefix is a variable not a type:
        ITeamAnchor[] path = roleTypeBinding._teamAnchor.getBestNamePath();

        // If anchor is a field, prepend a team anchor with "Outer.this"
        // for the type containing the anchor field.
        char[] declaringClass = null;
        int prefixLen = 0;
        if (roleTypeBinding._teamAnchor instanceof FieldBinding) {
            declaringClass = ((FieldBinding) (roleTypeBinding)._teamAnchor).declaringClass.internalName();
            prefixLen = 2;
        }
        char[][] names = new char[path.length + 1 + prefixLen][];
        if (declaringClass != null) {
            // do prepend
            names[0] = declaringClass;
            names[1] = "this".toCharArray(); //$NON-NLS-1$
        }
        for (int i = 0; i < path.length; i++) {
            names[i + prefixLen] = path[i].internalName();
        }
        names[path.length + prefixLen] = tb.internalName();

        return names;
    }
    char[][] packName = includePackage && (tb.getPackage() != null) ? tb.getPackage().compoundName
            : new char[0][];
    char[][] outerName = (tb.enclosingType() != null)
            ? compoundNameOfReferenceType(tb.enclosingType(), false, createTeamAnchor)
            : new char[0][];
    char[][] result = new char[packName.length + outerName.length + 1][];
    System.arraycopy(packName, 0, result, 0, packName.length);
    System.arraycopy(outerName, 0, result, packName.length, outerName.length);
    result[result.length - 1] = tb.internalName();
    return result;
}