Including a Java code snippet by method name, not line number











up vote
2
down vote

favorite
1












I am currently writing documents that contain large amounts of references to source code, in which I would typically include a complete method at a time.



/*
...
*/

public class MyClass {

public MyClass(String... someArguments) {
/*
...
*/
}

@Override
public String toString() {
return "Some information on this specific instance.";
}
}


Currently, I would do this for example as follows:



lstinputlisting[firstnumber=13,firstline=13,lastline=16]{./path/MyClass.java}}



This source code is kept in separate files that are being edited in parallel, resulting in the methods I want to include moving or changing its line count. The method itself might be rewritten to include additional lines, annotations, etc.



A simple addition at the beginning of the document (such as an additional import) results in all references to these lines needing to be updated.



Ideally I would like to do something like this:



importmethod{toString()}{./path/MyClass.java}



or possibly with other options/syntax:



importmethod[annotations=true]{MyClass > toString()}{./path/MyClass.java}



That would always look for the complete section of this method including annotations, JavaDoc comments, etc. as long as the method signature does not change.



Does this already exist in some package or could this be achieved with a macro?










share|improve this question


















  • 1




    You could use text markers, see 5.7 Arbitrary linerange markers in the listings documentation.
    – Ulrike Fischer
    Dec 4 at 12:11










  • Thanks, I was always looking at an outdated version of the reference, this made me search for the current version of the documentation.
    – Z42
    Dec 4 at 20:18

















up vote
2
down vote

favorite
1












I am currently writing documents that contain large amounts of references to source code, in which I would typically include a complete method at a time.



/*
...
*/

public class MyClass {

public MyClass(String... someArguments) {
/*
...
*/
}

@Override
public String toString() {
return "Some information on this specific instance.";
}
}


Currently, I would do this for example as follows:



lstinputlisting[firstnumber=13,firstline=13,lastline=16]{./path/MyClass.java}}



This source code is kept in separate files that are being edited in parallel, resulting in the methods I want to include moving or changing its line count. The method itself might be rewritten to include additional lines, annotations, etc.



A simple addition at the beginning of the document (such as an additional import) results in all references to these lines needing to be updated.



Ideally I would like to do something like this:



importmethod{toString()}{./path/MyClass.java}



or possibly with other options/syntax:



importmethod[annotations=true]{MyClass > toString()}{./path/MyClass.java}



That would always look for the complete section of this method including annotations, JavaDoc comments, etc. as long as the method signature does not change.



Does this already exist in some package or could this be achieved with a macro?










share|improve this question


















  • 1




    You could use text markers, see 5.7 Arbitrary linerange markers in the listings documentation.
    – Ulrike Fischer
    Dec 4 at 12:11










  • Thanks, I was always looking at an outdated version of the reference, this made me search for the current version of the documentation.
    – Z42
    Dec 4 at 20:18















up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I am currently writing documents that contain large amounts of references to source code, in which I would typically include a complete method at a time.



/*
...
*/

public class MyClass {

public MyClass(String... someArguments) {
/*
...
*/
}

@Override
public String toString() {
return "Some information on this specific instance.";
}
}


Currently, I would do this for example as follows:



lstinputlisting[firstnumber=13,firstline=13,lastline=16]{./path/MyClass.java}}



This source code is kept in separate files that are being edited in parallel, resulting in the methods I want to include moving or changing its line count. The method itself might be rewritten to include additional lines, annotations, etc.



A simple addition at the beginning of the document (such as an additional import) results in all references to these lines needing to be updated.



Ideally I would like to do something like this:



importmethod{toString()}{./path/MyClass.java}



or possibly with other options/syntax:



importmethod[annotations=true]{MyClass > toString()}{./path/MyClass.java}



That would always look for the complete section of this method including annotations, JavaDoc comments, etc. as long as the method signature does not change.



Does this already exist in some package or could this be achieved with a macro?










share|improve this question













I am currently writing documents that contain large amounts of references to source code, in which I would typically include a complete method at a time.



/*
...
*/

public class MyClass {

public MyClass(String... someArguments) {
/*
...
*/
}

@Override
public String toString() {
return "Some information on this specific instance.";
}
}


Currently, I would do this for example as follows:



lstinputlisting[firstnumber=13,firstline=13,lastline=16]{./path/MyClass.java}}



This source code is kept in separate files that are being edited in parallel, resulting in the methods I want to include moving or changing its line count. The method itself might be rewritten to include additional lines, annotations, etc.



A simple addition at the beginning of the document (such as an additional import) results in all references to these lines needing to be updated.



Ideally I would like to do something like this:



importmethod{toString()}{./path/MyClass.java}



or possibly with other options/syntax:



importmethod[annotations=true]{MyClass > toString()}{./path/MyClass.java}



That would always look for the complete section of this method including annotations, JavaDoc comments, etc. as long as the method signature does not change.



Does this already exist in some package or could this be achieved with a macro?







listings java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 4 at 10:42









Z42

112




112








  • 1




    You could use text markers, see 5.7 Arbitrary linerange markers in the listings documentation.
    – Ulrike Fischer
    Dec 4 at 12:11










  • Thanks, I was always looking at an outdated version of the reference, this made me search for the current version of the documentation.
    – Z42
    Dec 4 at 20:18
















  • 1




    You could use text markers, see 5.7 Arbitrary linerange markers in the listings documentation.
    – Ulrike Fischer
    Dec 4 at 12:11










  • Thanks, I was always looking at an outdated version of the reference, this made me search for the current version of the documentation.
    – Z42
    Dec 4 at 20:18










1




1




You could use text markers, see 5.7 Arbitrary linerange markers in the listings documentation.
– Ulrike Fischer
Dec 4 at 12:11




You could use text markers, see 5.7 Arbitrary linerange markers in the listings documentation.
– Ulrike Fischer
Dec 4 at 12:11












Thanks, I was always looking at an outdated version of the reference, this made me search for the current version of the documentation.
– Z42
Dec 4 at 20:18






Thanks, I was always looking at an outdated version of the reference, this made me search for the current version of the documentation.
– Z42
Dec 4 at 20:18

















active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f463121%2fincluding-a-java-code-snippet-by-method-name-not-line-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f463121%2fincluding-a-java-code-snippet-by-method-name-not-line-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents