List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration THROWN_EXCEPTIONS_PROPERTY
ChildListPropertyDescriptor THROWN_EXCEPTIONS_PROPERTY
To view the source code for org.eclipse.jdt.core.dom MethodDeclaration THROWN_EXCEPTIONS_PROPERTY.
Click Source Link
From source file:edu.brown.cs.bubbles.bedrock.BedrockElider.java
License:Open Source License
/********************************************************************************/ private String getFormatType(ASTNode n) { String typ = null;//from w w w .j a va 2s .co m if (n instanceof Name) { ASTNode p = n.getParent(); ASTNode pp = p.getParent(); StructuralPropertyDescriptor spd = n.getLocationInParent(); switch (p.getNodeType()) { case ASTNode.METHOD_INVOCATION: if (n.getLocationInParent() == MethodInvocation.NAME_PROPERTY) { typ = "CALL" + getMethodType((Name) n); } break; case ASTNode.SIMPLE_TYPE: case ASTNode.QUALIFIED_TYPE: case ASTNode.TYPE_PARAMETER: typ = "TYPE"; break; case ASTNode.METHOD_DECLARATION: if (spd == MethodDeclaration.NAME_PROPERTY) { typ = "METHODDECL" + getMethodType((Name) n); } else if (spd == MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY) typ = "TYPE"; break; case ASTNode.SINGLE_VARIABLE_DECLARATION: if (pp.getNodeType() == ASTNode.CATCH_CLAUSE) typ = "EXCEPTIONDECL"; else typ = "PARAMDECL"; break; case ASTNode.VARIABLE_DECLARATION_FRAGMENT: if (pp.getNodeType() == ASTNode.FIELD_DECLARATION) typ = "FIELDDECL"; else typ = "LOCALDECL"; break; case ASTNode.ENUM_DECLARATION: case ASTNode.TYPE_DECLARATION: case ASTNode.ANNOTATION_TYPE_DECLARATION: typ = "CLASSDECL" + getClassType((Name) n); break; case ASTNode.MARKER_ANNOTATION: case ASTNode.NORMAL_ANNOTATION: case ASTNode.SINGLE_MEMBER_ANNOTATION: typ = "ANNOT"; break; } } if (typ == null) { if (n instanceof SimpleName) { IBinding ib = ((Name) n).resolveBinding(); // BedrockPlugin.logD("BINDING FOR " + ((SimpleName) n).getIdentifier() + " = " + ib); if (ib != null && ib.getKind() == IBinding.VARIABLE) { IVariableBinding vb = (IVariableBinding) ib; if (vb.isEnumConstant()) typ = "ENUMC"; else if (vb.isField()) { typ = "FIELD" + getVariableType((Name) n); } } else if (ib != null && ib.getKind() == IBinding.METHOD) { typ = "CALL" + getMethodType((Name) n); } else if (ib != null && ib.getKind() == IBinding.ANNOTATION) { typ = "ANNOT"; } else if (ib != null && ib.getKind() == IBinding.TYPE) { typ = "TYPE"; } else if (ib == null) typ = "UNDEF"; } } // TODO: indicate whether the name is a user or system name return typ; }
From source file:edu.brown.cs.bubbles.rebase.java.RebaseJavaElider.java
License:Open Source License
/********************************************************************************/ private String getFormatType(ASTNode n) { String typ = null;/* www .j a v a 2s .co m*/ if (n instanceof Name) { ASTNode p = n.getParent(); ASTNode pp = p.getParent(); StructuralPropertyDescriptor spd = n.getLocationInParent(); switch (p.getNodeType()) { case ASTNode.METHOD_INVOCATION: if (n.getLocationInParent() == MethodInvocation.NAME_PROPERTY) { typ = "CALL" + getMethodType((Name) n); } break; case ASTNode.SIMPLE_TYPE: case ASTNode.QUALIFIED_TYPE: case ASTNode.TYPE_PARAMETER: typ = "TYPE"; break; case ASTNode.METHOD_DECLARATION: if (spd == MethodDeclaration.NAME_PROPERTY) { typ = "METHODDECL" + getMethodType((Name) n); } else if (spd == MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY) typ = "TYPE"; break; case ASTNode.SINGLE_VARIABLE_DECLARATION: if (pp.getNodeType() == ASTNode.CATCH_CLAUSE) typ = "EXCEPTIONDECL"; else typ = "PARAMDECL"; break; case ASTNode.VARIABLE_DECLARATION_FRAGMENT: if (pp.getNodeType() == ASTNode.FIELD_DECLARATION) typ = "FIELDDECL"; else typ = "LOCALDECL"; break; case ASTNode.ENUM_DECLARATION: case ASTNode.TYPE_DECLARATION: case ASTNode.ANNOTATION_TYPE_DECLARATION: typ = "CLASSDECL" + getClassType((Name) n); break; case ASTNode.MARKER_ANNOTATION: case ASTNode.NORMAL_ANNOTATION: case ASTNode.SINGLE_MEMBER_ANNOTATION: typ = "ANNOT"; break; } } if (typ == null) { if (n instanceof SimpleName) { RebaseJavaSymbol js = RebaseJavaAst.getDefinition(n); if (js == null) js = RebaseJavaAst.getReference(n); if (js != null) { switch (js.getSymbolKind()) { case ENUM: typ = "ENUMC"; break; case FIELD: typ = "FIELD" + getVariableType((Name) n); break; case METHOD: case CONSTRUCTOR: typ = "CALL" + getMethodType((Name) n); break; case ANNOTATION: typ = "ANNOT"; break; case CLASS: case INTERFACE: typ = "TYPE"; break; default: break; } } else { RebaseJavaType jt = RebaseJavaAst.getJavaType(n); if (jt != null && !jt.isErrorType()) typ = "TYPE"; else typ = getTypeFromContext(n); if (typ == null) typ = "UNDEF"; } } } // TODO: indicate whether the name is a user or system name return typ; }
From source file:edu.brown.cs.bubbles.rebase.newjava.RebaseJavaElider.java
License:Open Source License
/********************************************************************************/ private String getFormatType(ASTNode n) { String typ = null;//ww w . ja v a2 s . com if (n instanceof Name) { ASTNode p = n.getParent(); ASTNode pp = p.getParent(); StructuralPropertyDescriptor spd = n.getLocationInParent(); switch (p.getNodeType()) { case ASTNode.METHOD_INVOCATION: if (n.getLocationInParent() == MethodInvocation.NAME_PROPERTY) { typ = "CALL" + getMethodType((Name) n); } break; case ASTNode.SIMPLE_TYPE: case ASTNode.QUALIFIED_TYPE: case ASTNode.TYPE_PARAMETER: typ = "TYPE"; break; case ASTNode.METHOD_DECLARATION: if (spd == MethodDeclaration.NAME_PROPERTY) { typ = "METHODDECL" + getMethodType((Name) n); } else if (spd == MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY) typ = "TYPE"; break; case ASTNode.SINGLE_VARIABLE_DECLARATION: if (pp.getNodeType() == ASTNode.CATCH_CLAUSE) typ = "EXCEPTIONDECL"; else typ = "PARAMDECL"; break; case ASTNode.VARIABLE_DECLARATION_FRAGMENT: if (pp.getNodeType() == ASTNode.FIELD_DECLARATION) typ = "FIELDDECL"; else typ = "LOCALDECL"; break; case ASTNode.ENUM_DECLARATION: case ASTNode.TYPE_DECLARATION: case ASTNode.ANNOTATION_TYPE_DECLARATION: typ = "CLASSDECL" + getClassType((Name) n); break; case ASTNode.MARKER_ANNOTATION: case ASTNode.NORMAL_ANNOTATION: case ASTNode.SINGLE_MEMBER_ANNOTATION: typ = "ANNOT"; break; } } if (typ == null) { if (n instanceof SimpleName) { JcompSymbol js = JcompAst.getDefinition(n); if (js == null) js = JcompAst.getReference(n); if (js != null) { switch (js.getSymbolKind()) { case ENUM: typ = "ENUMC"; break; case FIELD: typ = "FIELD" + getVariableType((Name) n); break; case METHOD: case CONSTRUCTOR: typ = "CALL" + getMethodType((Name) n); break; case ANNOTATION: typ = "ANNOT"; break; case CLASS: case INTERFACE: typ = "TYPE"; break; default: break; } } else { JcompType jt = JcompAst.getJavaType(n); if (jt != null && !jt.isErrorType()) typ = "TYPE"; else typ = getTypeFromContext(n); if (typ == null) typ = "UNDEF"; } } } // TODO: indicate whether the name is a user or system name return typ; }
From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJMethod.java
License:Open Source License
@SuppressWarnings("deprecation") public void setExceptions(String[] exceptionTypes) { this.exceptions = exceptionTypes; this.addedExceptions = null; setListNodeProperty(getASTNode(), exceptionTypes, MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY, ASTNode.SIMPLE_NAME);// w w w . j a v a 2 s . com }
From source file:org.eclipse.emf.codegen.merge.java.facade.ast.ASTJMethod.java
License:Open Source License
@SuppressWarnings("deprecation") public void addException(String exceptionType) { if (addedExceptions == null) { addedExceptions = new ArrayList<String>(); }//from w w w .j a va 2s . c o m addedExceptions.add(exceptionType); addValueToListProperty(getASTNode(), exceptionType, MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY, ASTNode.SIMPLE_NAME); }
From source file:org.eclipse.emf.test.tools.merger.ASTTest.java
License:Open Source License
/** * Some test examples on using ASTRewrite to rewrite the code *///from w w w. ja va2 s . c o m @Test public void testWrite() throws Exception { // read String source = TestUtil.readFile(CLASS_FILE, false); ASTParser astParser = CodeGenUtil.EclipseUtil.newASTParser(); astParser.setSource(source.toCharArray()); CompilationUnit sourceCu = (CompilationUnit) astParser.createAST(null); astParser.setSource(source.toCharArray()); CompilationUnit targetCu = (CompilationUnit) astParser.createAST(null); // make modifications TypeDeclaration sourceClass = (TypeDeclaration) sourceCu.types().get(1); TypeDeclaration targetClass = (TypeDeclaration) targetCu.types().get(1); ASTRewrite rewriter = ASTRewrite.create(targetCu.getAST()); IDocument targetDoc = new Document(new String(source.toCharArray())); // copy whole method using strings and placeholder rewrite ASTNode sourceMethodToCopy = sourceClass.getMethods()[4]; ASTNode targetMethodToCopy = rewriter.createStringPlaceholder( source.substring(sourceMethodToCopy.getStartPosition(), sourceMethodToCopy.getStartPosition() + sourceMethodToCopy.getLength()), ASTNode.METHOD_DECLARATION); ListRewrite lrw = rewriter.getListRewrite(targetClass, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); lrw.insertFirst(targetMethodToCopy, null); // setBody() - replace body of the method ASTNode sourceMethodBodyToCopy = sourceClass.getMethods()[5].getBody(); ASTNode targetMethodBodyToCopy = rewriter.createStringPlaceholder( source.substring(sourceMethodBodyToCopy.getStartPosition(), sourceMethodBodyToCopy.getStartPosition() + sourceMethodBodyToCopy.getLength()), ASTNode.BLOCK); rewriter.replace(targetClass.getMethods()[6].getBody(), targetMethodBodyToCopy, null); // setExceptions() replace all exceptions @SuppressWarnings("deprecation") List<?> exceptionsToSet = sourceClass.getMethods()[5].thrownExceptions(); @SuppressWarnings("deprecation") List<?> targetExceptins = targetClass.getMethods()[6].thrownExceptions(); @SuppressWarnings("deprecation") ListRewrite listRewrite = rewriter.getListRewrite(targetClass.getMethods()[6], MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY); lrw = listRewrite; // remove all exceptions for (Iterator<?> it = targetExceptins.iterator(); it.hasNext();) lrw.remove((ASTNode) it.next(), null); // add all exceptions for (Iterator<?> it = exceptionsToSet.iterator(); it.hasNext();) lrw.insertLast((ASTNode) it.next(), null); // copy comment ASTNode comment = sourceClass.getMethods()[3].getJavadoc(); rewriter.set(targetClass.getMethods()[6], MethodDeclaration.JAVADOC_PROPERTY, comment, null); // apply changes TextEdit editsInWriter = rewriter.rewriteAST(targetDoc, null); editsInWriter.apply(targetDoc); String result = targetDoc.get(); File expectedOutputFile = new File( TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/Example1Changed.java").getAbsoluteFile(); String expectedResult = TestUtil.readFile(expectedOutputFile, false); assertEquals(expectedResult, result); }
From source file:org.eclipse.scout.sdk.saml.importer.internal.jdt.imports.OrganizeImportsHelper.java
License:Open Source License
private static int internalGetPossibleTypeKinds(ASTNode node) { int kind = ALL_TYPES; int mask = ALL_TYPES | VOIDTYPE; ASTNode parent = node.getParent();//from ww w .j av a2 s .c o m while (parent instanceof QualifiedName) { if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) { return REF_TYPES; } node = parent; parent = parent.getParent(); mask = REF_TYPES; } while (parent instanceof Type) { if (parent instanceof QualifiedType) { if (node.getLocationInParent() == QualifiedType.QUALIFIER_PROPERTY) { return mask & (REF_TYPES); } mask &= REF_TYPES; } else if (parent instanceof ParameterizedType) { if (node.getLocationInParent() == ParameterizedType.TYPE_ARGUMENTS_PROPERTY) { return mask & REF_TYPES_AND_VAR; } mask &= CLASSES | INTERFACES; } else if (parent instanceof WildcardType) { if (node.getLocationInParent() == WildcardType.BOUND_PROPERTY) { return mask & REF_TYPES_AND_VAR; } } node = parent; parent = parent.getParent(); } switch (parent.getNodeType()) { case ASTNode.TYPE_DECLARATION: if (node.getLocationInParent() == TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) { kind = INTERFACES; } else if (node.getLocationInParent() == TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) { kind = CLASSES; } break; case ASTNode.ENUM_DECLARATION: kind = INTERFACES; break; case ASTNode.METHOD_DECLARATION: if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY) { kind = CLASSES; } else if (node.getLocationInParent() == MethodDeclaration.RETURN_TYPE2_PROPERTY) { kind = ALL_TYPES | VOIDTYPE; } break; case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION: kind = PRIMITIVETYPES | ANNOTATIONS | ENUMS; break; case ASTNode.INSTANCEOF_EXPRESSION: kind = REF_TYPES; break; case ASTNode.THROW_STATEMENT: kind = CLASSES; break; case ASTNode.CLASS_INSTANCE_CREATION: if (((ClassInstanceCreation) parent).getAnonymousClassDeclaration() == null) { kind = CLASSES; } else { kind = CLASSES | INTERFACES; } break; case ASTNode.SINGLE_VARIABLE_DECLARATION: int superParent = parent.getParent().getNodeType(); if (superParent == ASTNode.CATCH_CLAUSE) { kind = CLASSES; } break; case ASTNode.TAG_ELEMENT: kind = REF_TYPES; break; case ASTNode.MARKER_ANNOTATION: case ASTNode.SINGLE_MEMBER_ANNOTATION: case ASTNode.NORMAL_ANNOTATION: kind = ANNOTATIONS; break; case ASTNode.TYPE_PARAMETER: if (((TypeParameter) parent).typeBounds().indexOf(node) > 0) { kind = INTERFACES; } else { kind = REF_TYPES_AND_VAR; } break; case ASTNode.TYPE_LITERAL: kind = REF_TYPES; break; default: } return kind & mask; }
From source file:org.evosuite.eclipse.quickfixes.ResolutionMarkerThrowsException.java
License:Open Source License
@Override public void run(IMarker marker) { // TODO Auto-generated method stub IResource res = marker.getResource(); try {//from w w w . j a v a 2 s . c om String markerMessage = (String) marker.getAttribute(IMarker.MESSAGE); String exception = markerMessage.split(" ")[0]; String[] exceptionPackageArray = exception.split("\\."); String exceptionPackage = ""; for (int i = 0; i < exceptionPackageArray.length - 1; i++) { exceptionPackage += exceptionPackageArray[i] + "."; } exception = exception.substring(exceptionPackage.length()); System.out.println("Package: " + exceptionPackage + ", Exception: " + exception); ICompilationUnit icomp = CompilationUnitManager.getICompilationUnit(res); CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res); int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1; if (position == 1) { int line = marker.getAttribute(IMarker.LINE_NUMBER, 0); position = compunit.getPosition(line, 0); } AST ast = compunit.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); IJavaElement element = icomp.getElementAt(position); IJavaElement method = getMethod(element); // TypeDeclaration td = (TypeDeclaration) compunit.types().get(0); TypeDeclaration td = (TypeDeclaration) compunit.types().get(0); MethodDeclaration md = td.getMethods()[0]; int counter = 1; while (!md.getName().getFullyQualifiedName().equals(method.getElementName()) && counter < td.getMethods().length) { md = td.getMethods()[counter]; System.out.println(md.getName().getFullyQualifiedName() + " " + method.getElementName()); counter++; } ListRewrite lr = rewriter.getListRewrite(md, MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName(exceptionPackage + exception)); ListRewrite lrClass = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY); lrClass.insertAt(id, 0, null); Statement s = (Statement) rewriter.createStringPlaceholder(exception, ASTNode.EMPTY_STATEMENT); lr.insertAt(s, 0, null); System.out.println("MD: " + md.getName() + "\nList: " + lr.getOriginalList()); ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager(); IPath path = compunit.getJavaElement().getPath(); try { bm.connect(path, null, null); ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null); IDocument document = textFileBuffer.getDocument(); TextEdit edits = rewriter.rewriteAST(document, null); edits.apply(document); textFileBuffer.commit(null, false); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedTreeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bm.disconnect(path, null, null); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } // (4) } System.out .println(lr.getRewrittenList() + "\nPosition: " + position + "\nEdits: " + rewriter.toString()); } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (CoreException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
From source file:org.jboss.forge.parser.java.impl.MethodImpl.java
License:Open Source License
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public Method<O> addThrows(final String type) { String packg = Types.getPackage(type); String name = Types.toSimpleName(type); if (!packg.isEmpty()) { getOrigin().addImport(type);/*www .ja v a2 s. c om*/ } SimpleName simpleName = method.getAST().newSimpleName(name); List list = (List) method.getStructuralProperty(MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY); list.add(simpleName); return this; }
From source file:org.jboss.forge.parser.java.impl.MethodImpl.java
License:Open Source License
@Override public List<String> getThrownExceptions() { ArrayList<String> result = new ArrayList<String>(); List<?> list = (List<?>) method.getStructuralProperty(MethodDeclaration.THROWN_EXCEPTIONS_PROPERTY); for (Object object : list) { result.add(object.toString());//from ww w. j a v a2 s. c om } return result; }