Java Code Examples for org.eclipse.jdt.core.IType#isMember()

The following examples show how to use org.eclipse.jdt.core.IType#isMember() . 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: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void appendFlags(final IMember member) throws JavaModelException {
	if (member instanceof IAnnotatable)
		for (IAnnotation annotation : ((IAnnotatable) member).getAnnotations()) {
			appendAnnotation(annotation);
		}
	
	int flags= member.getFlags();
	final int kind= member.getElementType();
	if (kind == IJavaElement.TYPE) {
		flags&= ~Flags.AccSuper;
		final IType type= (IType) member;
		if (!type.isMember())
			flags&= ~Flags.AccPrivate;
		if (Flags.isEnum(flags))
			flags&= ~Flags.AccAbstract;
	}
	if (Flags.isEnum(flags))
		flags&= ~Flags.AccFinal;
	if (kind == IJavaElement.METHOD) {
		flags&= ~Flags.AccVarargs;
		flags&= ~Flags.AccBridge;
	}
	if (flags != 0)
		fBuffer.append(Flags.toString(flags));
}
 
Example 2
Source File: JavaOutlinePage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Checks whether a given Java element is an inner type.
 *
 * @param element the java element
 * @return <code>true</code> iff the given element is an inner type
 */
private boolean isInnerType(IJavaElement element) {

	if (element != null && element.getElementType() == IJavaElement.TYPE) {
		IType type= (IType)element;
		try {
			return type.isMember();
		} catch (JavaModelException e) {
			IJavaElement parent= type.getParent();
			if (parent != null) {
				int parentElementType= parent.getElementType();
				return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE);
			}
		}
	}

	return false;
}
 
Example 3
Source File: ExtraFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static int getExtraFlags(IType type) throws JavaModelException {
	int extraFlags = 0;
	
	if (type.isMember()) {
		extraFlags |= ExtraFlags.IsMemberType;
	}
	
	if (type.isLocal()) {
		extraFlags |= ExtraFlags.IsLocalType;
	}
	
	IType[] memberTypes = type.getTypes();
	int memberTypeCounter = memberTypes == null ? 0 : memberTypes.length;
	if (memberTypeCounter > 0) {
		done : for (int i = 0; i < memberTypeCounter; i++) {
			int flags = memberTypes[i].getFlags();
			// if the member type is static and not private
			if ((flags & ClassFileConstants.AccStatic) != 0 && (flags & ClassFileConstants.AccPrivate) == 0 ) {
				extraFlags |= ExtraFlags.HasNonPrivateStaticMemberTypes;
				break done;
			}
		}
	}
	
	return extraFlags;
}
 
Example 4
Source File: SourceCreationOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Runs the stub generation on the specified class file.
 *
 * @param file
 *            the class file
 * @param parent
 *            the parent store
 * @param monitor
 *            the progress monitor to use
 * @throws CoreException
 *             if an error occurs
 */
@Override
protected void run(final IClassFile file, final IFileStore parent, final IProgressMonitor monitor) throws CoreException {
	try {
		monitor.beginTask(getOperationLabel(), 2);
		final IType type= file.getType();
		if (type.isAnonymous() || type.isLocal() || type.isMember())
			return;
		final String source= file.getSource();
		createCompilationUnit(parent, type.getElementName() + JavaModelUtil.DEFAULT_CU_SUFFIX, source != null ? source : "", monitor); //$NON-NLS-1$
	} finally {
		monitor.done();
	}
}
 
Example 5
Source File: StubCreationOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Runs the stub generation on the specified class file.
 *
 * @param file
 *            the class file
 * @param parent
 *            the parent store
 * @param monitor
 *            the progress monitor to use
 * @throws CoreException
 *             if an error occurs
 */
