Example usage for org.eclipse.jdt.core IType getFields

List of usage examples for org.eclipse.jdt.core IType getFields

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getFields.

Prototype

IField[] getFields() throws JavaModelException;

Source Link

Document

Returns the fields declared by this type in the order in which they appear in the source or class file.

Usage

From source file:at.bestsolution.efxclipse.tooling.fxgraph.ui.contentassist.FXGraphProposalProvider.java

License:Open Source License

private void createEnumPropvalueProposals(IFXEnumProperty prop, EObject model, ContentAssistContext context,
        EReference typeReference, ICompletionProposalAcceptor acceptor) {
    IType t = prop.getEnumType();
    if (t != null) {
        try {//from   w  ww  .  j a v a2  s  .c  o m
            for (IField f : t.getFields()) {
                if (Flags.isEnum(f.getFlags())) {
                    ICompletionProposal p = createCompletionProposal("\"" + f.getElementName() + "\"",
                            new StyledString(f.getElementName()).append(" - " + prop.getEnumTypeAsString(false),
                                    StyledString.QUALIFIER_STYLER),
                            IconKeys.getIcon(IconKeys.ENUM_KEY), getPriorityHelper().getDefaultPriority(),
                            "\"" + context.getPrefix(), context);
                    if (p instanceof ConfigurableCompletionProposal) {
                        ConfigurableCompletionProposal cp = (ConfigurableCompletionProposal) p;
                        cp.setAdditionalProposalInfo(model);
                        cp.setHover(new HoverImpl(f));
                    }

                    acceptor.accept(p);
                }
            }
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:at.bestsolution.efxclipse.tooling.fxgraph.ui.contentassist.FXGraphProposalProvider.java

License:Open Source License

private void collectStaticFields(List<IField> fields, IType type) throws JavaModelException {
    //FIXME Don't we have to check if the field is assignable???
    for (IField f : type.getFields()) {
        if (Flags.isStatic(f.getFlags())) {
            fields.add(f);//from w w w.j  a  v  a 2s .  c  o  m
        }
    }

    String s = type.getSuperclassName();

    if (s != null) {
        String fqn = at.bestsolution.efxclipse.tooling.model.internal.utils.Util.getFQNType(type,
                Signature.getTypeErasure(s));
        collectStaticFields(fields, type.getJavaProject().findType(fqn));
    }
}

From source file:at.bestsolution.efxclipse.tooling.fxgraph.ui.hover.FXHoverProvider.java

License:Open Source License

@Override
public IInformationControlCreatorProvider getHoverInfo(EObject object, ITextViewer viewer, IRegion region) {
    if (object instanceof Element) {
        Element e = (Element) object;
        if (e.getType() != null) {
            IType t = getJDTType(e.getType().getType());
            if (t != null) {
                return createHover(t, object, viewer, region);
            }//from  w w w .j  av  a  2  s  . c  o  m
        }
    } else if (object instanceof Property) {
        Property p = (Property) object;
        if (p.eContainer() instanceof Element) {
            Element e = (Element) p.eContainer();
            if (e.getType() != null) {
                IType t = getJDTType(e.getType().getType());
                if (t != null) {
                    IFXClass fxClass = FXPlugin.getClassmodel().findClass(t.getJavaProject(), t);
                    if (fxClass != null) {
                        IFXProperty fxp = fxClass.getProperty(p.getName());
                        if (fxp != null) {
                            return createHover(fxp.getJavaElement(), object, viewer, region);
                        }
                    }
                }
            }
        }
    } else if (object instanceof StaticCallValueProperty) {
        StaticCallValueProperty sp = (StaticCallValueProperty) object;
        if (sp.getType() != null) {
            IType t = getJDTType(sp.getType().getType());
            if (t != null) {
                IFXClass fxClass = FXPlugin.getClassmodel().findClass(t.getJavaProject(), t);
                if (fxClass != null) {
                    IFXProperty fxp = fxClass.getStaticProperty(sp.getName());
                    if (fxp != null) {
                        return createHover(fxp.getJavaElement(), object, viewer, region);
                    }
                }
            }
        }
    } else if (object instanceof StaticValueProperty) {
        StaticValueProperty sp = (StaticValueProperty) object;

        EObject eo = sp.eContainer();
        Element target = null;

        while (eo.eContainer() != null) {
            if (eo.eContainer() instanceof Element) {
                target = (Element) eo.eContainer();
                break;
            }
            eo = eo.eContainer();
        }

        if (target != null) {
            if (target.getType() != null) {
                IType t = getJDTType(target.getType().getType());
                if (t != null) {
                    IFXClass fxClass = FXPlugin.getClassmodel().findClass(t.getJavaProject(), t);
                    if (fxClass != null) {
                        IFXProperty fxp = fxClass.getStaticProperty(sp.getName());
                        if (fxp != null) {
                            return createHover(fxp.getJavaElement(), object, viewer, region);
                        }
                    }
                }
            }
        }
    } else if (object instanceof ValueProperty) {
        if (object instanceof ControllerHandledValueProperty) {
            ControllerHandledValueProperty cp = (ControllerHandledValueProperty) object;

            Model m = (Model) object.eResource().getContents().get(0);

            if (m != null) {
                ComponentDefinition def = m.getComponentDef();
                if (def != null) {
                    if (def.getController() != null && def.getController().getType() != null) {
                        IType t = getJDTType(def.getController().getType());
                        if (t != null) {
                            IFXCtrlClass fxClass = FXPlugin.getClassmodel().findCtrlClass(t.getJavaProject(),
                                    t);
                            if (fxClass != null) {
                                IFXCtrlEventMethod fxp = fxClass.getAllEventMethods().get(cp.getMethodname());
                                if (fxp != null) {
                                    return createHover(fxp.getJavaElement(), object, viewer, region);
                                }
                            }
                        }
                    }
                }
            }
        } else if (object instanceof SimpleValueProperty) {
            SimpleValueProperty sp = (SimpleValueProperty) object;
            if (sp.eContainer() instanceof Property && sp.getStringValue() != null) {
                Property p = (Property) sp.eContainer();

                if (p.eContainer() instanceof Element) {
                    Element e = (Element) p.eContainer();
                    if (e.getType() != null) {
                        IType t = getJDTType(e.getType().getType());
                        if (t != null) {
                            IFXClass fxClass = FXPlugin.getClassmodel().findClass(t.getJavaProject(), t);
                            if (fxClass != null) {
                                IFXProperty fxp = fxClass.getProperty(p.getName());
                                if (fxp instanceof IFXEnumProperty) {
                                    IType enumType = ((IFXEnumProperty) fxp).getEnumType();
                                    try {
                                        for (IField f : enumType.getFields()) {
                                            if (Flags.isEnum(f.getFlags())) {
                                                if (f.getElementName().equals(sp.getStringValue())) {
                                                    return createHover(f, object, viewer, region);
                                                }
                                            }
                                        }
                                    } catch (JavaModelException ex) {
                                        // TODO Auto-generated catch block
                                        ex.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    return super.getHoverInfo(object, viewer, region);
}

From source file:at.bestsolution.efxclipse.tooling.fxml.editors.FXMLCompletionProposalComputer.java

License:Open Source License

private void createAttributeValueEnumProposals(ContentAssistRequest contentAssistRequest,
        CompletionProposalInvocationContext context, IFXEnumProperty p) {
    IType t = p.getEnumType();
    if (t != null) {
        try {/*ww w  . ja v a 2  s. com*/
            for (IField f : t.getFields()) {
                if (Flags.isEnum(f.getFlags())) {
                    FXMLCompletionProposal cp = createProposal(contentAssistRequest, context,
                            "\"" + f.getElementName(),
                            new StyledString(f.getElementName()).append(" - " + p.getEnumTypeAsString(false),
                                    StyledString.QUALIFIER_STYLER),
                            IconKeys.getIcon(IconKeys.ENUM_KEY), ATTRIBUTE_MATCHER);
                    if (cp != null) {
                        cp.setAdditionalProposalInfo(EcoreFactory.eINSTANCE.createEClass());
                        cp.setHover(new HoverImpl(f));
                        contentAssistRequest.addProposal(cp);
                    }
                }
            }
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:at.bestsolution.efxclipse.tooling.fxml.editors.FXMLTextHover.java

License:Open Source License

public static IJavaElement computeTagAttValueHelp(IDOMNode xmlnode, int offset) {
    NamedNodeMap m = xmlnode.getAttributes();
    IDOMNode attribute = null;/*from   w ww.j a  v a 2  s  . c o  m*/
    if (m == null) {
        return null;
    }
    for (int i = 0; i < m.getLength(); i++) {
        IDOMNode a = (IDOMNode) m.item(i);
        if (a.contains(offset)) {
            attribute = a;
        }
    }

    if (attribute != null) {
        Node parent = xmlnode;
        IFXProperty p = null;

        if ("http://javafx.com/fxml".equals(attribute.getNamespaceURI())) {
            Document d = xmlnode.getOwnerDocument();
            return Util.findType(attribute.getNodeValue(), d);
        }

        if (attribute.getNodeName().contains(".")) {
            String[] parts = attribute.getNodeName().split("\\.");
            IType ownerType = Util.findType(parts[0], parent.getOwnerDocument());
            if (ownerType != null) {
                IFXClass fxClass = FXPlugin.getClassmodel().findClass(ownerType.getJavaProject(), ownerType);
                if (fxClass != null) {
                    p = fxClass.getStaticProperty(parts[1]);

                }
            }
        } else {
            IType ownerType = Util.findType(parent.getNodeName(), parent.getOwnerDocument());
            if (ownerType != null) {
                IFXClass fxClass = FXPlugin.getClassmodel().findClass(ownerType.getJavaProject(), ownerType);
                if (fxClass != null) {
                    p = fxClass.getProperty(attribute.getNodeName());

                }
            }
        }

        if (p instanceof IFXEnumProperty) {
            IType t = ((IFXEnumProperty) p).getEnumType();
            try {
                for (IField f : t.getFields()) {
                    if (Flags.isEnum(f.getFlags())) {
                        if (f.getElementName().equals(attribute.getNodeValue())) {
                            return f;
                        }
                    }
                }
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (p instanceof IFXEventHandlerProperty && attribute.getNodeValue().startsWith("#")) {
            Document d = xmlnode.getOwnerDocument();
            Element e = d.getDocumentElement();
            Attr a = e.getAttributeNodeNS("http://javafx.com/fxml", "controller");
            if (a != null) {
                IType t = Util.findType(a.getValue(), d);
                if (t != null) {
                    IFXCtrlClass cl = FXPlugin.getClassmodel().findCtrlClass(t.getJavaProject(), t);
                    IFXCtrlEventMethod method = cl.getAllEventMethods()
                            .get(attribute.getNodeValue().substring(1));
                    if (method != null) {
                        return method.getJavaElement();
                    }
                }
            }
        }
    }

    return null;
}

From source file:at.bestsolution.efxclipse.tooling.jdt.ui.internal.FXBeanJavaCompletionProposalComputer.java

License:Open Source License

@Override
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context,
        IProgressMonitor monitor) {//from   w w  w. ja v  a  2  s .  c  o  m
    if (context instanceof JavaContentAssistInvocationContext && false) {
        JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
        CompletionContext completionContext = javaContext.getCoreContext();
        IJavaElement enclosingElement = null;
        if (completionContext.isExtended()) {
            enclosingElement = completionContext.getEnclosingElement();
        } else {
            try {
                enclosingElement = javaContext.getCompilationUnit()
                        .getElementAt(context.getInvocationOffset() + 1);
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        List<ICompletionProposal> l = new ArrayList<ICompletionProposal>();

        if (enclosingElement != null) {
            IType type = (IType) enclosingElement.getAncestor(IJavaElement.TYPE);
            if (type == null) {
                return l;
            }

            try {
                IField[] fields = type.getFields();
                IMethod[] methods = type.getMethods();
                int offset = context.getInvocationOffset() - 3;
                if (offset > 0) {
                    String prefix = context.getDocument().get(offset, 3);
                    IType propType = type.getJavaProject().findType("javafx.beans.property.Property");
                    IType writableType = type.getJavaProject().findType("javafx.beans.value.WritableValue");

                    // Primitives
                    IType booleanType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyBooleanProperty");
                    IType doubleType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyDoubleProperty");
                    IType floatType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyFloatProperty");
                    IType intType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyIntegerProperty");
                    IType longType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyLongProperty");
                    IType stringType = type.getJavaProject()
                            .findType("javafx.beans.property.ReadOnlyStringProperty");

                    for (int i = 0; i < fields.length; i++) {
                        IField curr = fields[i];
                        if (!Flags.isEnum(curr.getFlags())) {
                            IType fieldType = toType(type, curr.getTypeSignature());
                            if (fieldType != null) {
                                if (assignable(fieldType, propType)) {
                                    if ("set".equals(prefix)) {
                                        if (assignable(fieldType, writableType)) {
                                            String setterName = NamingConventions.suggestSetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            if (!hasMethod(methods, setterName)) {
                                                StyledString s = new StyledString(setterName
                                                        + "(" + toValue(fieldType, booleanType, doubleType,
                                                                floatType, intType, longType, stringType)
                                                        + ") : void");
                                                s.append(" - Setter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(setterName, s));
                                            }
                                        }
                                    } else if (Character.isWhitespace(prefix.charAt(0))
                                            && prefix.endsWith("is")) {
                                        if (assignable(fieldType, booleanType)) {
                                            String getterName = NamingConventions.suggestGetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            getterName = "is" + getterName.substring(3);
                                            if (!hasMethod(methods, getterName)) {
                                                StyledString s = new StyledString(getterName + "() : boolean");
                                                s.append(" - Getter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(getterName, s));
                                            }
                                        }
                                    } else if ("get".equals(prefix)) {
                                        if (!assignable(fieldType, booleanType)) {
                                            String getterName = NamingConventions.suggestGetterName(
                                                    type.getJavaProject(), curr.getElementName(),
                                                    curr.getFlags(), false, null);
                                            if (!hasMethod(methods, getterName)) {
                                                StyledString s = new StyledString(getterName + "() : "
                                                        + toValue(fieldType, booleanType, doubleType, floatType,
                                                                intType, longType, stringType));
                                                s.append(" - Getter for '" + curr.getElementName() + "'",
                                                        StyledString.QUALIFIER_STYLER);
                                                l.add(new CompletionProposalImpl(getterName, s));
                                            }
                                        }
                                    } else if (Character.isWhitespace(prefix.charAt(2))) {
                                        String propertyName = curr.getElementName() + "Property";
                                        if (!hasMethod(methods, propertyName)) {
                                            StyledString s = new StyledString(propertyName);
                                            l.add(new CompletionProposalImpl(propertyName, s));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BadLocationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return l;
    }
    return Collections.emptyList();
}

From source file:com.android.ide.eclipse.adt.internal.refactorings.core.AndroidTypeRenameParticipant.java

License:Open Source License

private void addJavaChanges(IProject project, CompositeChange result, IProgressMonitor monitor) {
    if (!mIsCustomView) {
        return;//from   ww w  .  j a v  a2  s  .c  o m
    }

    // Also rename styleables, if any
    try {
        // Find R class
        IJavaProject javaProject = BaseProjectHelper.getJavaProject(project);
        ManifestInfo info = ManifestInfo.get(project);
        info.getPackage();
        String rFqcn = info.getPackage() + '.' + R_CLASS;
        IType styleable = javaProject.findType(rFqcn + '.' + ResourceType.STYLEABLE.getName());
        if (styleable != null) {
            IField[] fields = styleable.getFields();
            CompositeChange fieldChanges = null;
            for (IField field : fields) {
                String name = field.getElementName();
                if (name.equals(mOldSimpleName)
                        || name.startsWith(mOldSimpleName) && name.length() > mOldSimpleName.length()
                                && name.charAt(mOldSimpleName.length()) == '_') {
                    // Rename styleable fields
                    String newName = name.equals(mOldSimpleName) ? mNewSimpleName
                            : mNewSimpleName + name.substring(mOldSimpleName.length());
                    RenameRefactoring refactoring = RenameResourceParticipant.createFieldRefactoring(field,
                            newName, true);

                    try {
                        sIgnore = true;
                        RefactoringStatus status = refactoring.checkAllConditions(monitor);
                        if (status != null && !status.hasError()) {
                            Change fieldChange = refactoring.createChange(monitor);
                            if (fieldChange != null) {
                                if (fieldChanges == null) {
                                    fieldChanges = new CompositeChange("Update custom view styleable fields");
                                    // Disable these changes. They sometimes end up
                                    // editing the wrong offsets. It looks like Eclipse
                                    // doesn't ensure that after applying each change it
                                    // also adjusts the other field offsets. I poked around
                                    // and couldn't find a way to do this properly, but
                                    // at least by listing the diffs here it shows what should
                                    // be done.
                                    fieldChanges.setEnabled(false);
                                }
                                // Disable change: see comment above.
                                fieldChange.setEnabled(false);
                                fieldChanges.add(fieldChange);
                            }
                        }
                    } catch (CoreException e) {
                        AdtPlugin.log(e, null);
                    } finally {
                        sIgnore = false;
                    }
                }
            }
            if (fieldChanges != null) {
                result.add(fieldChanges);
            }
        }
    } catch (CoreException e) {
        AdtPlugin.log(e, null);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java

License:Open Source License

private TypeDeclaration convert(IType type, IType alreadyComputedMember,
        TypeDeclaration alreadyComputedMemberDeclaration) throws JavaModelException {
    /* create type declaration - can be member type */
    TypeDeclaration typeDeclaration = new TypeDeclaration(this.compilationResult);

    if (type.getDeclaringType() != null) {
        typeDeclaration.bits |= ASTNode.IsMemberType;
    }/*from   w  w w  . ja  v a 2  s .  c  om*/
    typeDeclaration.name = type.getElementName().toCharArray();
    typeDeclaration.modifiers = type.getFlags();

    /* set superclass and superinterfaces */
    if (type.getSuperclassName() != null) {
        TypeReference typeReference = createTypeReference(type.getSuperclassTypeSignature());
        if (typeReference != null) {
            typeDeclaration.superclass = typeReference;
            typeDeclaration.superclass.bits |= ASTNode.IsSuperType;
        }
    }

    String[] interfaceTypes = type.getSuperInterfaceTypeSignatures();
    int interfaceCount = interfaceTypes == null ? 0 : interfaceTypes.length;
    typeDeclaration.superInterfaces = new TypeReference[interfaceCount];
    int count = 0;
    for (int i = 0; i < interfaceCount; i++) {
        TypeReference typeReference = createTypeReference(interfaceTypes[i]);
        if (typeReference != null) {
            typeDeclaration.superInterfaces[count] = typeReference;
            typeDeclaration.superInterfaces[count++].bits |= ASTNode.IsSuperType;
        }
    }
    if (count != interfaceCount) {
        System.arraycopy(typeDeclaration.fields, 0,
                typeDeclaration.superInterfaces = new TypeReference[interfaceCount], 0, interfaceCount);
    }

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {

        /* convert type parameters */
        ITypeParameter[] typeParameters = type.getTypeParameters();
        if (typeParameters != null && typeParameters.length > 0) {
            int parameterCount = typeParameters.length;
            org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParams = new org.eclipse.jdt.internal.compiler.ast.TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                ITypeParameter typeParameter = typeParameters[i];
                typeParams[i] = createTypeParameter(typeParameter.getElementName().toCharArray(),
                        stringArrayToCharArray(typeParameter.getBounds()), 0, 0);
            }

            typeDeclaration.typeParameters = typeParams;
        }
    }

    /* convert member types */
    IType[] memberTypes = type.getTypes();
    int memberTypeCount = memberTypes == null ? 0 : memberTypes.length;
    typeDeclaration.memberTypes = new TypeDeclaration[memberTypeCount];
    for (int i = 0; i < memberTypeCount; i++) {
        if (alreadyComputedMember != null && alreadyComputedMember.getFullyQualifiedName()
                .equals(memberTypes[i].getFullyQualifiedName())) {
            typeDeclaration.memberTypes[i] = alreadyComputedMemberDeclaration;
        } else {
            typeDeclaration.memberTypes[i] = convert(memberTypes[i], null, null);
        }
        typeDeclaration.memberTypes[i].enclosingType = typeDeclaration;
    }

    /* convert fields */
    IField[] fields = type.getFields();
    int fieldCount = fields == null ? 0 : fields.length;
    typeDeclaration.fields = new FieldDeclaration[fieldCount];
    count = 0;
    for (int i = 0; i < fieldCount; i++) {
        FieldDeclaration fieldDeclaration = convert(fields[i], type);
        if (fieldDeclaration != null) {
            typeDeclaration.fields[count++] = fieldDeclaration;
        }
    }
    if (count != fieldCount) {
        System.arraycopy(typeDeclaration.fields, 0, typeDeclaration.fields = new FieldDeclaration[count], 0,
                count);
    }

    /* convert methods - need to add default constructor if necessary */
    IMethod[] methods = type.getMethods();
    int methodCount = methods == null ? 0 : methods.length;

    /* source type has a constructor ?           */
    /* by default, we assume that one is needed. */
    int neededCount = 1;
    for (int i = 0; i < methodCount; i++) {
        if (methods[i].isConstructor()) {
            neededCount = 0;
            // Does not need the extra constructor since one constructor already exists.
            break;
        }
    }
    boolean isInterface = type.isInterface();
    neededCount = isInterface ? 0 : neededCount;
    typeDeclaration.methods = new AbstractMethodDeclaration[methodCount + neededCount];
    if (neededCount != 0) { // add default constructor in first position
        typeDeclaration.methods[0] = typeDeclaration.createDefaultConstructor(false, false);
    }
    boolean hasAbstractMethods = false;
    count = 0;
    for (int i = 0; i < methodCount; i++) {
        AbstractMethodDeclaration method = convert(methods[i], type);
        if (method != null) {
            boolean isAbstract;
            if ((isAbstract = method.isAbstract()) || isInterface) { // fix-up flag
                method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
            }
            if (isAbstract) {
                hasAbstractMethods = true;
            }
            typeDeclaration.methods[neededCount + (count++)] = method;
        }
    }
    if (count != methodCount) {
        System.arraycopy(typeDeclaration.methods, 0,
                typeDeclaration.methods = new AbstractMethodDeclaration[count + neededCount], 0,
                count + neededCount);
    }
    if (hasAbstractMethods) {
        typeDeclaration.bits |= ASTNode.HasAbstractMethods;
    }
    return typeDeclaration;
}

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 {//ww  w  . j  av  a 2 s  . 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:com.curlap.orb.plugin.generator.impl.CurlDataClassGeneratorImpl.java

License:Open Source License

protected List<Field> getAllSuperclassFields(Set<String> importPackages, String superclass)
        throws JavaModelException {
    List<Field> fields = new ArrayList<Field>();
    TypeNameMatch typeNameMatch = new JavaElementSearcher(iCompilationUnit).searchClassInfo(superclass);
    if (typeNameMatch == null)
        return fields;
    IType iType = typeNameMatch.getType();
    for (IField iField : iType.getFields()) {
        // skip static field
        if (Flags.isStatic(iField.getFlags()))
            continue;
        String name = iField.getElementName();
        Field field = new Field();
        field.setName(name);/*w ww . j a  v a  2  s. c  o  m*/
        field.setType(CurlSpecUtil.marshalCurlTypeWithSignature(
                addImportedPackageIfNecessaryWithSignature(importPackages, iField.getTypeSignature())));
        field.setDefaultValue(CurlSpecUtil.getDefaultValue(field.getType()));
        field.setIsTransient(Flags.isTransient(iField.getFlags()));
        String modifier = Flags.toString(iField.getFlags());
        if (modifier.length() == 0)
            modifier = "package";
        field.setGetterModifier(modifier + "-get");
        field.setSetterModifier(modifier + "-set");
        field.setComment("");
        fields.add(field);
    }
    // more superclasses
    if (iType.getSuperclassName() != null)
        fields.addAll(getAllSuperclassFields(importPackages, iType.getSuperclassName()));
    return fields;
}