Example usage for org.eclipse.jdt.core IJavaElement METHOD

List of usage examples for org.eclipse.jdt.core IJavaElement METHOD

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement METHOD.

Prototype

int METHOD

To view the source code for org.eclipse.jdt.core IJavaElement METHOD.

Click Source Link

Document

Constant representing a method or constructor.

Usage

From source file:org.eclipse.e4.demo.simpleide.jdt.internal.editor.viewer.JavaElementLabelComposer.java

License:Open Source License

/**
 * Appends the label for a type. Considers the T_* flags.
 * /* w w w  .j  ava  2  s. co  m*/
 * @param type
 *            the element to render
 * @param flags
 *            the rendering flags. Flags with names starting with 'T_' are
 *            considered.
 */
public void appendTypeLabel(IType type, long flags) {

    if (getFlag(flags, JavaElementLabels.T_FULLY_QUALIFIED)) {
        IPackageFragment pack = type.getPackageFragment();
        if (!pack.isDefaultPackage()) {
            appendPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
    }
    if (getFlag(flags, JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED)) {
        IType declaringType = type.getDeclaringType();
        if (declaringType != null) {
            appendTypeLabel(declaringType, JavaElementLabels.T_CONTAINER_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
        int parentType = type.getParent().getElementType();
        if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD
                || parentType == IJavaElement.INITIALIZER) { // anonymous or
            // local
            appendElementLabel(type.getParent(), 0);
            fBuffer.append('.');
        }
    }

    String typeName = getElementName(type);
    if (typeName.length() == 0) { // anonymous
        try {
            if (type.getParent() instanceof IField && type.isEnum()) {
                typeName = '{' + JavaElementLabels.ELLIPSIS_STRING + '}';
            } else {
                String supertypeName;
                String[] superInterfaceSignatures = type.getSuperInterfaceTypeSignatures();
                if (superInterfaceSignatures.length > 0) {
                    supertypeName = getSimpleTypeName(type, superInterfaceSignatures[0]);
                } else {
                    supertypeName = getSimpleTypeName(type, type.getSuperclassTypeSignature());
                }
                typeName = messages.JavaElementLabels_anonym_type(supertypeName);
            }
        } catch (JavaModelException e) {
            // ignore
            typeName = messages.JavaElementLabels_anonym();
        }
    }
    fBuffer.append(typeName);
    if (getFlag(flags, JavaElementLabels.T_TYPE_PARAMETERS)) {
        if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && type.isResolved()) {
            BindingKey key = new BindingKey(type.getKey());
            if (key.isParameterizedType()) {
                String[] typeArguments = key.getTypeArguments();
                appendTypeArgumentSignaturesLabel(type, typeArguments, flags);
            } else {
                String[] typeParameters = Signature.getTypeParameters(key.toSignature());
                appendTypeParameterSignaturesLabel(typeParameters, flags);
            }
        } else if (type.exists()) {
            try {
                appendTypeParametersLabels(type.getTypeParameters(), flags);
            } catch (JavaModelException e) {
                // ignore
            }
        }
    }

    // category
    if (getFlag(flags, JavaElementLabels.T_CATEGORY) && type.exists()) {
        try {
            appendCategoryLabel(type, flags);
        } catch (JavaModelException e) {
            // ignore
        }
    }

    // post qualification
    if (getFlag(flags, JavaElementLabels.T_POST_QUALIFIED)) {
        int offset = fBuffer.length();
        fBuffer.append(JavaElementLabels.CONCAT_STRING);
        IType declaringType = type.getDeclaringType();
        if (declaringType != null) {
            appendTypeLabel(declaringType, JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            int parentType = type.getParent().getElementType();
            if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD
                    || parentType == IJavaElement.INITIALIZER) { // anonymous
                // or
                // local
                fBuffer.append('.');
                appendElementLabel(type.getParent(), 0);
            }
        } else {
            appendPackageFragmentLabel(type.getPackageFragment(), flags & QUALIFIER_FLAGS);
        }
        if (getFlag(flags, JavaElementLabels.COLORIZE)) {
            fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
        }
    }
}

From source file:org.eclipse.emf.ecore.xcore.ui.refactoring.XcoreRenameRefactoringProcessorFactory.java

License:Open Source License

@Override
public JavaRenameProcessor createRenameProcessor(IJavaElement element) {
    try {/*from  w  ww. j  a  v a  2 s.  c o m*/
        switch (element.getElementType()) {
        case IJavaElement.TYPE: {
            return new RenameTypeProcessor((IType) element);
        }
        case IJavaElement.FIELD: {
            return new RenameFieldProcessor((IField) element);
        }
        case IJavaElement.METHOD: {
            return Flags.isStatic(((IMethod) element).getFlags())
                    ? new RenameNonVirtualMethodProcessor((IMethod) element)
                    : new RenameVirtualMethodProcessor((IMethod) element);
        }
        case IJavaElement.TYPE_PARAMETER: {
            return new RenameTypeParameterProcessor((ITypeParameter) element);
        }
        case IJavaElement.LOCAL_VARIABLE: {
            return new RenameLocalVariableProcessor((ILocalVariable) element);
        }
        }
    } catch (JavaModelException exc) {
        LOG.error("Error creating refactoring processor for " + element.getElementName(), exc);
    }
    return null;
}

From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java

License:Open Source License

private TextEdit updateNonJavaContent(ICompilationUnit cu, String[] destPackageName, String[] currPackageName,
        String newName) throws JavaModelException {
    // package statement
    IPackageDeclaration[] packageDecls = cu.getPackageDeclarations();
    boolean doPackage = !Util.equalArraysOrNull(currPackageName, destPackageName);
    boolean doName = newName != null;
    MultiTextEdit multiEdit = new MultiTextEdit();
    if (doPackage) {
        if (packageDecls.length == 1) {
            ISourceRange packageRange = packageDecls[0].getSourceRange();
            if (destPackageName == null || destPackageName.length == 0) {
                // move to default package
                multiEdit.addChild(new DeleteEdit(packageRange.getOffset(), packageRange.getLength()));
            } else {
                multiEdit.addChild(new ReplaceEdit(packageRange.getOffset(), packageRange.getLength(),
                        "package " + Util.concatWith(destPackageName, '.'))); //$NON-NLS-1$
            }/*from  w w w . j  a  v  a 2  s . c om*/
        } else {
            // move from default package
            // we don't keep track of comments, so we don't know where they start.  Just add the package declaration at location 0
            multiEdit.addChild(new InsertEdit(0, "package " + Util.concatWith(destPackageName, '.') + "\n")); //$NON-NLS-1$//$NON-NLS-2$
        }
    }

    if (doName) {
        int dotIndex = cu.getElementName().indexOf('.');
        dotIndex = dotIndex == -1 ? cu.getElementName().length() : dotIndex;
        String oldTypeName = cu.getElementName().substring(0, dotIndex);
        dotIndex = newName.indexOf('.');
        dotIndex = dotIndex == -1 ? newName.length() : dotIndex;
        String newTypeName = newName.substring(0, dotIndex);
        IType type = cu.getType(oldTypeName);
        if (type.exists()) {
            ISourceRange nameRange = type.getNameRange();
            // main type can be implicitly defined, so check offsets
            if (nameRange.getOffset() > 0 && nameRange.getLength() > 0
                    && oldTypeName.length() == nameRange.getLength()) {
                multiEdit.addChild(new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), newTypeName));
            }
            IJavaElement[] children = type.getChildren();
            for (int i = 0; i < children.length; i++) {
                if (children[i].getElementType() == IJavaElement.METHOD) {
                    IMethod method = (IMethod) children[i];
                    if (method.isConstructor()) {
                        nameRange = method.getNameRange();
                        // main type can be implicitly defined, so check offsets
                        if (nameRange.getOffset() > 0 && nameRange.getLength() > 0) {
                            multiEdit.addChild(
                                    new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), newTypeName));
                        }
                    }
                }
            }
        }
    }
    return multiEdit;
}