@Override
protected void run(final IClassFile file, final IFileStore parent, final IProgressMonitor monitor) throws CoreException {
	try {
		monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 2);
		SubProgressMonitor subProgressMonitor= new SubProgressMonitor(monitor, 1);
		final IType type= file.getType();
		if (type.isAnonymous() || type.isLocal() || type.isMember())
			return;
		String source= new StubCreator(fStubInvisible).createStub(type, subProgressMonitor);
		createCompilationUnit(parent, type.getElementName() + JavaModelUtil.DEFAULT_CU_SUFFIX, source, monitor);
	} finally {
		monitor.done();
	}
}
 
Example 6
Source File: MethodProposalInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tests if a method equals to the given signature. Parameter types are only
 * compared by the simple name, no resolving for the fully qualified type
 * name is done. Constructors are only compared by parameters, not the name.
 *
 * @param name Name of the method
 * @param paramTypes The type signatures of the parameters e.g.
 *        <code>{"QString;","I"}</code>
 * @param isConstructor Specifies if the method is a constructor
 * @param method the method to be compared with this info's method
 * @param typeVariables a map from type variables to types
 * @param type the given type that declares the method
 * @return Returns <code>true</code> if the method has the given name and
 *         parameter types and constructor state.
 * @throws JavaModelException if the method does not exist or if an exception occurs while accessing its corresponding resource
 */
private boolean isSameMethodSignature(String name, String[] paramTypes, boolean isConstructor, IMethod method, Map<String, char[]> typeVariables, IType type) throws JavaModelException {
	if (isConstructor || name.equals(method.getElementName())) {
		if (isConstructor == method.isConstructor()) {
			String[] otherParams= method.getParameterTypes(); // types may be type variables
			boolean isBinaryConstructorForNonStaticMemberClass=
					method.isBinary()
					&& type.isMember()
					&& !Flags.isStatic(type.getFlags());
			int syntheticParameterCorrection= isBinaryConstructorForNonStaticMemberClass && paramTypes.length == otherParams.length - 1 ? 1 : 0;
			if (paramTypes.length == otherParams.length - syntheticParameterCorrection) {
				fFallbackMatch= method;
				String signature= method.getSignature();
				String[] otherParamsFromSignature= Signature.getParameterTypes(signature); // types are resolved / upper-bounded
				// no need to check method type variables since these are
				// not yet bound when proposing a method
				for (int i= 0; i < paramTypes.length; i++) {
					String ourParamName= computeSimpleTypeName(paramTypes[i], typeVariables);
					String otherParamName1= computeSimpleTypeName(otherParams[i + syntheticParameterCorrection], typeVariables);
					String otherParamName2= computeSimpleTypeName(otherParamsFromSignature[i + syntheticParameterCorrection], typeVariables);

					if (!ourParamName.equals(otherParamName1) && !ourParamName.equals(otherParamName2)) {
						return false;
					}
				}
				return true;
			}
		}
	}
	return false;
}
 
Example 7
Source File: JavaOutlineInformationControl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isInnerType(IJavaElement element) {
	if (element != null && element.getElementType() == IJavaElement.TYPE) {
		IType type= (IType)element;
		try {
			return type.isMember();
		} catch (JavaModelException e) {
			IJavaElement parent= type.getParent();
			if (parent != null) {
				int parentElementType= parent.getElementType();
				return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE);
			}
		}
	}
	return false;
}
 
Example 8
Source File: MarkerUtil.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Get the underlying resource (Java class) for given BugInstance.
 *
 * @param bug
 *            the BugInstance
 * @param project
 *            the project
 * @return the IResource representing the Java class
 */
