List of usage examples for org.eclipse.jdt.core ILocalVariable getElementName
@Override String getElementName();
From source file:at.bestsolution.efxclipse.tooling.fxml.compile.FxmlAnnotationCompilationParticipant.java
License:Open Source License
/** * @param context// www .j a v a 2 s . co m */ private List<CategorizedProblem> checkCU(final ICompilationUnit unit, final Collection<CategorizedProblem> existingProblems) { List<CategorizedProblem> problems = new ArrayList<CategorizedProblem>(); if (existingProblems != null) { problems.addAll(existingProblems); } List<String> fxmlMethods = new ArrayList<String>(); try { IJavaProject project = unit.getJavaProject(); for (IType type : unit.getTypes()) { for (IMethod method : type.getMethods()) { for (IAnnotation a : method.getAnnotations()) { if ("FXML".equals(a.getElementName())) { ////$NON-NLS-1$ if (fxmlMethods.contains(method.getElementName())) { DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(), "JavaFX controller method name is not unique: " //$NON-NLS-1$ + method.getElementName(), IProblem.ExternalProblemNotFixable, new String[0], ProblemSeverities.Warning, method.getSourceRange().getOffset(), method.getSourceRange().getOffset() + method.getSourceRange().getLength(), getMethodLineNumber(type, method), 0); problems.add(problem); } fxmlMethods.add(method.getElementName()); switch (method.getNumberOfParameters()) { case 0: break; case 1: { ILocalVariable pType = method.getParameters()[0]; String[][] resolvedType = type .resolveType(Signature.toString(pType.getTypeSignature())); IType parameterType = null; if (resolvedType != null) { parameterType = project.findType(resolvedType[0][0] + "." + resolvedType[0][1]); //$NON-NLS-1$ } if (resolvedType == null || !Util.assignable(parameterType, project.findType("javafx.event.Event"))) { ////$NON-NLS-1$ DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(), "Parameter '" //$NON-NLS-1$ + pType.getElementName() + "' is not assignable to javafx.event.Event", //$NON-NLS-1$ IProblem.ExternalProblemNotFixable, new String[0], ProblemSeverities.Warning, pType.getSourceRange().getOffset(), pType.getSourceRange().getOffset() + pType.getSourceRange().getLength(), getMethodLineNumber(type, method), 0); problems.add(problem); } } break; default: { DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(), "JavaFX controller method must have 0 or exactly 1 argument", //$NON-NLS-1$ IProblem.ExternalProblemNotFixable, new String[0], ProblemSeverities.Warning, method.getSourceRange().getOffset(), method.getSourceRange().getOffset() + method.getSourceRange().getLength(), getMethodLineNumber(type, method), 0); problems.add(problem); } } } } } } } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } return problems; }
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 a v a 2 s . co 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.ecfeed.ui.common.EclipseModelBuilder.java
License:Open Source License
public MethodNode buildMethodModel(IMethod method) throws JavaModelException { MethodNode methodNode = new MethodNode(method.getElementName()); for (ILocalVariable parameter : method.getParameters()) { String typeName = getTypeName(method, parameter); boolean expected = isAnnotated(parameter, "expected"); methodNode.addParameter(buildParameterModel(parameter.getElementName(), typeName, expected)); }//from w w w . ja va 2s . c o m return methodNode; }
From source file:edu.brown.cs.bubbles.bedrock.BedrockUtil.java
License:Open Source License
private static void outputNameDetails(ILocalVariable lcl, IvyXmlWriter xw) { outputSymbol(lcl, "Local", lcl.getElementName(), null, xw); }
From source file:fr.obeo.ariadne.ide.connector.java.internal.explorer.JavaExplorer.java
License:Open Source License
/** * Explores the given Java method and create a matching Ariadne operation in the given Ariadne classifier. * /*w ww. j av a 2 s . c om*/ * @param iMethod * The Java method to analyzed * @param classifier * The Ariadne classifier * @param isInterface * Indicates if the Java classifier containing the method is an interface * @param monitor * The progress monitor */ private void exploreOperation(IMethod iMethod, Classifier classifier, boolean isInterface, IProgressMonitor monitor) { Operation operation = null; List<Operation> operations = classifier.getOperations(); for (Operation anOperation : operations) { if (anOperation.getQualifiedName().equals(this.getQualifiedName(iMethod, isInterface))) { operation = anOperation; } } if (operation == null) { operation = CodeFactory.eINSTANCE.createOperation(); operation.setQualifiedName(this.getQualifiedName(iMethod, isInterface)); operation.setName(iMethod.getElementName()); classifier.getOperations().add(operation); } if (operation != null) { String javadoc = this.getJavadoc(iMethod, monitor); operation.setDescription(javadoc); operation.setVersion(this.getVersionFromJavadoc(javadoc)); operation.setVisibility(this.getVisibility(iMethod, isInterface)); try { int flags = iMethod.getFlags(); operation.setFinal(Flags.isFinal(flags)); operation.setStatic(Flags.isStatic(flags)); if (Flags.isDeprecated(flags)) { // Add the property "Deprecated" this.addDeprecatedProperty(operation); } // Set the return type String returnType = Signature.toString(iMethod.getReturnType()); Classifier ariadneClassifier = this.getOrCreateClassifier(returnType); if (ariadneClassifier != null) { operation.getReturnTypes().add(ariadneClassifier); } } catch (JavaModelException e) { e.printStackTrace(); } try { // Explore parameters ILocalVariable[] parameters = iMethod.getParameters(); // If we don't have any parameters yet, we create them if (parameters.length > 0 && operation.getParameters().size() == 0) { for (ILocalVariable iLocalVariable : parameters) { String typeSignature = iLocalVariable.getTypeSignature(); Parameter parameter = CodeFactory.eINSTANCE.createParameter(); parameter.setName(iLocalVariable.getElementName()); parameter.setQualifiedName( Signature.toString(typeSignature) + ' ' + iLocalVariable.getElementName()); parameter.setFinal(Flags.isFinal(iLocalVariable.getFlags())); if (Flags.isDeprecated(iLocalVariable.getFlags())) { // Add the property "Deprecated" this.addDeprecatedProperty(parameter); } operation.getParameters().add(parameter); // Set the typing dependency String signature = Signature.toString(iLocalVariable.getTypeSignature()); Classifier ariadneClassifier = this.getOrCreateClassifier(signature); if (ariadneClassifier != null) { parameter.getTypes().add(ariadneClassifier); } } } } catch (JavaModelException e) { e.printStackTrace(); } } }
From source file:io.sarl.eclipse.tests.util.Jdt2EcoreTest.java
License:Apache License
/** Replies a mock of a IMethod (method or constructor). * * <table>//w ww.j ava2s.c o m * <tr><td>Z</td><td>boolean</td></tr> * <tr><td>B</td><td>byte</td></tr> * <tr><td>C</td><td>char</td></tr> * <tr><td>S</td><td>short</td></tr> * <tr><td>I</td><td>int</td></tr> * <tr><td>J</td><td>long</td></tr> * <tr><td>F</td><td>float</td></tr> * <tr><td>D</td><td>double</td></tr> * <tr><td>V</td><td>void</td></tr> * <tr><td>L fully-qualified-class ;</td><td>fully-qualified-class</td></tr> * <tr><td>[ type</td><td>type[]</td></tr> * </table> */ private static IMethod createIMethodMock(boolean isConstructor, IType type, String methodName, String returnType, String[] parameterNames, String[] parameterTypes, IAnnotation[] parameterAnnotations, IAnnotation[] methodAnnotations, int flags) { try { IMethod method = AbstractSarlTest.mock(IMethod.class); when(method.getDeclaringType()).thenReturn(type); when(method.getElementName()).thenReturn(methodName); when(method.getElementType()).thenReturn(IJavaElement.METHOD); when(method.isConstructor()).thenReturn(isConstructor); IAnnotation[] ma = methodAnnotations; if (ma == null) { ma = new IAnnotation[0]; } when(method.getAnnotations()).thenReturn(ma); if (Strings.isNullOrEmpty(returnType)) { when(method.getReturnType()).thenReturn("V"); } else { when(method.getReturnType()).thenReturn(returnType); } when(method.getNumberOfParameters()).thenReturn(parameterNames.length); when(method.getParameterNames()).thenReturn(parameterNames); when(method.getParameterTypes()).thenReturn(parameterTypes); ILocalVariable[] parameters = new ILocalVariable[parameterNames.length]; for (int i = 0; i < parameterNames.length; ++i) { ILocalVariable var = AbstractSarlTest.mock(ILocalVariable.class); when(var.getElementName()).thenReturn(parameterNames[i]); when(var.getTypeSignature()).thenReturn(parameterTypes[i]); when(var.getDeclaringMember()).thenReturn(method); IAnnotation a = parameterAnnotations[i]; if (a == null) { when(var.getAnnotations()).thenReturn(new IAnnotation[0]); } else { when(var.getAnnotations()).thenReturn(new IAnnotation[] { a }); } parameters[i] = var; } when(method.getParameters()).thenReturn(parameters); when(method.getFlags()).thenReturn(flags); return method; } catch (JavaModelException exception) { throw new RuntimeException(exception); } }
From source file:net.sourceforge.tagsea.java.JavaWaypointUtils.java
License:Open Source License
/** * @param variable// w w w .jav a 2 s . c o m * @return */ private static String getStringForVariable(ILocalVariable variable) { try { return Signature.toString(variable.getTypeSignature()) + " " + variable.getElementName(); } catch (Exception e) { return ""; } }
From source file:org.codehaus.groovy.eclipse.refactoring.core.rename.renameLocal.GroovyRenameLocalVariableProcessor.java
License:Apache License
/** * @param localVariable//from w w w .ja v a2s .c o m * @param newName * @param status */ private void initialize(ILocalVariable localVariable, String newName, RefactoringStatus status) { this.localVariable = localVariable; ICompilationUnit unit = (ICompilationUnit) localVariable.getAncestor(IJavaElement.COMPILATION_UNIT); if (unit instanceof GroovyCompilationUnit) { this.unit = (GroovyCompilationUnit) unit; } else { status.merge(RefactoringStatus.createErrorStatus( "Expecting a Groovy compilation unit, but instead found " + unit.getElementName())); } if (newName != null && newName.length() > 0) { if (newName.equals(localVariable.getElementName())) { status.merge(RefactoringStatus.createErrorStatus("New name is the same as the old name")); } setNewElementName(newName); } else { status.merge(RefactoringStatus.createErrorStatus("Invalid new name")); } }
From source file:org.eclipse.fx.ide.fxml.compile.FxmlAnnotationCompilationParticipant.java
License:Open Source License
/** * @param context/* ww w . j a va2s. c o m*/ */ private static List<CategorizedProblem> checkCU(final ICompilationUnit unit, final Collection<CategorizedProblem> existingProblems) { List<CategorizedProblem> problems = new ArrayList<CategorizedProblem>(); if (existingProblems != null) { problems.addAll(existingProblems); } List<String> fxmlMethods = new ArrayList<String>(); try { IJavaProject project = unit.getJavaProject(); for (IType type : unit.getTypes()) { for (IMethod method : type.getMethods()) { for (IAnnotation a : method.getAnnotations()) { if ("FXML".equals(a.getElementName())) { ////$NON-NLS-1$ if (fxmlMethods.contains(method.getElementName())) { DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(), "JavaFX controller method name is not unique: " //$NON-NLS-1$ + method.getElementName(), IProblem.ExternalProblemNotFixable, new String[0], ProblemSeverities.Warning, method.getSourceRange().getOffset(), method.getSourceRange().getOffset() + method.getSourceRange().getLength(), getMethodLineNumber(type, method), 0); problems.add(problem); } fxmlMethods.add(method.getElementName()); switch (method.getNumberOfParameters()) { case 0: break; case 1: { ILocalVariable pType = method.getParameters()[0]; String[][] resolvedType = type .resolveType(Signature.toString(pType.getTypeSignature())); IType parameterType = null; if (resolvedType != null) { parameterType = project.findType(resolvedType[0][0] + "." + resolvedType[0][1]); //$NON-NLS-1$ } if (resolvedType == null || !Util.assignable(parameterType, project.findType("javafx.event.Event"))) { ////$NON-NLS-1$ DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(), "Parameter '" //$NON-NLS-1$ + pType.getElementName() + "' is not assignable to javafx.event.Event", //$NON-NLS-1$ IProblem.ExternalProblemNotFixable, new String[0], ProblemSeverities.Warning, pType.getSourceRange().getOffset(), pType.getSourceRange().getOffset() + pType.getSourceRange().getLength(), getMethodLineNumber(type, method), 0); problems.add(problem); } } break; default: { DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(), "JavaFX controller method must have 0 or exactly 1 argument", //$NON-NLS-1$ IProblem.ExternalProblemNotFixable, new String[0], ProblemSeverities.Warning, method.getSourceRange().getOffset(), method.getSourceRange().getOffset() + method.getSourceRange().getLength(), getMethodLineNumber(type, method), 0); problems.add(problem); } } } } } } } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } return problems; }
From source file:org.eclipse.recommenders.args.completion.rcp.ParameterCompletionProposalComputer.java
License:Open Source License
@Override public final List<ICompletionProposal> computeCompletionProposals( final ContentAssistInvocationContext javaContext, final IProgressMonitor monitor) { List<ICompletionProposal> output = new ArrayList<ICompletionProposal>(); this.jdtContext = (JavaContentAssistInvocationContext) javaContext; this.crContext = ctxFactory.create(jdtContext); //get the type of the node, and the index of parameter ASTNode completeNode = crContext.getCompletionNode().orNull(); String declarationSignature = null; int index = 0; if (completeNode instanceof MessageSend) { MessageSend mn = (MessageSend) completeNode; if (mn.actualReceiverType == null) { return Collections.emptyList(); }/*from w w w . ja v a 2 s . c o m*/ declarationSignature = new String(mn.actualReceiverType.closestMatch().signature()).replace('/', '.'); if (mn.arguments != null) { index = mn.arguments.length; } } else if (completeNode instanceof CompletionOnQualifiedAllocationExpression) { CompletionOnQualifiedAllocationExpression cn = (CompletionOnQualifiedAllocationExpression) completeNode; declarationSignature = new String(cn.resolvedType.signature()).replace('/', '.'); if (cn.arguments != null) { index = cn.arguments.length; } } else { return Collections.EMPTY_LIST; } //calculate the possible parameters for every proposal Map<IJavaCompletionProposal, CompletionProposal> proposals = crContext.getProposals(); for (IJavaCompletionProposal k : proposals.keySet()) { try { InternalCompletionProposal proposol = (InternalCompletionProposal) proposals.get(k); int kind = proposol.getKind(); //if is an anonymous class declaration then continue if (kind == CompletionProposal.ANONYMOUS_CLASS_DECLARATION) { continue; } if (getParameterTypes(proposol).length <= index) { continue; } char[][] parameterNames = proposol.findParameterNames(null); PreciseParamRecommender preciseParamRecommender = new PreciseParamRecommender(); preciseParamRecommender.setIcu(jdtContext.getCompilationUnit()); preciseParamRecommender.setClassName(declarationSignature); preciseParamRecommender.setSrcString(jdtContext.getCompilationUnit().getSource()); String methodName = ""; if (proposol.getName() == null) { String s = declarationSignature; s = s.substring(1, s.length() - 1); Class ownClass = Class.forName(s); methodName = ownClass.getSimpleName(); } else { methodName = new String(proposol.getName()); } preciseParamRecommender.setMethodName(new StringBuffer().append(methodName)); preciseParamRecommender.setParameterNames(proposol.findParameterNames(null)); preciseParamRecommender.setParameterTypes(getParameterTypes(proposol)); preciseParamRecommender.setInvocationOffset(jdtContext.getInvocationOffset()); preciseParamRecommender.setCurrentPosition(index); // TODO: find a better solution to take into account info other than names List<IField> visibleFields = crContext.getVisibleFields(); List<ILocalVariable> visibleLocals = crContext.getVisibleLocals(); List<String> visibleNames = new ArrayList<String>(); for (IField f : visibleFields) { visibleNames.add(f.getElementName()); } for (ILocalVariable v : visibleLocals) { visibleNames.add(v.getElementName()); } preciseParamRecommender.setVisibleNames(visibleNames); preciseParamRecommender.parse(); preciseParamRecommender.recommend(); ICompletionProposal[] argsProposals = integrateProposals( preciseParamRecommender.getRecommendationsList().get(index)); output.addAll(0, Arrays.asList(argsProposals)); } catch (JavaModelException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } List<ICompletionProposal> temp = output; Set<String> containsProposal = new HashSet<String>(); output = new ArrayList<ICompletionProposal>(); for (ICompletionProposal proposal : temp) { if (containsProposal.contains(proposal.getDisplayString())) { continue; } containsProposal.add(proposal.getDisplayString()); output.add(proposal); } if (output.size() > RECOMMENDATION_LIMIT) { output = output.subList(0, RECOMMENDATION_LIMIT - 1); } if (output.size() == 0) { return Collections.emptyList(); } return output; }