From source file:org.eclipse.jpt.jpa.core.jpql.spi.JpaType.java

License:Open Source License

protected Collection<IConstructor> buildConstructors() {

    ITypeBinding typeBinding = getTypeBinding();

    // No Java source is attached to the Java class file, parse the class with a reader
    if ((typeBinding == null) && this.type.isBinary()) {
        Collection<IConstructor> constructors = new ArrayList<IConstructor>();

        try {// w  w  w .ja v  a 2 s . co m
            // Root types
            for (IJavaElement rootType : this.type.getTypeRoot().getChildren()) {
                // Root type
                if (rootType.getElementType() == IJavaElement.TYPE) {
                    for (IJavaElement javaElement : ((IType) rootType).getChildren()) {
                        // Method
                        if (javaElement.getElementType() == IJavaElement.METHOD) {
                            IMethod method = (IMethod) javaElement;
                            // Constructor
                            if (method.isConstructor()) {
                                constructors.add(new ClassConstructor(this, method));
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            JptJpaCorePlugin.instance().logError(e);
        }

        return constructors;
    }
    // Use the ITypeBinding to retrieve the constructors
    else if (typeBinding != null) {
        Collection<IConstructor> constructors = new ArrayList<IConstructor>();

        for (IMethodBinding method : typeBinding.getDeclaredMethods()) {
            if (method.isConstructor()) {
                constructors.add(new JpaConstructor(this, method));
            }
        }

        return constructors;
    }

    return Collections.emptyList();
}

From source file:org.eclipse.jst.common.internal.annotations.ui.AnnotationTagCompletionProc.java

License:Open Source License

/**
 * Calculates the the area of the annotation we're trying to complete. Also initializes
 * m_tagScope.//from w  ww .j ava  2 s . c  o m
 * 
 * @param fromOffset
 * @return
 * @throws JavaModelException
 */
private AnnotationArea getAnnotationArea(int fromOffset) throws JavaModelException {
    // First, roughly calculate the end of the comment.
    IJavaElement el = m_icu.getElementAt(fromOffset);
    int absmax, absmin;
    if (el == null)
        return null;
    int ty = el.getElementType();

    switch (ty) {
    case IJavaElement.FIELD:
        IField f = (IField) el;
        absmax = f.getNameRange().getOffset();
        absmin = f.getSourceRange().getOffset();
        m_tagScope = TagSpec.FIELD;
        break;

    case IJavaElement.TYPE:
        IType t = (IType) el;
        absmax = t.getNameRange().getOffset();
        absmin = t.getSourceRange().getOffset();
        m_tagScope = TagSpec.TYPE;
        break;

    case IJavaElement.METHOD:
        IMethod m = (IMethod) el;
        absmax = m.getNameRange().getOffset();
        absmin = m.getSourceRange().getOffset();
        m_tagScope = TagSpec.METHOD;
        break;

    default:
        m_tagScope = -1;
        return null;
    }

    // Make sure we're not after the name for the member.
    if (absmax < fromOffset) {
        return null;
    }

    int min = 0, max = 0;
    try {
        // Search backwards for the starting '@'.
        boolean found = false;
        for (min = fromOffset; min >= absmin; min--) {
            if (m_doc.getChar(min) == '@') {
                found = true;
                break;
            }
        }
        if (!found) {
            return null;
        }

        // Search forwards for the next '@', or the end of the comment.
        for (max = fromOffset + 1; max < absmax; max++) {
            if (m_doc.getChar(max) == '@') {
                break;
            }
        }
    } catch (BadLocationException e) {
        return null;
    }

    return new AnnotationArea(el, min, Math.min(absmax, max));
}

From source file:org.eclipse.jst.jee.model.internal.common.AbstractAnnotationFactory.java

License:Open Source License

protected void processEjbAnnotation(IAnnotation annotation, List<EjbLocalRef> localRefs, IMember member,
        Collection<IType> dependedTypes) throws JavaModelException {
    int memberType = member.getElementType();
    IMemberValuePair[] pairs = annotation.getMemberValuePairs();

    String beanInterfaceValue = (String) getAnnotatedValue("beanInterface", pairs); //$NON-NLS-1$
    beanInterfaceValue = internalProcessInjection(beanInterfaceValue, member, dependedTypes);
    if (beanInterfaceValue == null)
        return;/*from  w  w  w. ja v  a 2  s.  co  m*/

    /*
     * The name of the reference should be the value of the "name"
     * attribute. If there is no "name" attribute then the name of the
     * reference is the qualified name of the member. Check this at
     * Enterprise Java Beans, 3.0, Section 14.1.5.3
     */
    String refName = (String) getAnnotatedValue("name", pairs); //$NON-NLS-1$
    if (refName == null) {
        refName = getMemberQualifiedName(member);
    }
    EjbLocalRef ref = JavaeeFactory.eINSTANCE.createEjbLocalRef();
    ref.setEjbRefName(refName);
    localRefs.add(ref);
    ref.setLocal(beanInterfaceValue);
    ref.setLocalHome(beanInterfaceValue);
    ref.setEjbLink((String) getAnnotatedValue("beanName", pairs)); //$NON-NLS-1$
    ref.setMappedName((String) getAnnotatedValue("mappedName", pairs)); //$NON-NLS-1$
    if (memberType == IJavaElement.METHOD || memberType == IJavaElement.FIELD) {
        createInjectionTarget(refName, ref.getInjectionTargets(), annotation);
    }
}

From source file:org.eclipse.jst.jee.model.internal.common.AbstractAnnotationFactory.java

License:Open Source License

/**
 * Resource annotation can be placed on class, method, field.
 * //from  w ww  . ja v  a 2  s  .  com
 * Checks are made if the resource annotation is valid.
 * <p>
 * If on class there should be a "type" attribute. If on method the method
 * must have one param with type that is an interface. If on field the field
 * type must be an interface.
 * 
 * If the type of the method/field can not be resolved the result will
 * contain the unresolved value.
 * 
 * In case the type of method/field is array, wildcard, simple type this is
 * not a place for the annotation.
 * </p>
 * 
 * <p>
 * In case of method/field the type specified using the "type" attribute has
 * a higher priority that the method/field type.
 * </p>
 * 
 * <p>
 * Only resolved types are added to dependedTypes
 * </p>
 * 
 * @param sessionBean
 * @param member
 * @param annotation
 * @param dependedTypes
 * @throws JavaModelException
 */
protected void processResourceRefAnnotation(IAnnotation annotation, List<ResourceRef> resourceRefs,
        IMember member, Collection<IType> dependedTypes) throws JavaModelException {

    IMemberValuePair[] pairs = annotation.getMemberValuePairs();
    String specifiedType = (String) getAnnotatedValue("type", pairs); //$NON-NLS-1$
    specifiedType = internalProcessInjection(specifiedType, member, dependedTypes);
    if (specifiedType == null)
        return;
    String refName = (String) getAnnotatedValue("name", pairs); //$NON-NLS-1$
    if (refName == null)
        refName = getMemberQualifiedName(member);
    ResourceRef ref = JavaeeFactory.eINSTANCE.createResourceRef();
    ref.setResRefName(refName);
    ref.setResType(specifiedType);
    ref.setMappedName((String) getAnnotatedValue("mappedName", pairs)); //$NON-NLS-1$
    String description = (String) getAnnotatedValue("description", pairs); //$NON-NLS-1$
    if (description != null) {
        Description desc = JavaeeFactory.eINSTANCE.createDescription();
        desc.setValue(description);
        ref.getDescriptions().clear();
        ref.getDescriptions().add(desc);
    }
    if (member.getElementType() == IJavaElement.METHOD || member.getElementType() == IJavaElement.FIELD) {
        createInjectionTarget(refName, ref.getInjectionTargets(), annotation);
    }
    String value = (String) getAnnotatedValue("authenticationType", pairs); //$NON-NLS-1$
    /*
     * the default value is AuthenticationType.APPLICATION which is handled
     * by the EMF. no need to check for this value
     */
    if ("AuthenticationType.CONTAINER".equals(value)) { //$NON-NLS-1$
        ref.setResAuth(ResAuthType.CONTAINER_LITERAL);
    } else if ("CONTAINER".equals(value) //$NON-NLS-1$
            && containsImport(member.getCompilationUnit(), "AuthenticationType.CONTAINER")) { //$NON-NLS-1$
        ref.setResAuth(ResAuthType.CONTAINER_LITERAL);
    }
    Boolean shareable = (Boolean) getAnnotatedValue("shareable", pairs); //$NON-NLS-1$
    /*
     * The default value for sharable is true. Check and process only
     * unsharable
     */
    if (Boolean.FALSE.equals(shareable))
        ref.setResSharingScope(ResSharingScopeType.UNSHAREABLE_LITERAL);

    resourceRefs.add(ref);
}

From source file:org.eclipse.jst.jee.model.internal.common.AbstractAnnotationFactory.java

License:Open Source License

/**
 * The method has the task of processing the member along with the specified
 * member and return a String. The result is to be used as a reference value
 * for the injection on this member. Usage are the
 * /*from  w  w  w.  j  a  v a 2s  . co  m*/
 * @EJB and
 * @Resource annotations.
 * 
 * <p>
 * If the specifiedType is <code>null</code> and member is of type
 * IJavaElement.TYPE the method returns <code>null</code>
 * </p>
 * 
 * <p>
 * If the type of the member can be resolved and is an interface the method
 * returns <code>null</code>. Here the "type" of the member is the result
 * from {@link #getUnresolvedType(IMember)}
 * </p>
 * 
 * 
 * Only if the specifiedType can be calculated and is resolved it is added
 * to the dependedTypes. If the specifiedType can not be resolved nothing is
 * added to dependedTypes.
 * 
 * @see {@link #processEjbAnnotation(IAnnotation, SessionBean, IMember, Collection)}
 * @see #processResourceRefAnnotation(SessionBean, IMember, IAnnotation,
 *      Collection)
 * 
 * @param specifiedType
 * @param member
 * @param dependedTypes
 * @return
 * @throws JavaModelException
 */
private String internalProcessInjection(final String specifiedType, IMember member,
        Collection<IType> dependedTypes) throws JavaModelException {
    boolean methodOrField = member.getElementType() == IJavaElement.METHOD
            || member.getElementType() == IJavaElement.FIELD;
    IType declaringType = (IType) (member.getElementType() == IJavaElement.TYPE ? member
            : member.getDeclaringType());
    String memberType = getUnresolvedType(member);
    // not type for this member can be retrieved. If member is a method or
    // field this means there is an error.
    if (getClassTypeSignature(memberType) == null && methodOrField)
        return null;

    // both type are null. This is not a valid case. This will hapen for a
    // type without specified type.
    if (specifiedType == null && memberType == null)
        return null;

    String innerSpecifiedType = specifiedType;

    IType resolvedType = resolveType(declaringType, memberType);
    // we were able to get a type for the param of a method or type of
    // a field.
    // check if it is an interface. It might not be resolved, but we have a
    // value
    // for unresolved.
    if (methodOrField) {
        // if the resolved type is not null and it is not an interface this
        // annotation is not valid
        if (resolvedType != null) {
            if (resolvedType.isInterface())
                memberType = resolvedType.getFullyQualifiedName();
            else
                // invalid - if the method is with param that is not an
                // interface. Or the type of the field is not an interface.
                return null;
        }
    }
    // from now one use only the specified type for type resolving. If there
    // is no specified type use the member type. The check for whether they
    // were both null is previously made
    IType resolvedSpecifiedType = null;
    if (innerSpecifiedType == null) {
        innerSpecifiedType = memberType;
        resolvedSpecifiedType = resolvedType;
    } else
        resolvedSpecifiedType = resolveType(declaringType, innerSpecifiedType);
    if (resolvedSpecifiedType != null) {
        if (resolvedSpecifiedType.isInterface()) {
            innerSpecifiedType = resolvedSpecifiedType.getFullyQualifiedName();
            dependedTypes.add(resolvedSpecifiedType);
        } else
            // we have resolved the specified type and it is not an
            // interface. Not a valid annotation.
            return null;
    }
    return innerSpecifiedType;
}

From source file:org.eclipse.jst.jee.model.internal.common.AbstractAnnotationFactory.java

License:Open Source License

/**
 * This method returns a qualified name for this member. The name is to be
 * used as ejb-ref-name.//from  w w w  . j  a  v a2s.com
 * 
 * If the member is a type then fullyQualifiedName of the type is returned.
 * 
 * If the member is a <code>field</code> declared in a <code>type</code>
 * then the result is
 * <code>type.getFullyQualifiedName() +"/" + field.elementName</code>
 * 
 * If the member is a <code>method</code> declared in a <code>type</code>
 * and method name begins with "set" then: for type name =
 * "org.eclipse.Bean" and method name = "setMethodOne()" the result is
 * "org.eclipse.Bean/methodOne"
 * 
 * Check this at Enterprise Java Beans, 3.0, Section 14.1.5.3
 * 
 * @param member
 * @return
 */
private String getMemberQualifiedName(IMember member) {
    String memberName = member.getElementName();
    int elementType = member.getElementType();
    if (elementType == IJavaElement.METHOD && memberName.startsWith("set")) { //$NON-NLS-1$
        char ch = Character.toLowerCase(memberName.charAt(3));
        memberName = ch + memberName.substring(4);
    }
    return elementType == IJavaElement.TYPE ? ((IType) member).getFullyQualifiedName()
            : member.getDeclaringType().getFullyQualifiedName() + "/" + memberName; //$NON-NLS-1$
}

From source file:org.eclipse.jst.jee.model.internal.common.AbstractAnnotationFactory.java

License:Open Source License

/**
 * Return the javaee type of this member. For types return <code>null</code>.
 * For methods with one param return the java type of this param. For fields
 * return the return the java type of the field.
 * /*from   w  w  w  . jav a2 s. c o  m*/
 * If the result is <code>null</code> then this member is not valid and a
 * javaee type can not be returned. This may happen for a method with more
 * then one param or for a field with a class type or primitive type
 * 
 * @param member
 * @param memberType
 * 
 * @return
 * @throws JavaModelException
 */
private String getUnresolvedType(IMember member) throws JavaModelException {
    int memberType = member.getElementType();
    String unresolvedTypeName = null;
    if (memberType == IJavaElement.FIELD) {
        unresolvedTypeName = Signature.toString(((IField) member).getTypeSignature());
    } else if (memberType == IJavaElement.METHOD) {
        IMethod method = (IMethod) member;
        if (method.getNumberOfParameters() != 1)
            return null;
        unresolvedTypeName = Signature.toString(method.getParameterTypes()[0]);
    } else if (memberType == IJavaElement.TYPE)
        return null;
    return unresolvedTypeName;
}