private static @CheckForNull IJavaElement getJavaElement(BugInstance bug, IJavaProject project) throws JavaModelException {

    SourceLineAnnotation primarySourceLineAnnotation = bug.getPrimarySourceLineAnnotation();
    String qualifiedClassName = primarySourceLineAnnotation.getClassName();

    //        if (Reporter.DEBUG) {
    //            System.out.println("Looking up class: " + packageName + ", " + qualifiedClassName);
    //        }

    Matcher m = fullName.matcher(qualifiedClassName);
    IType type;
    String innerName = null;
    if (m.matches() && m.group(2).length() > 0) {

        String outerQualifiedClassName = m.group(1).replace('$', '.');
        innerName = m.group(2).substring(1);
        // second argument is required to find also secondary types
        type = project.findType(outerQualifiedClassName, (IProgressMonitor) null);

        /*
         * code below only points to the first line of inner class even if
         * this is not a class bug but field bug
         */
        if (type != null && !hasLineInfo(primarySourceLineAnnotation)) {
            completeInnerClassInfo(qualifiedClassName, innerName, type, bug);
        }
    } else {
        // second argument is required to find also secondary types
        type = project.findType(qualifiedClassName.replace('$', '.'), (IProgressMonitor) null);

        // for inner classes, some detectors does not properly report source
        // lines:
        // instead of reporting the first line of inner class, they report
        // first line of parent class
        // in this case we will try to fix this here and point to the right
        // start line
        if (type != null && type.isMember()) {
            if (!hasLineInfo(primarySourceLineAnnotation)) {
                completeInnerClassInfo(qualifiedClassName, type.getElementName(), type, bug);
            }
        }
    }

    // reassign it as it may be changed for inner classes
    primarySourceLineAnnotation = bug.getPrimarySourceLineAnnotation();

    /*
     * Eclipse can help us find the line number for fields => we trying to
     * add line info for fields here
     */
    int startLine = primarySourceLineAnnotation.getStartLine();
    // TODO don't use "1", use "0" ?
    if (startLine <= 1 && type != null) {
        FieldAnnotation primaryField = bug.getPrimaryField();
        if (primaryField != null) {
            completeFieldInfo(qualifiedClassName, type, bug, primaryField);
        }
    }

    return type;
}
 
Example 9
Source File: JavadocContents.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private String computeMethodAnchorPrefixEnd(BinaryMethod method) throws JavaModelException {
	String typeQualifiedName = null;
	if (this.type.isMember()) {
		IType currentType = this.type;
		StringBuffer buffer = new StringBuffer();
		while (currentType != null) {
			buffer.insert(0, currentType.getElementName());
			currentType = currentType.getDeclaringType();
			if (currentType != null) {
				buffer.insert(0, '.');
			}
		}
		typeQualifiedName = new String(buffer.toString());
	} else {
		typeQualifiedName = this.type.getElementName();
	}
	
	String methodName = method.getElementName();
	if (method.isConstructor()) {
		methodName = typeQualifiedName;
	}
	IBinaryMethod info = (IBinaryMethod) method.getElementInfo();

	char[] genericSignature = info.getGenericSignature();
	String anchor = null;
	if (genericSignature != null) {
		genericSignature = CharOperation.replaceOnCopy(genericSignature, '/', '.');
		anchor = Util.toAnchor(0, genericSignature, methodName, Flags.isVarargs(method.getFlags()));
		if (anchor == null) throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.UNKNOWN_JAVADOC_FORMAT, method));
	} else {
		anchor = Signature.toString(method.getSignature().replace('/', '.'), methodName, null, true, false, Flags.isVarargs(method.getFlags()));
	}
	IType declaringType = this.type;
	if (declaringType.isMember()) {
		// might need to remove a part of the signature corresponding to the synthetic argument (only for constructor)
		if (method.isConstructor() && !Flags.isStatic(declaringType.getFlags())) {
			int indexOfOpeningParen = anchor.indexOf('(');
			if (indexOfOpeningParen == -1) {
				// should not happen as this is a method signature
				return null;
			}
			int index = indexOfOpeningParen;
			indexOfOpeningParen++;
			int indexOfComma = anchor.indexOf(',', index);
			if (indexOfComma != -1) {
				index = indexOfComma + 2;
			} else {
				// no argument, but a synthetic argument
				index = anchor.indexOf(')', index);
			}
			anchor = anchor.substring(0, indexOfOpeningParen) + anchor.substring(index);
		}
	}
	return anchor + JavadocConstants.ANCHOR_PREFIX_END;
}