Example usage for org.eclipse.jdt.core IField getAnnotations

List of usage examples for org.eclipse.jdt.core IField getAnnotations

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IField getAnnotations.

Prototype

IAnnotation[] getAnnotations() throws JavaModelException;

Source Link

Document

Returns the annotations for this element.

Usage

From source file:at.bestsolution.efxclipse.tooling.fxml.refactoring.RenameJFXControllerParticipant.java

License:Open Source License

@Override
protected boolean initialize(Object element) {
    if (element instanceof ICompilationUnit) {
        renamedElement = (ICompilationUnit) element;
        try {//from  w w w.java  2 s  .  c  o  m
            for (IJavaElement e : renamedElement.getChildren()) {
                if (e instanceof IType) {
                    // check fields
                    for (IField m : ((IType) e).getFields()) {
                        for (IAnnotation a : m.getAnnotations()) {
                            if (isFxmlAnnotation(a)) {
                                return true;
                            }
                        }
                    }
                    // check methods
                    for (IMethod m : ((IType) e).getMethods()) {
                        for (IAnnotation a : m.getAnnotations()) {
                            if (isFxmlAnnotation(a)) {
                                return true;
                            }
                        }
                    }
                }

            }
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    } else {
        return false;
    }
}

From source file:at.bestsolution.efxclipse.tooling.model.internal.FXCtrlClass.java

License:Open Source License

private Map<String, IFXCtrlField> getLocalFields() {
    if (fields == null) {
        fields = new HashMap<String, IFXCtrlField>();
        try {/* w w  w  . j  a  v  a2 s  .co  m*/
            for (IField f : type.getFields()) {
                boolean annotated = false;
                for (IAnnotation a : f.getAnnotations()) {
                    if (a.getElementName().endsWith("FXML")) {
                        annotated = true;
                        break;
                    }
                }

                if (annotated) {
                    String erasedFQNType = Util.getFQNType((IType) f.getParent(),
                            Signature.getTypeErasure(Signature.toString(f.getTypeSignature())));
                    FXCtrlField field = new FXCtrlField(this, f, erasedFQNType);
                    fields.put(f.getElementName(), field);
                }
            }
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    return fields;
}

From source file:com.codenvy.ide.ext.java.server.SourcesFromBytecodeGenerator.java

License:Open Source License

private void generateType(IType type, StringBuilder builder, String indent) throws JavaModelException {
    int flags = 0;

    appendAnnotationLabels(type.getAnnotations(), flags, builder, indent.substring(TAB.length()));
    builder.append(indent.substring(TAB.length()));
    builder.append(getModifiers(type.getFlags(), type.getFlags())).append(' ').append(getJavaType(type))
            .append(' ').append(type.getElementName());

    if (type.isResolved()) {
        BindingKey key = new BindingKey(type.getKey());
        if (key.isParameterizedType()) {
            String[] typeArguments = key.getTypeArguments();
            appendTypeArgumentSignaturesLabel(type, typeArguments, flags, builder);
        } else {/*from  w ww .j  a va2s  . c o m*/
            String[] typeParameters = Signature.getTypeParameters(key.toSignature());
            appendTypeParameterSignaturesLabel(typeParameters, builder);
        }
    } else {
        appendTypeParametersLabels(type.getTypeParameters(), flags, builder);
    }

    if (!"java.lang.Object".equals(type.getSuperclassName())
            && !"java.lang.Enum".equals(type.getSuperclassName())) {

        builder.append(" extends ");
        if (type.getSuperclassTypeSignature() != null) {
            //                appendTypeSignatureLabel(type, type.getSuperclassTypeSignature(), flags, builder);
            builder.append(Signature.toString(type.getSuperclassTypeSignature()));
        } else {
            builder.append(type.getSuperclassName());
        }
    }
    if (!type.isAnnotation()) {
        if (type.getSuperInterfaceNames().length != 0) {
            builder.append(" implements ");
            String[] signatures = type.getSuperInterfaceTypeSignatures();
            if (signatures.length == 0) {
                signatures = type.getSuperInterfaceNames();
            }
            for (String interfaceFqn : signatures) {
                builder.append(Signature.toString(interfaceFqn)).append(", ");
            }
            builder.delete(builder.length() - 2, builder.length());
        }
    }
    builder.append(" {\n");

    List<IField> fields = new ArrayList<>();
    if (type.isEnum()) {
        builder.append(indent);
        for (IField field : type.getFields()) {
            if (field.isEnumConstant()) {
                builder.append(field.getElementName()).append(", ");
            } else {
                fields.add(field);
            }
        }
        if (", ".equals(builder.substring(builder.length() - 2))) {
            builder.delete(builder.length() - 2, builder.length());
        }
        builder.append(";\n");

    } else {
        fields.addAll(Arrays.asList(type.getFields()));
    }

    for (IField field : fields) {
        if (Flags.isSynthetic(field.getFlags())) {
            continue;
        }
        appendAnnotationLabels(field.getAnnotations(), flags, builder, indent);
        builder.append(indent).append(getModifiers(field.getFlags(), type.getFlags()));
        if (builder.charAt(builder.length() - 1) != ' ') {
            builder.append(' ');
        }

        builder.append(Signature.toCharArray(field.getTypeSignature().toCharArray())).append(' ')
                .append(field.getElementName());
        if (field.getConstant() != null) {
            builder.append(" = ");
            if (field.getConstant() instanceof String) {
                builder.append('"').append(field.getConstant()).append('"');
            } else {
                builder.append(field.getConstant());
            }
        }
        builder.append(";\n");
    }
    builder.append('\n');

    for (IMethod method : type.getMethods()) {
        if (method.getElementName().equals("<clinit>") || Flags.isSynthetic(method.getFlags())) {
            continue;
        }
        appendAnnotationLabels(method.getAnnotations(), flags, builder, indent);
        BindingKey resolvedKey = method.isResolved() ? new BindingKey(method.getKey()) : null;
        String resolvedSig = (resolvedKey != null) ? resolvedKey.toSignature() : null;
        builder.append(indent).append(getModifiers(method.getFlags(), type.getFlags()));

        if (builder.charAt(builder.length() - 1) != ' ') {
            builder.append(' ');
        }
        if (resolvedKey != null) {
            if (resolvedKey.isParameterizedMethod()) {
                String[] typeArgRefs = resolvedKey.getTypeArguments();
                if (typeArgRefs.length > 0) {
                    appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags, builder);
                    builder.append(' ');
                }
            } else {
                String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
                if (typeParameterSigs.length > 0) {
                    appendTypeParameterSignaturesLabel(typeParameterSigs, builder);
                    builder.append(' ');
                }
            }
        } else if (method.exists()) {
            ITypeParameter[] typeParameters = method.getTypeParameters();
            if (typeParameters.length > 0) {
                appendTypeParametersLabels(typeParameters, flags, builder);
                builder.append(' ');
            }
        }

        if (!method.isConstructor()) {

            String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig)
                    : method.getReturnType();
            appendTypeSignatureLabel(method, returnTypeSig, 0, builder);
            builder.append(' ');
            //                builder.append(Signature.toCharArray(method.getReturnType().toCharArray())).append(' ');
        }
        builder.append(method.getElementName());
        builder.append('(');
        for (ILocalVariable variable : method.getParameters()) {
            builder.append(Signature.toString(variable.getTypeSignature()));
            builder.append(' ').append(variable.getElementName()).append(", ");

        }

        if (builder.charAt(builder.length() - 1) == ' ') {
            builder.delete(builder.length() - 2, builder.length());
        }
        builder.append(')');
        String[] exceptionTypes = method.getExceptionTypes();
        if (exceptionTypes != null && exceptionTypes.length != 0) {
            builder.append(' ').append("throws ");
            for (String exceptionType : exceptionTypes) {
                builder.append(Signature.toCharArray(exceptionType.toCharArray())).append(", ");
            }
            builder.delete(builder.length() - 2, builder.length());
        }
        if (type.isInterface() || type.isAnnotation()) {
            builder.append(";\n\n");
        } else {
            builder.append(" {").append(METHOD_BODY).append("}\n\n");
        }
    }
    for (IType iType : type.getTypes()) {
        generateType(iType, builder, indent + indent);
    }
    builder.append(indent.substring(TAB.length()));
    builder.append("}\n");
}

From source file:era.foss.tracer.EraTracerIncrementalProjectBuilder.java

License:Open Source License

private static void handleJavaFile(IFile file) {
    // .java file are the same as ICompilationUnit
    // (@see http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_int_model.htm)
    IJavaElement javaelem = JavaCore.create(file);
    assert (javaelem instanceof ICompilationUnit);
    ICompilationUnit compilunitabstraction = (ICompilationUnit) javaelem;
    // transform ICompUnit into CompUnit for offset-to-linenumber transformation
    CompilationUnit compilunit = parse(compilunitabstraction);

    try {/* ww w. ja  v  a2s.c om*/

        for (IType itype : compilunitabstraction.getTypes()) {

            for (IAnnotation ianno : itype.getAnnotations()) {
                annoToMarker(ianno, itype.getElementName(), file, compilunit);
            }

            for (IField ifield : itype.getFields()) {
                for (IAnnotation ianno : ifield.getAnnotations()) {
                    annoToMarker(ianno, ifield.getElementName(), file, compilunit);
                }
            }

            for (IMethod imeth : itype.getMethods()) {
                for (IAnnotation ianno : imeth.getAnnotations()) {
                    annoToMarker(ianno, imeth.getElementName(), file, compilunit);
                }
            }

        }

    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    logger.info("Resource " + file.getFullPath() + " has been built.");
}

From source file:org.eclipse.fx.ide.fxml.refactoring.RenameJFXControllerParticipant.java

License:Open Source License

@Override
protected boolean initialize(Object element) {
    if (element instanceof ICompilationUnit) {
        this.renamedElement = (ICompilationUnit) element;
        try {/*from  ww w  . j a v  a  2  s.  c o  m*/
            for (IJavaElement e : this.renamedElement.getChildren()) {
                if (e instanceof IType) {
                    // check fields
                    for (IField m : ((IType) e).getFields()) {
                        for (IAnnotation a : m.getAnnotations()) {
                            if (isFxmlAnnotation(a)) {
                                return true;
                            }
                        }
                    }
                    // check methods
                    for (IMethod m : ((IType) e).getMethods()) {
                        for (IAnnotation a : m.getAnnotations()) {
                            if (isFxmlAnnotation(a)) {
                                return true;
                            }
                        }
                    }
                }

            }
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    } else {
        return false;
    }
}

From source file:org.eclipse.fx.ide.model.internal.FXCtrlClass.java

License:Open Source License

private Map<String, IFXCtrlField> getLocalFields() {
    if (this.fields == null) {
        this.fields = new HashMap<String, IFXCtrlField>();
        try {//from  ww w .ja  v  a 2  s .  c  om
            for (IField f : this.type.getFields()) {
                boolean annotated = Flags.isPublic(f.getFlags());
                if (!annotated) {
                    for (IAnnotation a : f.getAnnotations()) {
                        if (a.getElementName().endsWith("FXML")) { //$NON-NLS-1$
                            annotated = true;
                            break;
                        }
                    }
                }

                if (annotated) {
                    String erasedFQNType = Util.getFQNType((IType) f.getParent(),
                            Signature.getTypeErasure(Signature.toString(f.getTypeSignature())));
                    FXCtrlField field = new FXCtrlField(this, f, erasedFQNType);
                    this.fields.put(f.getElementName(), field);
                }
            }
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    return this.fields;
}

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

License:Open Source License

private void processFieldAnnotations(Result result, IType type) throws JavaModelException {
    for (IField field : type.getFields()) {
        for (IAnnotation annotation : field.getAnnotations()) {
            processMemberAnnotations(result, field, annotation);
        }// ww w . ja va 2 s.  co m
    }
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.library.ClassFileParser.java

License:Open Source License

/**
 * Visit a {@code IField}.//ww  w .j a v a 2s . co m
 * 
 * @param field
 *            the {@code IField}
 * @return the {@link FieldDeclaration} object corresponding to the
 *         {@code IField}, or {@code null} if the field does not exist or is
 *         {@link org.eclipse.jdt.core.Flags#isSynthetic(int) synthetic}
 * @throws JavaModelException
 */
protected FieldDeclaration visitField(final IField field) throws JavaModelException {
    if (Flags.isSynthetic(field.getFlags())) {
        return null;
    }

    FieldDeclaration element = getFactory().createFieldDeclaration();
    initializeNode(element);

    // type
    String type = field.getTypeSignature();
    element.setType(getRefOnType(type));

    // visibility modifier
    Modifier m = getFactory().createModifier();
    element.setModifier(m);
    m.setBodyDeclaration(element);
    manageModifier(m, field.getFlags(), field);

    // the fragment of this field
    VariableDeclarationFragment fragment = getFactory().createVariableDeclarationFragment();
    initializeNode(fragment);
    fragment.setExtraArrayDimensions(0);
    fragment.setName(field.getElementName());
    fragment.setVariablesContainer(element);

    for (IAnnotation annotation : field.getAnnotations()) {
        Annotation anno = getFactory().createAnnotation();
        element.getAnnotations().add(anno);
        visitAnnotation(annotation, anno);
    }

    ClassFileParserUtils.manageBindingDeclaration(fragment, field, this);

    return element;
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.library.ClassFileParser.java

License:Open Source License

protected EnumConstantDeclaration visitEnumConstantDeclaration(final IField field) throws JavaModelException {
    EnumConstantDeclaration element = getFactory().createEnumConstantDeclaration();
    initializeNode(element);/*w  ww.j  a v a2 s. co m*/

    element.setName(field.getElementName());
    element.setModifier(getFactory().createModifier());

    // annotations
    for (IAnnotation annotation : field.getAnnotations()) {
        Annotation anno = getFactory().createAnnotation();
        element.getAnnotations().add(anno);
        visitAnnotation(annotation, anno);
    }

    ClassFileParserUtils.manageBindingDeclaration(element, field, this);

    return element;
}

From source file:org.hawkinssoftware.rns.analysis.compile.domain.DomainRoleChecker.java

License:Open Source License

@Override
protected void startHierarchyAnalysis() {
    try {//from ww w.ja va2  s.co  m
        boolean isDomainRole = isDomainRole(hierarchy.getType());
        for (IType superclass : hierarchy.getAllSuperclasses(hierarchy.getType())) {
            isDomainRole |= isDomainRole(superclass);
        }

        int instanceCount = 0;
        for (IField field : hierarchy.getType().getFields()) {
            if (Flags.isStatic(field.getFlags())) {
                for (IAnnotation annotation : field.getAnnotations()) {
                    if (annotation.getElementName()
                            .contains(RNSUtils.getPlainName(DomainRole.Instance.class))) {
                        String message;
                        if (isDomainRole) {
                            // TODO: would much rather use type-qualified names, but the sig doesn't have them
                            if (Signature.getSignatureSimpleName(field.getTypeSignature())
                                    .equals(hierarchy.getType().getElementName())) {
                                // The field is of the same type as the class that declares it, so this can be a
                                // valid instance
                                instanceCount++;
                                continue;
                            } else {
                                message = String.format("Malformed @%s: Field '%s' must be of type %s.",
                                        RNSUtils.getPlainName(DomainRole.Instance.class),
                                        field.getElementName(), hierarchy.getType().getFullyQualifiedName());
                            }
                        } else {
                            message = String.format("Malformed @%s: Field '%s' must be a member of a %s.",
                                    RNSUtils.getPlainName(DomainRole.Instance.class), field.getElementName(),
                                    hierarchy.getType().getFullyQualifiedName());
                        }
                        SourceDeclarationInstruction<FieldDeclaration, IVariableBinding> fieldDeclaration = source
                                .getFieldDeclaration(field);
                        createError(message, fieldDeclaration.markerReferenceNode);
                    }
                }
            }
        }

        SourceDeclarationInstruction<AbstractTypeDeclaration, ITypeBinding> typeDeclaration = source
                .getTypeDeclaration(hierarchy.getType());

        if (isDomainRole) {
            if (instanceCount == 0) {
                String message = String.format(
                        "Malformed %s: class %s must contain one static field of its own type annotated with @%s.",
                        DomainRole.class.getSimpleName(), hierarchy.getType().getFullyQualifiedName(),
                        RNSUtils.getPlainName(DomainRole.Instance.class));
                createError(message, typeDeclaration.markerReferenceNode);
            } else if (instanceCount > 1) {
                String message = String.format("Malformed %s: class %s has multiple fields annotated @%s.",
                        DomainRole.class.getSimpleName(), hierarchy.getType().getFullyQualifiedName(),
                        RNSUtils.getPlainName(DomainRole.Instance.class));
                createError(message, typeDeclaration.markerReferenceNode);
            }
        }
    } catch (CoreException e) {
        e.printStackTrace();
    }
}